C++ Inheritance Types

The level of inheritance is the length of path from the top base class to the bottom derived class. A base class for any derived class could also be derived from other classes.

Types of inheritance

It is based of level of inheritance and interrelation among the classes involved in the inheritance process are:

  1. Single
  2. Hybrid
  3. Hierarchical
  4. Multiple
  5. Multilevel
  6. Multipath

 

1. Single Inheritance

In Single inheritance, class is derived from only one base class.

Syntax:

class base { 
    //… 
 }; 
class derived: [access_to_base] base { 
    //….
}

single-inheritanceClick here to have a look at the more realistic program.

 

2. Multiple Inheritance

A class is derived from two or more base class.
Syntax:

class derived:[access_to_base1] base1, [access_to_base2] base2
{
    //member of derived class
}

Example:

class B1
{
    //…
};
class B2
{
    //…
};
class D: public B1, protected B2
{
    //..
};

multiple-inherimultiple-ex

 

 

 

 

 

 

 

 

 

Click here to have a look at the more realistic program.

 

3. Multilevel inheritance

  • The derivation of a class from another derived class is called multilevel inheritance.
  • A new class that is derived from the base class serves as a base for further inheritance.
  • In case of second derivation, the members inherited as protected or public form first inheritance can be visible from the second derived class.

multilevel inheritanceClick here to have a look at the more realistic program.

 

4. Hierarchical inheritance

  • Classes are derived from single base class.
  • Eg. Animal is the base class from which dog, cat, cow can be derived and the new class can be base for other new classes.
  • Syntax:
class derived: [access_to_base] base { 
    //… 
 }; 
class derived2:[access_to_base] base { 
    //… 
 }; 
.
. 
class derivedn:[ access_to_base] base {
    //… 
 }

Example:

class employee { 
    //… 
 }; 
class manager: public employee { 
    //… 
 }; 
class secretary:public employee { 
    //… 
 }; 
class teacher: public employee {
    //… 
};

Hierarchical inheritance

Figure: Hierarchical inheritance

 

5. Hybrid inheritance

This is mixture of more than one form of inheritance.

hybrid inheritance

Here, Student class is singly derived from class Person. class result is multiply inherited from class student and class exam. The combination of two inheritances form hybrid inheirtance.

 

We've presented multiple inheritance in separate topic after this.

C++ Overriding
C++ Multipath inheritance