Home > OOP > Different Types of Inheritance in C++

Different Types of Inheritance in C++

There are different types of inheritance in c++ including:

Multilevel inheritance

Multiple Inheritance

Hierarchical inheritance

Multilevel inheritance: –

Multilevel Inheritance is the one in which a class is derived from a base class. And then another class is inherited from that derived class and so on.
Its structure is like:

class A 
{
     public:
          A() 
          {
               // this is constructor for class A.
          }
};
class B :public A
{
     B() 
     {
          // this is constructor for class B.
     }
};
class C :public B
{
     C () 
     {
          // this is constructor for class C.
     }
};
 

multilevel inheritance
Below example shows us how multilevel inheritance looks like.
Example: –

#include "stdafx.h"
#include "iostream"
#include"string"

using namespace std;

class person
{
     protected:
          string name;
          string address;
     public:
          person()
          {
               name ="";
               address = "";
          }
};

class student : public person
{
     protected:
          string regNo;
          float cgpa;
};
class gradStudent :public student
{
     string department;

     public:
          void getData()
          {
               cout << "Please Enter Name : ";
               getline(cin, name);
               cout << "Please Enter Address : ";
               getline(cin, address);
               cout << "Please Enter Registration Number : ";
               getline(cin, regNo);
               cout << "Please Enter cgpa : ";
               cin >> cgpa;
               cin.ignore();
               cout << "Please Enter Department : " ;
               getline(cin, department);
          }
          void show()
          {
               cout << "\n********Multilevel Inheritance********\n";
               cout << "\n***Student Record*** \n\n";
               cout << "Name : " << name << endl;
               cout << "Address : " << address << endl;
               cout << "Registration Number : " << regNo << endl;
               cout << "CGPA : " << cgpa << endl;
               cout << "Department : " << department << endl;
          }
};
void main()
{
     gradStudent s;
     s.getData();
     s.show();
}

Output: –
different types of inheritance in c++ : multilevel inheritance

In the above example, a class student is inheriting from the class person and class gradstudent is inheriting from the class student. In this way, the class gradstudent has the characteristics from both classes. This form of inheritance makes levels. i.e. the class student is inheriting from the person class. This is level one. And then the class gradstudent inherits from the class student.

Note:- It is not necessary to have one class at each level. We can have any number of classes at each level.

Multiple Inheritance: –

Another form of inheritance in multiple inheritance in which a class inherits from more than one classes. Thus inheriting characteristics from all the parent classes. Structure for multiple inheritance is:


class A 
{
     public:
     A() 
     {
          // this is constructor for class A.
     }
};
class B 
{
     B() 
     {
          // this is constructor for class B.
     }
};
class C :public A, public B
{
     C () 
     {
          // this is constructor for class C.
     }
};

Multiple inheritance
Let’s have a look at an example.
Example: –

#include "stdafx.h"
#include "iostream"
#include"string"

using namespace std;

class person
{
     protected:     
          string name;
          string address;
     public:
          person()
          {
               name ="";
               address = "";
          }
};

class student 
{
     protected:
          string regNo;
          float cgpa;
};
class gradStudent :public student, public person
{
     string department;
     
     public:
          void getData()
          {
               cout << "Please Enter Name : ";
               getline(cin, name);
               cout << "Please Enter Address : ";
               getline(cin, address);
               cout << "Please Enter Registration Number : ";
               getline(cin, regNo);
               cout << "Please Enter cgpa : ";
               cin >> cgpa;
               cin.ignore();
               cout << "Please Enter Department : " ;
               getline(cin, department);
          }
          void show()
          {
               cout << "\n********Multiple Inheritance********\n";
               cout << "\n***Student Record*** \n\n";
               cout << "Name : " << name << endl;
               cout << "Address : " << address << endl;
               cout << "Registration Number : " << regNo << endl;
               cout << "CGPA : " << cgpa << endl;
               cout << "Department : " << department << endl;
	}
};
void main()
{
     gradStudent s;
     s.getData();
     s.show();
}

Output: –
different types of inheritance in c++ : Multiple Inheritance

Ambiguity: –

