oop class lawrence d’antonio lecture 3 an overview of c++
TRANSCRIPT
OOP ClassOOP ClassLawrence D’AntonioLawrence D’Antonio
Lecture 3Lecture 3
An Overview of C++An Overview of C++
Classes/ObjectsClasses/Objects
Basic unit of OOPL Basic unit of OOPL Autonomous entitiesAutonomous entities State, behavior, identityState, behavior, identity Data encapsulationData encapsulation Encapsulation of responsibilitiesEncapsulation of responsibilities Information hidingInformation hiding Message passingMessage passing Exception handlingException handling
Object OrganizationObject Organization
Inheritance (Single and multiple)Inheritance (Single and multiple) Dynamic bindingDynamic binding Generalization/SpecializationGeneralization/Specialization AbstractionAbstraction Has-a versus is-a relationshipHas-a versus is-a relationship Uses relationshipUses relationship Polymorphism/ Virtual functionsPolymorphism/ Virtual functions
PolymorphismPolymorphism
Virtual functionsVirtual functions OverloadingOverloading Type conversionsType conversions TemplatesTemplates
Generic programmingGeneric programming
TemplatesTemplates STLSTL TraitsTraits MetaprogrammingMetaprogramming Boost libraryBoost library Programming with conceptsProgramming with concepts
TypingTyping
Statically typed. Variables must be Statically typed. Variables must be declared before used. Exceptions: casts, declared before used. Exceptions: casts, unions, temporary objects.unions, temporary objects.
Weakly typed (?). Variables are not bound Weakly typed (?). Variables are not bound to a specific data type.to a specific data type.
Type conversionsType conversions SubtypingSubtyping
Code ReuseCode Reuse
Parametric polymorphismParametric polymorphism Subtyping polymorphismSubtyping polymorphism CompositionComposition PatternsPatterns
What is a class?What is a class?
A class is a set of objects sharing common A class is a set of objects sharing common features.features.
A class defines an object’s attributes and A class defines an object’s attributes and behavior. Methods are provided to act on behavior. Methods are provided to act on an object and to pass messages between an object and to pass messages between objects.objects.
A class is the basic unit of abstraction.A class is the basic unit of abstraction. A class is the basic unit of modularity.A class is the basic unit of modularity. A class can be concrete or abstract.A class can be concrete or abstract.
What is an object?What is an object?
An object is an instance of a class.An object is an instance of a class. An object has state, behavior, identity.An object has state, behavior, identity.
What is an object?
Coad-Yourdon
An abstraction of something in a problem domain, reflecting the capabilities of the system to keep information about it, interact with it, or both; an encapsulation of attribute values and their exclusive services.
What is an object?
OMG
An object is a thing. It is created as the instance of an object type. Each object has a unique identity that is distinct from and independent of any of its characteristics. Each object offers one or more operations.
What is an object?
Firesmith
An object is defined as a software abstraction that models all relevant aspects of a single tangible or conceptual entity or thing from the application domain or solution space. An object is one of the primary entities in an object-oriented application, typically corresponds to a software module, and consists of a set of related attribute types, messages, exceptions, operations, and optional component objects.
What is an object?
Booch
From the perspective of human cognition, an object is any of the following:
A tangible and/or visible thing. Something that may be apprehended
intellectually. Something toward which thought or action is
directed.
What is an object?
Booch continued.
An object has state, behavior, and identity; the structure and behavior of similar objects are defined in their common class; the terms instance and object are interchangeable.
What is an object?
Shlaer-Mellor
An object is an abstraction of a set of real-world things such that:
All the things in the set have the same characteristic.
All instances are subject to and conform to the same set of rules and policies.
What is an object?
Jacobson
An object is characterized by a number of operations and a state which remembers the effect of these operations.
What is encapsulation?What is encapsulation?
Internal details of objects are concealed Internal details of objects are concealed from the objects users (information hiding).from the objects users (information hiding).
Both data and implementation may be Both data and implementation may be hidden. The object is a black box.hidden. The object is a black box.
Access to members is controlled through Access to members is controlled through the class definition.the class definition.
The accessible part of a class is called its The accessible part of a class is called its interface.interface.
Data encapsulation exampleclass Clock { private:
int hours; // 1-12 private int minutes; // 0-59
public:Clock(int h, int m) {
if (h < 1 || h > 12) { throw(”Hours must be between 1 and
12″); } if (m < 0 || m > 59) {
throw(”Minutes must be between 0 and 59″);
} h = hours; m = minutes;
}//...
};
Class Invariants
The above is an example of “Programming by Contract.”
The class guarantees that
These are called class invariants
1 hours 12, 0 minutes 59
What is a method?What is a method?
A method is a member function that acts A method is a member function that acts upon an object. A method is generally upon an object. A method is generally called for one object (exception: static called for one object (exception: static members).members).
Commonly found methods are Commonly found methods are constructors, destructors, assignment, constructors, destructors, assignment, mutators, accessors.mutators, accessors.
What is message passing?What is message passing?
Messages are transfers of data or Messages are transfers of data or requests for another object to take an requests for another object to take an action.action.
What is polymorphism?What is polymorphism?
Different types of objects respond to the Different types of objects respond to the same message and use the appropriate same message and use the appropriate method.method.
Parametric polymorphism parametrizes the Parametric polymorphism parametrizes the object type (e.g., a list class, where the object type (e.g., a list class, where the type of object stored is parametrized).type of object stored is parametrized).
Subtype (or inclusion) polymorphism allows Subtype (or inclusion) polymorphism allows objects of a given type to be substituted for objects of a given type to be substituted for by objects of a subtype.by objects of a subtype.
What is inheritance?What is inheritance?
One class (derived/child) relies on the definition of another class (base/parent).
Single vs. multiple inheritance A method of sharing code or sharing
interface among classes. Language may define a class tree (with
single root): Java, Smalltalk Language may define a class forest: C++
What is typing?What is typing?
Static typing: Data type determined at compile-time. Type must be declared.
Dynamic typing: Data type may be determined at run-time. Type need not be declared.
Strong typing: Variables are bound to a specific type.
Weak typing: A variable’s type may change.
Varieties of typing
Static and strong typing: Java, Pascal, OCaml, Haskell
Static and weak typing: C/C++ Dynamic and strong typing: Python Dynamic and weak typing: PHP
Dynamic typing example# Python example
class Cat: def speak(self): print "meow!"
class Dog: def speak(self): print "woof!"
class Bob: def speak(self): print "hello world!"
def command(pet): pet.speak()
pets = [ Cat(), Dog(), Bob() ]
for pet in pets: command(pet)
Weak typing example
var x := 5;
var y := "37";
Print(x + y);
In Visual Basic this prints: 42
In JavaScript this prints: 537
What is exception handling?
The mechanism used to report and recover from abnormal states.
When an error is detected, execution is passed to a handler.