oop class lawrence d’antonio lecture 3 an overview of c++

28
OOP Class OOP Class Lawrence D’Antonio Lawrence D’Antonio Lecture 3 Lecture 3 An Overview of C++ An Overview of C++

Upload: angelina-gregory

Post on 03-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

OOP ClassOOP ClassLawrence D’AntonioLawrence D’Antonio

Lecture 3Lecture 3

An Overview of C++An Overview of C++

Page 2: OOP Class Lawrence D’Antonio Lecture 3 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

Page 3: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

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

Page 4: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

PolymorphismPolymorphism

Virtual functionsVirtual functions OverloadingOverloading Type conversionsType conversions TemplatesTemplates

Page 5: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

Generic programmingGeneric programming

TemplatesTemplates STLSTL TraitsTraits MetaprogrammingMetaprogramming Boost libraryBoost library Programming with conceptsProgramming with concepts

Page 6: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

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

Page 7: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

Code ReuseCode Reuse

Parametric polymorphismParametric polymorphism Subtyping polymorphismSubtyping polymorphism CompositionComposition PatternsPatterns

Page 8: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

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.

Page 9: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

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.

Page 10: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

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.

Page 11: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

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.

Page 12: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

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.

Page 13: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

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.

Page 14: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

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.

Page 15: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

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.

Page 16: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

What is an object?

Jacobson

An object is characterized by a number of operations and a state which remembers the effect of these operations.

Page 17: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

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.

Page 18: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

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;

}//...

};

Page 19: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

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

Page 20: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

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.

Page 21: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

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.

Page 22: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

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.

Page 23: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

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++

Page 24: OOP Class Lawrence D’Antonio Lecture 3 An Overview of 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.

Page 25: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

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

Page 26: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

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)

Page 27: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

Weak typing example

var x := 5;

var y := "37";

Print(x + y);

In Visual Basic this prints: 42

In JavaScript this prints: 537

Page 28: OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++

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.