An ambiguity exists in multiple inheritance when overriding a function. i.e. when we have a class which is inheriting from two or more classes and these classes are having some function/s of the same name.
In order to overcome this issue, we use scope resolution operator with a class name when accessing these type of function/s.
E.g.

#include "stdafx.h"
#include "iostream"
#include"string"

using namespace std;

class person
{
     protected:
          string name;
          string address;
     public:
          person()
          {
               name ="";
               address = "";
          }
          void getData()
          {
               cout << "Please Enter Name : ";
               getline(cin, name);
               cout << "Please Enter Address : ";
               getline(cin, address);
          }
          void show()
          {
               cout << "\n********Multiple Inheritance********\n";
               cout << "\n***Student Record*** \n\n";
               cout << "Name : " << name << endl;
               cout << "Address : " << address << endl;
          }
};

class student 
{
     protected:
          string regNo;
          float cgpa;
     public:
          void getData()
          {
               cout << "Please Enter Registration Number : ";
               getline(cin, regNo);
               cout << "Please Enter cgpa : ";
               cin >> cgpa;
               cin.ignore();
          }
          void show()
          {
               cout << "Registration Number : " << regNo << endl;
               cout << "CGPA : " << cgpa << endl;
          }
};
class gradStudent :public student, public person
{
     string department;

     public:     
          void getData()
          {
               // this will create ambiguity to call getData function of which parent class.
               getData();
               cout << "Please Enter Department : " ;
               getline(cin, department);
          }
          void show()
          { 
               // this will create ambiguity to call show function of which parent class.
               show();
               cout << "Department : " << department << endl;
          }
};
void main()
{
     gradStudent s;
     s.getData();
     s.show();
}

To solve this ambiguity in above code we just need to change these in above code.

void getData()
     {
          person::getData();
          student::getData();
          cout << "Please Enter Department : " ;
          getline(cin, department);
     }
     void show()
     {
          person::show(); 
          student::show();
          cout << "Department : " << department << endl;
     }

As we explicitly specified or told the compiler about each function. Now compiler knows which function needs to be called.

Hierarchical Inheritance: –

In Hierarchical Inheritance, one or more classes are inherited from a single class. In this way, the classes make the Hierarchical structure of inheritance.


class A 
{
     public:
     A() 
     {
          // this is constructor for class A.
     }
};
class B: public A
{
     B() 
     {
          // this is constructor for class B.
     }
};
class C: public A
{
     C () 
     {
          // this is constructor for class C.
     }
};

hierarchical inheritance

As in the figure above class B and class C are inheriting from the same class A. Thus forming the Hierarchical structure. Below example demonstrates its working.
Example: –

#include "stdafx.h"
#include "iostream"
#include"string"

using namespace std;

class person
{
     protected:
          string name;
          string address;
     public:
          person()
          {
               name ="";
               address = "";
          }
          void getData()
          {
               cout << "Please Enter Name : ";
               getline(cin, name);
               cout << "Please Enter Address : ";
               getline(cin, address);
          }
          void show()
          {
               cout << "Name : " << name << endl;
               cout << "Address : " << address << endl;
          }
};

class student : public person
{
     protected:
          string regNo;
          float cgpa;
     public:
          void getData()
          {
               person::getData();
               cout << "Please Enter Registration Number : ";
               getline(cin, regNo);
               cout << "Please Enter cgpa : ";
               cin >> cgpa;
               cin.ignore();
          }
          void show()
          {
               person::show();
               cout << "Registration Number : " << regNo << endl;
               cout << "CGPA : " << cgpa << endl;
          }
};
class Teacher :public person
{
     string designation;

     public:
          void getData()
          {
               person::getData();
               cout << "Please Enter Department : " ;
               getline(cin, designation);
          }
          void show()
          {
               person::show(); 
               cout << "Designation : " << designation << endl;
          }
};
void main()
{
     student s;
     s.getData();
     cout << "\n***Student Record*** \n\n";
     s.show();
     cout << endl;
     Teacher t;
     t.getData();
     cout << "\n***teacher Record*** \n\n";
     t.show();
}

Output: –
different types of inheritance in c++ : hierarchical inheritance

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.