Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Programming with C++ Yingcai Xiao.

Similar presentations


Presentation on theme: "Object-Oriented Programming with C++ Yingcai Xiao."— Presentation transcript:

1

2 Object-Oriented Programming with C++ Yingcai Xiao

3 Code Reuse Inheritance Encapsulation Polymorphism

4 Two ways of code reuse: composition & inheritance Composition (“has-a” relationship): An object of a class has an object of another class in it. Use predefined code of a class by creating an object of the class. Inheritance (“is-a” relationship): A child class inherits everything from its parent class. Use predefined code of a class by sub-classing from it.

5 class CWheel { private: float air-pressure; public: void inflate(); … }; // predefined class class CPassengerCar { private: int number-of-seats; CWheel fl-wheel, fr-wheel; CWheel rl-wheel, rr-wheel; public: … };

6 class CCar { private: CEngine engine; CBreak break; public: void accelerate(); void stop(); … }; // predefined class class CPassengerCar : public CCar { private: int number-of-seats; CWheel fl-wheel, fr-wheel; CWheel rl-wheel, rr-wheel; public: … }; “a passenger car is a car” “a truck is a car” “a race car is a car”

7 class CCar { private: CEngine engine; CBreak break; public: void accelerate(); void stop(); … }; Grouping data and operations (functions/methods) together to facilitate code reuse. Controlling the access of the members. (Private members are hidden and can only be accessed by the member functions of the class. Users can only use the given public functions to alter the private data members).

8 class CCar { protected: CEngine engine; CBreak break; public: void accelerate(); void stop(); … }; Protected members are hidden and can only be accessed by the member functions of the class and its decedent classes.

9 class CShape { private: int color; public: virtual float area() { }; // a virtual function can be overridden by the children }; One method behaves differently for different objects. class CCircle : public CShape{ public: float area() {return (pi * r * r)}; }; class CRect : public CShape{ public: float area() {return (w*h)}; };

10 CShape *shapes[4]; CCircle *c1 = new CCircle(5); CCircle *c2 = new CCircle(10); CRect *r1 = new CRect(4,5); CRect *r2 = new CRect(8,5); shapes[0] = (CShape *) c1; // cast to parent allowed shapes[1] = (CShape *) c2; // cast to parent allowed shapes[2] = (CShape *) r1; // cast to parent allowed shapes[3] = (CShape *) r2; // cast to parent allowed for (I=0; I area() << endl; One function behaves differently for different objects.

11 CObject in MFC is the parent of all classes in an MFC based application. Therefore MFC can use a loop to process the objects in the application without knowing how the classes in the application are defined as long as they are subclassed from CObject and follow the standards.

12 Polymorphism makes it possible for objects of different child classes to have the same interface. To enforce a standard interface, one can make the parent class an abstract class and subclass from it. An abstract class is a class containing a pure virtual function. One can’t instantiated an abstract class. It is just for setting up standard interfaces. Its child classes must override and implement the pure virtual function. (A regular virtual function is not required to be overridden by the child classes and it is inherited if it is not overridden.) Setting up standard interfaces

13 class CShape { public: virtual float area() = 0; // a pure virtual function }; // CShape a-shape; not allowed. class CCircle : public CShape{ public: float area(); // must be implemented. }; class CRect : public CShape{ public: float area(); // must be implemented. };

14 Two types of code reuse: inclusion (has-a) inheritance (is-a) Three pillars of OOP: encapsulation (to facilitate code reuse) inheritance (code reuse) polymorphism (to facilitate code reuse)


Download ppt "Object-Oriented Programming with C++ Yingcai Xiao."

Similar presentations


Ads by Google