Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS415 C++ Programming Takamitsu Kawai 304-293-0405 x4212 G11 CERC building WV Virtual Environments Lab West Virginia University."— Presentation transcript:

1 CS415 C++ Programming Takamitsu Kawai kawai@csee.wvu.edu 304-293-0405 x4212 G11 CERC building WV Virtual Environments Lab West Virginia University

2 Object-Oriented Programming Concepts The Object Model –Object, Attributes, Methods, Messages –Encapsulation –Class & Instance Inheritance –Base class, Derived class Polymorphism –Dynamic binding –Static binding

3 The Object Model

4 Object = Attributes + Methods message attrib utes method

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

6 Example 1: Bicycle object change_gears change_cadence brake - speed - cadence - gear (cadence: how frequent you pump the pedal.)

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

8 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 attributes methods

9 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(); };

10 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

11 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 NO! OK

12 Class & Instance change gears brake - speed - cadence - gear change gears brake - speed = 15 [mph] - cadence = 60 [rpm] - gear = 3rd change cadence change cadence Class Instance -A blueprint of an object -No occupied memory space (just a declaration) -An actually created object -Requires memory space to store attributes Instantiate

13 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] class Bicycle

14 Inheritance

15 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)

16 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

17 Inheritance Ex. 1: Telephone Objects class 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

18 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. “ WirelessTelephone is a kind of Telephone.”

19 Inheritance Ex. 2: Graphics Objects Shape RectangleCrossEllipse virtual void Shape::draw(const Canvas*); void Rectangle::draw(const Canvas*); void Cross ::draw(const Canvas*); void Ellipse ::draw(const Canvas*); extensible...

20 Polymorphism

21 Polymorphism Ex. 1: Drawing Tool RectangleTriangle Ellipse

22 What we want to do ? Shape* shapes[10] Put them in a “generic” container, and... RectangleTriangle Ellipse

23 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”

24 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” So, a proper draw() function for each object needs to be automatically chosen in runtime (dynamic binding).

25 Polymorphism Ex. 2:Operator ‘ = ‘ and ‘ + ’ int x = 1; int y = 2; int z; What should happen if 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).

26 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…


Download ppt "CS415 C++ Programming Takamitsu Kawai 304-293-0405 x4212 G11 CERC building WV Virtual Environments Lab West Virginia University."

Similar presentations


Ads by Google