Types of Inheritance in C++

Following are the types of Inheritance.

1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance

1. Single Inheritance

  • In Single Inheritance, one class is derived from another class.
  • It represents a form of inheritance where there is only one base and derived class.
single inheritance

Example: Program demonstrating Single Inheritance

#include<iostream>
using namespace std;
class Student
{
        public:
        int rollno;
        string sname;
        
        void setdata()
        {
                rollno = 1;
                sname = "ABC";
        }
};
class Marks : public Student
{
   public:
        int m1, m2, m3, total;
        float per;
        void setmarks()
        {
                setdata();
                m1 = 60;
                m2 = 65;
                m3 = 70;
                total = m1 + m2 + m3;
                per = total / 3;
        }
        void show()
        {
                setmarks();
                cout<<"Student Information"<<endl;
                cout<<"Student Roll No :  "<<rollno<<endl;
                cout<<"Student Name :  "<<sname<<endl;
                cout<<"-----------------------"<<endl;
                cout<<"Student Marks"<<endl;
                cout<<"Marks1 :  "<<m1<<endl;
                cout<<"Marks2 :  "<<m2<<endl;
                cout<<"Marks3 :  "<<m3<<endl;
                cout<<"Total :  "<<total<<endl;
                cout<<"Percentage :  "<<per<<endl;
        }
};
int main()
{
        Marks mar;
        mar.setmarks();
        mar.show();
        return 0;
}


Output:
Student Information
Student Roll No : 1
Student Name : ABC
-----------------------
Student Marks
Marks1 : 60
Marks2 : 65
Marks3 : 70
Total : 195
Percentage : 65

In the above example, the base class Student has two public member variables namely rollno and sname. These members are kept public, so that they can accessible from the derived class function to display marks, total and percentage.

The class Marks is derived from the class Student:
class Marks : public Student

This concept of gaining access to the members of the base class is called as code reusability.

The main() function has a first creation of the object of the class Marks. The object mar is created of class Marks.

2. Multiple Inheritance

  • In Multiple Inheritance, one class is derived from multiple classes.

  • multiple inheritance

  • In the above figure, Class D is derived from class A, Class B and Class C.
  • We need to use following syntax for deriving a class from multiple classes.
class derived_class_name : access_specifier base_classname1, access_specifier base_classname2, access_specifier base_classname3

Example: Program demonstrating Multiple Inheritance

#include<iostream>
using namespace std;
class A
{
  public:
     A()  
     {
          cout << "Class A" << endl;
     }
};
class B
{
   public:
      B()  
      {
           cout << "Class B" << endl;
      }
};
class C
{
   public:
      C()
      {
           cout<<"Class C"<<endl;
      }
};
class D: public C, public B, public A  // Note the order. Class C, Class B and then Class A
{
   public:
      D()  
      {
           cout << "Class D" << endl;
      }
};
int main()
{
     D d;
     return 0;
}


Output:
Class C
Class B
Class A
Class D

In the above example, Class C is called before Class B and Class A. Class D is a derived class from Class C, Class B and Class A. The constructors of inherited classes are called in the same order in which they are inherited.

In this case, derived class allows to access members of both the base classes from which it is inherited. Class D is derived from multiple classes.

3. Multilevel Inheritance

  • In Multilevel inheritance, one class is derived from a class which is also derived from another class.
  • It represents a type of inheritance when a derived class is a base class for another class.

multi level

Example: Program demonstrating Multilevel Inheritance

#include <iostream>
using namespace std;

class A
{
    public:
      void show()
      {
          cout<<"Class A - Base Class";
      }
};
class B : public A
{

};
class C : public B
{

};
int main()
{
    C c;
    c.show();
    return 0;
}


Output:
Class A - Base Class

In the above program, Class B is derived from Class A and Class C is derived from Class B. Object c is created of Class C in main() function. When the show() function is called, Class A is executed show() because there is no show() function Class C and Class B. At the time of execution the program first looks for show() function in Class C, but cannot find it. Then looks in Class B because Class C is derived from Class B and again cannot find it. Finally looks for show() function in Class A and executes it and displays the output.

