Dremendo Tag Line

Types of Inheritance in C++ Programming

OOPs in C++

In this lesson, we will understand how many types of Inheritance are their in C++ and how to use them in class object programming.

Types of Inheritance in C++

There are four types of inheritance available in C++, and they are:

  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
video-poster

Single Inheritance

C++ single inheritance

When a class inherits another class, this type of inheritance is called Single Inheritance. It is the most simple form of inheritance in C++.

The above image shows an example of single inheritance in which class B inherits class A. Thus class A can be called a base class or parent class, and class B can be called a child class or subclass or derived class.

Example of Single Inheritance

#include <iostream>

using namespace std;

class ClassA
{
  public:
    void display()
    {
        cout<<"In Class A"<<endl;
    }
};

class ClassB : public ClassA
{
  public:
    void display()
    {
        ClassA::display();
        cout<<"In Class B"<<endl;
    }
};

int main()
{
    ClassB x;
    x.display();

    return 0;
}

Output

In Class A
In Class B

Multiple Inheritance

C++ multiple inheritance

When a class is derived by inheriting two or more classes, this type of inheritance is called Multiple Inheritance.

In the above image, class C has been derived by inheriting class A and class B.

Example of Multiple Inheritance

#include <iostream>

using namespace std;

class ClassA
{
  public:
    void display()
    {
        cout<<"In Class A"<<endl;
    }
};

class ClassB
{
  public:
    void display()
    {
        cout<<"In Class B"<<endl;
    }
};

class ClassC : public ClassA, public ClassB
{
  public:
    void display()
    {
        ClassA::display();
        ClassB::display();
        cout<<"In Class C"<<endl;
    }
};

int main()
{
    ClassC x;
    x.display();

    return 0;
}

Output

In Class A
In Class B
In Class C

Multilevel Inheritance

C++ multilevel inheritance

When a class is created by inheriting another derived class which is previously derived using another class, this type of inheritance is called Multilevel Inheritance.

In the above example, class C has been derived from class B, which is derived from class A.

Example of Multilevel Inheritance

#include <iostream>

using namespace std;

class ClassA
{
  public:
    void display()
    {
        cout<<"In Class A"<<endl;
    }
};

class ClassB : public ClassA
{
  public:
    void display()
    {
        ClassA::display();
        cout<<"In Class B"<<endl;
    }
};

class ClassC : public ClassB
{
  public:
    void display()
    {
        ClassB::display();
        cout<<"In Class C"<<endl;
    }
};

int main()
{
    ClassC x;
    x.display();

    return 0;
}

Output

In Class A
In Class B
In Class C

Hierarchical Inheritance

C++ hierarchical inheritance

When two or more classes inherit the same base class, this type of inheritance is called Hierarchical Inheritance.

In the above image, classes B, C and D inherit the same base class A.

Example of Hierarchical Inheritance

#include <iostream>

using namespace std;

class ClassA
{
  public:
    void display()
    {
        cout<<"In Class A"<<endl;
    }
};

class ClassB : public ClassA
{
  public:
    void display()
    {
        ClassA::display();
        cout<<"In Class B"<<endl;
    }
};

class ClassC : public ClassA
{
  public:
    void display()
    {
        ClassA::display();
        cout<<"In Class C"<<endl;
    }
};

class ClassD : public ClassA
{
  public:
    void display()
    {
        ClassA::display();
        cout<<"In Class D"<<endl;
    }
};

int main()
{
    ClassB x;
    ClassC y;
    ClassD z;
    x.display();
    y.display();
    z.display();

    return 0;
}

Output

In Class A
In Class B
In Class A
In Class C
In Class A
In Class D