26
CS415 C++ Programming Takamitsu Kawai [email protected] 304-293-0405 x4212 G11 CERC building WV Virtual Environments Lab West Virginia University

CS415 C++ Programming Takamitsu Kawai [email protected] 304-293-0405 x4212 G11 CERC building WV Virtual Environments Lab West Virginia University

Embed Size (px)

Citation preview

CS415

C++ Programming

Takamitsu [email protected]

304-293-0405 x4212

G11 CERC building

WV Virtual Environments Lab

West Virginia University

Object-Oriented Programming Concepts

• The Object Model– Object, Attributes, Methods, Messages– Encapsulation– Class & Instance

• Inheritance– Base class, Derived class

• Polymorphism– Dynamic binding– Static binding

The Object Model

The Object Model

Object = Attributes + Methods

message

message

message

message

attributes

method

method

method

method

How OO Programs Work

- Based on message passing among objects- In C++, sending/receiving a message corresponds to a member function call.

Example 1: Bicycle object

change_gears

change_cadence

brake- speed

- cadence

- gear

(cadence: how frequent you pump the pedal.)

class Bicycle {private: float speed; float cadence; int gear;public: void change_gears(int gear); void break(); void chage_cadence(float cadence);};

In C++,...

member variables (attributes)

member functions (methods)

Example 2: CD Player Object (from the lab assignment)

CD player object

bool powerOn;

bool trayOpen;

bool mediaLoaded;

bool playing;

void pushPower(void);

void pushEject(void);

void pushPlay(void);

void pushStop(void);

void loadUnloadMedia(void);

messages

interface

attributesmethods

In C++,...

member variables (attributes)

member functions (methods)

class CDPlayer {private: bool powerOn; bool trayOpen; bool mediaLoaded; bool playing;public: void pushPower(); void pushEject(); void pushPlay(); void pushStop(); void loadUnloadMedia();};

• Attributes : data that describe the internal status of an object– “member variables” in C++

– inaccessible from outside Encapsulation– also known as “state”, etc.

• Methods : functions which can access the internal status of an object– “member functions” in C++– accessible from outside– manipulates attributes– also known as “behavior”, etc.

The Object Model

Object = Attributes + Methods

Encapsulation

change gears

change cadence

brake- speed

- cadence

- gear

User

Users should use methods to manipulate objects.Users should not access attributes directly.Access to the attributes is done by methods.Methods can keep the consistency of the attributes.

OK

OK

NO!

OK

Class & Instance

change gears

brake- speed

- cadence

- gear

change gears

brake - speed = 15 [mph]- cadence = 60 [rpm]- gear = 3rd

changecadence

changecadence

Class Instance-A blueprint of an object-No occupied memory space (just a declaration)

-An actually created object -Requires memory spaceto store attributes

Instantiate

Instance and member variables

instance 1

- cadence = 60 [rpm]- gear = 3rd

Different instances can have different values of member variables

- speed = 4 [mph]- cadence = 12 [rpm]- gear = 1st

- speed = 35 [mph]- cadence = 80 [rpm]- gear = 5th

instance 2

instance 3

- speed = 10 [mph]classBicycle

Inheritance

Inheritance

MountainBike

We can make “specialized” version of Bicycles by adding more features (attributes, methods)

Base class (Super class)

RacingBike TandemBike

Bicycle

Derived classes (Subclasses) (this means inheritance)

Benefits from Inheritance

• Enhances extensibility of programs

– class library, application frameworks (Ex. MFC etc.)• provides “base” classes to create new

classes specific to new problem domain• you can create new classes by adding new

features to existing classes

• Simplifies programs

– enables generic programming

Inheritance Ex. 1: Telephone Objectsclass Telephone

hangupStatus

pickUp()dial()hangUp()

class SpeakerTelephone

hangupStatus

pickUp()dial()hangUp()

speakerVolume

volumeUp()

volumeDown()

class WirelessTelephone

hangupStatus

pickUp()dial()hangUp()

antennaStatus

extendAntenna()

shrinkAntenna()

class VoiceMailTelephone

hangupStatus

pickUp()dial()hangUp()

preservedMassages

listenMessage()

deleteMessage()

Superclass(Base Class)

Subclasses(Derived Classes)

attributes

methods

General or Specific

Base classes - general - less members - smaller object size

Derived Classes - specific - more members - larger object size

class Telephone

class SpeakerTelephone

class WirelessTelephone

class VoiceMailTelephone

class SpeakerRedialTelephone

Inheritance is called“is-a” relationship.(cf. “has-a” relationship)

Ex. “WirelessTelephoneis a kind of Telephone.”

Inheritance Ex. 2: Graphics Objects

Shape

Rectangle Cross Ellipse

virtual void Shape::draw(const Canvas*);

void Rectangle::draw(const Canvas*);void Cross ::draw(const Canvas*);void Ellipse ::draw(const Canvas*);

extensible...

Polymorphism

Polymorphism Ex. 1: Drawing Tool

Rectangle Triangle

Ellipse

What we want to do ?

Shape* shapes[10]

Put them in a “generic” container, and...

Rectangle Triangle

Ellipse

What we want to do ?

Shape* shapes[10]

... manipulate them by sending the same message

“draw”

CharScreen s(20,10);

for (i = 0; i < numShapes; i++) { shapes[i]->draw(s);}

“draw”“draw”

What we want to do ?

Shape* shapes[10]

But, how to draw() each object should be different...

“draw”

void Rectangle::draw() { // draw a rectangle...}

void Triangle::draw() { // draw a triangle...}

void Ellipse::draw() { // draw an ellipse...}

“draw”“draw”

So, a proper draw() function for each object needs to be automatically chosen in runtime(dynamic binding).

Polymorphism Ex. 2:Operator ‘=‘ and ‘+’

int x = 1;int y = 2;int z;

What should happenif you say z = x + y; for each case?

String x=”abc”;String y=”def”;String r;

double x = 1.2;double y = 3.4; double z;

Complex x(1.0, 2.0); Complex y(2.0, 3.0); Complex z;

Different types of ‘+’ and ‘=‘ operations should be executed based on the data type. Which function is used can be determined in compile time (static binding).

Polymorphism

For a given (same) message, to respond in a different way based on the receiver’s object type.

• dynamic binding• static binding

to appear in the later lecture…