The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as
well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. Donald E. Knuth

Inheritance

Inheritance is an important concept in C++ which allows one class to derive the features of another class. This supports code reusability and save implementation time. The existing class is known as base class and the class which inherits the data members and member functions of the base class is known as derived class. The use of inheritance reflects is-a relationship between two entities (classes). Suppose we have an existing class fruit and we want to create a new class apple. Now, apple is a fruit. So, apple class can inherit from fruit class.

Modes of Inheritance
Based of access specifiers, derived class can inherit from base class in either public, private or protected visibility mode. The default mode is private.
In public mode of inheritance, public members of base class become public members in derived class and protected members of base class become protected members in derived class.
In protected mode of inheritance, the public and protected members of the base class become protected members in the derived class.
In private mode of inheritance, the public and protected members of the base class become private members in the derived class.
Please note that, private members can never be inherited in any mode. Also, the constructors, destructors and friend functions of the base class are not inherited.

Forms of Inheritance
One class can inherit from multiple base classes and multiple derived classes can inherit from one base class. Based on this, there are different types of inheritance which are described below.

Single Inheritance
It is the simplest form of inheritance involving one base class and one derived class. An important situation to be considered is when the base class and derived class define a function with the same name and same argument list. In this case, derived class function overrides the base class function. Following program illustrates single inheritance :

#include<iostream>
using namespace std;

class X {
   int a;
public :
   X() { }
   int b;
   void setVal() {
      cout << "Inside class X" << endl;
      a = 5;
      b = 2;
   }
   void display() {
      cout << "a : " << a << "  b : " << b << endl;
   }
};

/* X is inherited by Y in public mode
 * Data member of X visible in Y is 'b;
 * Member function of X visible in Y is setval()
 * display() function of 'X' is overridden by display() of 'Y'
 */
class Y : public X {
   int c;
public :
   Y() { }
   void setVariable() {
      cout << "Inside class Y" << endl;
      c = 7;
   }
   void display() {
      // cout << "a : " << a; // error since 'a' is not inherited
      cout << "b : " << b << "  c : " << c << endl;
   }
};

int main() {
   Y obj; // declare an object of class 'Y'
   obj.setVal(); // base class function is called with object of derived class
   obj.setVariable();
   obj.display(); // derived class function is called
   return 0;
}


Multilevel Inheritance
Consider a base class A and another class B which inherits from A. Now another class C inherits from B. So, B becomes the base class for C and derived class for A. This kind of inheritance is known as multilevel inheritance. See the program below :

#include<iostream>
using namespace std;

class X {
   int a;
public:
   int b;
   void setX() {
      cout << "Inside class X" << endl;
      a = 5;
      b = 7;
   }
};

/*
 * Data members of Y are 'b', 'c' and 'd'
 * Member Functions of Y are setX() and setY();
 */
class Y : public X {
   int c;
public :
   int d;
   void setY() {
      cout << "Inside class Y" << endl;
      c = 13;
      d = 18;
   }
};

/*
 * Data members of Z are 'b', 'd', 'e' and 'f'
 * Member Functions of Y are setX(), setY(), setZ();
 */
class Z : public Y {
   int e;
public :
   int f;
   void setZ() {
      cout << "Inside class Z" << endl;
      e = 29;
      f = 51;
   }
   void display() {
      cout << "Own members : " << " e = " << e << " f = " << f << endl;
      cout << "Inherited : " << " b = " << b << " d = " << b << endl;
   }
};

int main() {
   Z obj;
   obj.setX();
   obj.setY();
   obj.setZ();
   obj.display();
   return 0;
}


Multiple Inheritance
When one class derives from multiple base classes, this is known as multiple inheritance. In this case, it is possible that two base classes defines a function with same name and same argument list. That function becomes ambiguous in derived class and the ambiguity is resolved by using classname and scope resolution operator ( :: ) while calling that function as shown in the program below :

#include<iostream>
using namespace std;

class X {
   int a;
public :
   int b;
   void setX() {
      a = 5;
      b = 7;
   }
   void display() {
      cout << "a : " << a << "  b : " << b << endl;
   }
};

