Você está na página 1de 13

Object-Oriented Programming

with C++

Yingcai Xiao

Code Reuse Inheritance Encapsulation Polymorphism

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.

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

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

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

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.

One method behaves differently for different objects. class CShape { private: int color; public: virtual float area() { }; // a virtual function can be overridden by the children };

class CCircle : public CShape{ public: float area() {return (pi * r * r)}; };
class CRect : public CShape{ public: float area() {return (w*h)}; };

One function behaves differently for different objects. 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<4; I++) cout << shapes[I]->area() << endl;

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.

Setting up standard interfaces

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

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. };

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)

Você também pode gostar