If the show() function is declared in Class C too then it would have override show() function in Class A because of member function overriding.

4. Hierarchical Inheritance

  • In Hierarchical Inheritance, there are multiple classes derived from one class.
  • In this case, multiple derived classes allow to access the members of one base class.
hierarchical inheritance

Syntax:

class base_class
{
   . . .
};
class first_derived_class : access_specifier base_class
{
   . . .
};
class second_derived_class : access_specifier base_class
{
   . . .
};
class third_derived_class : access_specifier base_class
{
   . . .
};


hierarchical inheritance example

Example: Program demonstrating Hierarchical Inheritance

#include <iostream>
using namespace std;
class Shape
{
    public:
      Shape()
      {
          cout<<"Base Class - Shape"<<endl;
      }
};
class Rectangle : public Shape
{
        public:
        Rectangle()
        {
                 cout<<"Derived Class - Rectangle"<<endl;
        }
};
class Triangle : public Shape
{
        public:
        Triangle()
        {
                 cout<<"Derived Class - Triangle"<<endl;
         }
};
class Circle : public Shape
{
        public:
        Circle()
        {
                 cout<<"Derived Class - Circle"<<endl;
        }
};
int main()
{
        Rectangle r;
        cout<<"---------------------------"<<endl;
        Triangle t;
        cout<<"---------------------------"<<endl;
        Circle c;
        return 0;
}


Output:
Base Class - Shape
Derived Class - Rectangle
---------------------------
Base Class - Shape
Derived Class - Triangle
---------------------------
Base Class - Shape
Derived Class - Circle

5. Hybrid Inheritance

  • Hybrid inheritance is also known as Virtual Inheritance.
  • It is a combination of two or more inheritance.
hybrid inheritance
  • In hybrid inheritance, when derived class have multiple paths to a base class, a diamond problem occurs. It will result in duplicate inherited members of the base class.
  • To avoid this problem easily, use Virtual Inheritance. In this case, derived classes should inherit base class by using Virtual Inheritance.

Example: Program demonstrating Hybrid Inheritance

#include<iostream>
using namespace std;
class Student
{
    protected:
          int rollno;
    public:
          void get_rollno(int r)
          {
                   rollno=r;
           }
          void show_rollno(void)
          {
                  cout<<"Student Result"<<endl;
                  cout<<"----------------------------"<<endl;
                  cout<<"Roll no : "<<rollno<<"\n";
          }
};
class Test:public virtual Student
{
     protected:
            float mark1, mark2;
     public:
            void get_marks(float m1,float m2)
            {
                      mark1 = m1;
                      mark2 = m2;
            }
            void show_marks()
            {
                      cout<<"Marks obtained:"<<endl;
                      cout<<"Mark1 = "<<mark1<<"\n"<<"Mark2 = "<<mark2<<"\n";
            }
};
class Sports : public virtual Student
{
      protected:
              float score;
      public:
              void get_score(float s)
              {
                        score = s;
              }
              void show_score(void)
              {
                        cout<<"Sport Scores = "<<score<<"\n";
              }
};
class Result: public Test, public Sports
{
                float total;
       public:
                void show(void);
};
void Result :: show(void)
{
        total = mark1 + mark2 + score;
        show_rollno();
        show_marks();
        show_score();
        cout<<"----------------------------"<<endl;
        cout<<"Total Score = "<<total<<"\n";
}
int main()
{
        Result re;
        re.get_rollno(101);
        re.get_marks(60,65);
        re.get_score(6.0);
        re.show();
        return 0;
}


Output:
Student Result
----------------------------
Roll no : 101
Marks obtained:
Mark1 = 60
Mark2 = 65
Sport Scores = 6
----------------------------
Total Score = 131

In the above program, base class Student is created and another class called Test and Sports are inherited from the main class. Test class and Sports class inherited by another class called Result to calculate the marks.