Chapter 10 Inheritance and Polymorphism C/C++ Language Programming Wanxiang Che
Array of Objects // Initialize sum double sum = 0; // Add areas to sum Circle circles[2] = {Circle(3), Circle(4)}; // Initialize sum double sum = 0; // Add areas to sum for (int i = 0; i < 2; i++) sum+=circles[i].get_area();
How to sum different Shape? Circle circle(3); Rectangle rect(4, 5); double sum = 0; sum += circle.get_area(); sum += rect.get_area();
How to sum different Shape? Shape shapes[2] = {Circle(3), Rectangle(4, 5)}; // Initialize sum double sum = 0; // Add areas to sum for (int i = 0; i < 2; i++) sum += shapes[i].get_area();
Inheritance Concept Derive a new class (subclass) from an existing class (base class or superclass). Inheritance creates a hierarchy of related classes (types) which share code and interface.
Shape class hierarchy Shape inherits (isa) Rectangle • • • • Circle Square Triangle Circle • • • • inherits (isa)
Inheritance UML
Define a Class Hierarchy Syntax: class DerivedClassName : access-level BaseClassName access-level specifies the type of derivation private by default, or public E.g. class Circle: public Shape Any class can serve as a base class Thus a derived class can also be a base class
Redefining Functions A derived class can override methods defined in its parent class. With overriding, the method in the subclass has the identical signature to the method in the base class. a subclass implements its own version of a base class method. class A { protected: int x, y; public: void print () {cout<<“From A”<<endl;} } class B : public A { public: void print () {cout<<“From B”<<endl;} }
redefining vs. overloading Overloading a function A way to provide more than one function with the same name but with different signatures to distinguish them. Redefining a function The function must be defined in the derived class using the same signature and same return type as in its base class
Static Binding Classes X print() inherits (isa) Y print() Z print() X x; Y y; Z z; X *px; px = & ??; // can be x,y,or z px->print(); // ?? print() inherits (isa) Y print() Z print()
Two Types of Binding Static Binding (the default in C++) px->print() uses X’s print this is known at compile time Dynamic Binding px->print() uses the print() in the object pointed at this is only known at run time with virtual functions
Why “only known at run time”? Assume dynamic binding is being used: X x; Y y; Z z; X *px; cin >> val; if (val == 1) px = &x; else px = &y; px->print(); // which print() is used?
Define Virtual Functions To enable dynamic binding for a function, need to do two things: The function must be declared virtual in the base class. The variable that references the object for the function must contain the address of the object.
Virtual Function class Shape { public: Shape(); Shape(const Shape& orig); virtual ~Shape(); void print(); virtual double get_area(); };
Solution int main() { Shape *shapes[2] = {new Circle(3), new Rectangle(4, 5)}; double sum = 0; for(int i = 0; i < 2; i++){ sum += shapes[i]->get_area(); } cout << sum << endl; return 0;
Note If a function is defined virtual in a base class, it is automatically virtual in all its derived classes. It is not necessary to add the keyword virtual in the function declaration in the derived class.