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

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

  • Multilevel inheritance
  • Multiple Inheritance
  • Hierarchal 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: –
multilevel inheritance

In the above example, a class student is inheriting from the class person and class gradstudent is inheriting from the class student. Thus getting characteristics from both classes.

Multiple Inheritance: –

Another form of inheritance in multiple inheritance in which a class inherits from more than one classes. Thus inheriting characteristics from all classes.
Its structure is like:


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: –
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 with 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;
     }

Now compiler know which function needs to be called.

Hierarchical Inheritance: –

In Hierarchical Inheritance one or more classes are inherited from a single class.


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
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: –
hierarchical inheritance

Leave a Reply

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