Inheritance and Polymorphism by Yukong Zhang Object-oriented programming languages are the most widely used modern programming languages. They model programming based on objects which are very close to things found in the real world. This program paradigm provides three important mechanisms: Encapsulation, Inheritance, and Polymorphism. 1 Encapsulation Encapsulation hides the members of a class from the users. One of the advantages of this information hiding is to minimize the impact on the users code whenever the modifications on the class have to be made. 2 Inheritance 2.1 Definition A mechanism by which one class acquires the properties both data and functions of another class. The following names are used to describe inheritance. Base class Parent class Super class Derived class Child class Subclass 2.2 Member variables Child class inherits all member variables and member functions of its parent class/classes plus its own members. Therefore, an object of child class requires more memory space than an object of its parent class. If a member variable of a parent class is defined as private (it is true in most cases), its child classes cannot directly access the member, even though the child classes possess the asset. They have to use the public functions defined in the parent class to access or manipulate parent s member variable. Sometimes, protected is used for the member variables in a parent class to allow child classes to access them. 2.3 Functions Both parent and child classes can define two functions with the exactly same names and parameter list (or prototypes). In this case, we say the function defined in the child class overrides the function defined in the parent class.
2.4 Constructors When a child object is being created, one of the constructors in its parent class will be called first through an initializer in its child constructors, then followed by the child constructor. For example: class P P() cout << "Default constructor P" << endl; P(int n) cout << "Parameterized constructor P" << endl; ~P() cout << "Destructor P" << endl; class C:public P C(int m):p(m) cout << "Parameterized constructor C" << endl; ~C() cout << "Destructor C" << endl; int main() C o(24); return 0; The output of the program would be: Parameterized constructor P Parameterized constructor C Destructor C Destructor P If a child class has a number of member variables that are of types of other classes, the constructor of its parent class will be called first, then followed by the constructors of the member classes in the order the member variables appear in the child class. The order of initializers doesn t matter. However, which constructors will be called depends on how the initializers are specified. If no initializers are specified for some members, the default constructors for the members will be called. For example, Page 2
class P; class A; class B; class C; class X:public P X(const A & a, const B & b ): cc( ), aa(a) bb = b; cout << "X constructor" << endl; ~X() cout << "X destructor" << endl; private: A aa; B bb; C cc; In the example above, class P is a parent class of class X. Classes A, B, and C are the types for member variables aa, bb, cc. When an object of class X is being created, its parent class P s default constructor will be called first, then A s copy constructor, B s default constructor, C s default constructor, finally the body of X s constructor. The order of the initializers doesn t matter at all, but the visible initializers determine which constructors should be called. 3 Polymorphism 3.1 Definition Polymorphism means multiple forms. In programming languages, it means a variable may have a different type in a certain context. 3.2 Usage To use polymorphism, the parent type variable must be declared as either pointer or reference. Also, virtual keyword must be used in the member function of the parent class. The member function is also overridden by child classes. For example, class Person Page 3
virtual void print(); class Student void print(); int main() Person * p; Person * ps = new Person(); Student * st = new Student(); p = ps; p->print(); p = st; p->print(); //Person s print() will be called. //Student s print() will be called. delete ps; delete st; return 0; 3.3 Virtual Destructor Virtual destructor is used to trigger a call to its parent s destructor when a child object is being destroyed. Without the keywork virtual in parent destructor, only the child destructor will be called, which is not desired. 4 Abstract Class 4.1 Definition A class is said to be an abstract class if it has one or more pure virtual functions. A pure virtual function is one that doen t have a function body and must be overridden by child class. It has the syntax =0 in the member function declaration. An abstract class is intended to be inherited by child classes only. It cannot be used to create concrete objects. If a class has pure member functions exclusively, without any data member variables, the class is called pure abstract class. Pure abstract class in C++ is Page 4
similar to interface in Java programming language. It is a way to enforce all child class that inherit from the same parent class to implement the same set of member functions declared in its parent class. That is, all child classes that inherit from the same parent have the same behaviors. For example below, all concrete shapes such as rectangles, triangles, circle, etc should have the same behavior that calculates their areas, even though the formulas for calculating areas are different. #include <iostream> using namespace std; //Abstract class class Shape Shape() //Pure virtual function virtual double calculatearea() = 0; //Concrete class class Rectangle : public Shape Rectangle(double h, double w) height = h; width = w; double calculatearea() return height * width; private: double height; double width; int main() Shape * s; Rectangle * r = new Rectangle(3, 4); s = r; cout << "The area is " << r->calculatearea() << endl; cout << "The area is " << s->calculatearea() << endl; return 0; //Output: //The area is 12 Page 5
//The area is 12 //Press any key to continue... Page 6