class Y {
   int c;
public :
   int d;
   void setY() {
      c = 15;
      d = 17;
   }
   void display() {
      cout << "c : " << c << "  d : " << d << endl;
   }
};

/* class 'Z' inherits from classes 'X' and 'Y'
 * Data members of Z are 'b', 'c', 'd'
 * Member functions of Z are setX() and setY()
 * display() function is ambiguous
 */
class Z : public X, public Y {
public :
   void display() {
      X :: display(); // calling display() of class 'X'
      Y :: display(); // calling display() of class 'Y'
   }
};

int main() {
   Z obj;
   obj.setX();
   obj.setY();
   obj.display();
   return 0;
}


Hierarchical Inheritance
In this form of inheritance multiple classes inherits from a single class i.e there is one base class and multiple derived classes. This form of inheritance is commonly used in C++ program design. Following program illustrates the concept :

#include<iostream>
using namespace std;

class fruit {
public :
   void fruitInfo() {
      cout << "I am a fruit. ";
   }
};

// class 'mango' inherits from class 'fruit'
class mango : public fruit {
public :
   void mangoInfo() {
      fruitInfo(); // calling base class function
      cout << "My name is mango. " << endl;
   }
};

// class 'apple' inherits from class 'fruit'
class apple : public fruit {
public :
   void appleInfo() {
      fruitInfo();// calling base class function
      cout << "My name is apple. " << endl;
   }
};

int main() {
   mango m; // declare an object of type 'mango'
   apple a; // declare an object of type 'apple'
   m.mangoInfo();
   a.appleInfo();
   return 0;
}


Hybrid Inheritance
When two or more forms of inheritance are used to design a C++ program, it is known as hybrid inheritance. Suppose classes B and C inherits from base class A ( hierarchical inheritance ). Then class D inherits from classes B and C ( multiple inheritance ). The problem here is that there will be two copies of the members of class A in class D. This leads to ambiguity. To resolve this, we make the common base class A as virtual so that compiler takes necessary care to see that only one copy of members of class A is present in class D. Following program illustrates the use of virtual base class :

#include<iostream>
using namespace std;

class fruit {
public :
   void fruitInfo() {
      cout << "I am a fruit. ";
   }
};

// class 'mango' inherits from class 'fruit'
class mango : virtual public fruit {
public :
   void mangoInfo() {
      fruitInfo(); // calling base class function
      cout << "My name is mango. " << endl;
   }
};

// class 'apple' inherits from class 'fruit'
class apple : virtual public fruit {
public :
   void appleInfo() {
      fruitInfo();// calling base class function
      cout << "My name is apple. " << endl;
   }
};

class mixed_fruit : public mango, public apple {
public :
   void mixed_fruitInfo() {
      appleInfo(); // calling function of class 'apple'
      mangoInfo(); // calling function of class 'mango'
      cout << "Mixed Fruit " << endl;
   }
};

int main() {
   mixed_fruit mf;
   mf.mixed_fruitInfo();
   return 0;
}


Constructors in Derived class
If the base class has a parameterized constructor, then it is the responsibility of the derived class constructor to call the base class constructor with necessary parameters. In case of multiple inheritance, constructors of base classes should be called in the order they were inherited during definition of the base class. However, the constructor of virtual base class is called first regardless of the order. See the program below :

#include<iostream>
using namespace std;

class X {
public:
   int a;
   X(int val) {
      cout << "Constructor of class X invoked" << endl;
      a = val;
   }
};

class Y {
public:
   int b;
   Y(int val) {
      cout << "Constructor of class Y invoked" << endl;
      b = val;
   }
};

/* class 'X' is inherited first, the class 'Y' */
class Z : public X, public Y {
public :
   Z(int m, int n) : X(m), Y(n) {
      cout << "Constructor of class Z invoked" << endl;
   }
};

int main() {
   Z obj(7, 9);
   cout << "a : " << obj.a << "  b : " << obj.b << endl;
   return 0;
}


Abstract classes
An abstract class is the one which is used only as a base class and not for creating objects. It is design concept used in C++ programming language. It provides an interface over classes providing similar functionalities.

Back | Next