Types of Inheritance in C++

C++ supports five types of inheritance:

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

Base class: A base class is a class in an object-oriented programming, from which other classes are derived. Derived class: A Derived class is defined as the class derived from the base class.

1. Single Inheritance

In single inheritance, a class can inherit attributes from only one class. i.e. one derived class inherits from one base class only. This is also the simplest form of Inheritance.

C++ Inheritance
where, ‘A’ – base class,
and ‘B’ – derived class.

Syntax

class subclass_name : access_mode base_class
{
  //body of subclass
};

Example:

#include <iostream>  
using namespace std;  
 class Account {  
   public:  
   float salary = 45000;   
 };  
   class Programmer: public Account {  
   public:  
   float bonus = 6000;    
   };       
int main(void) {  
     Programmer p1;  
     cout<<"Salary: "<<p1.salary<<endl;    
     cout<<"Bonus: "<<p1.bonus<<endl;    
    return 0;  
} 

Output:

Salary: 45000
Bonus: 6000

2. Multiple Inheritance 

In Multiple inheritance, a single derived class may inherit attributes from two or more than two base classes.

Syntax: 

class subclass_name : visibility base_1, visibility base_2, ....
{
  //body of subclass
};

Example:

#include <iostream>   
using namespace std;  
class A  
{  
    protected:  
     int a;  
    public:  
    void get_a(int n)  
    {  
        a = n;  
    }  
};  
  
class B  
{  
    protected:  
    int b;  
    public:  
    void get_b(int n)  
    {  
        b = n;  
    }  
};  
class C : public A,public B  
{  
   public:  
    void display()  
    {  
        std::cout << "The value of a is : " <<a<< std::endl;  
        std::cout << "The value of b is : " <<b<< std::endl;  
        cout<<"Sum of a and b is : "<<a+b;  
    }  
};  
int main()  
{  
   C c;  
   c.get_a(15);  
   c.get_b(25);  
   c.display();  
  
    return 0;  
}  

Output:

The value of a is : 15
The value of b is : 25
Sum of a and b is : 40

3. Hierarchical Inheritance

In Hierarchical inheritance, from a single base class, more than one sub/derived class is inherited . i.e. more than one derived class inherits attributes from a single base class.

Syntax:

class A 
{  
    // body of the class A.  
}    
class B : public A   
{  
    // body of class B.  
}  
class C : public A  
{  
    // body of class C.  
}   
class D : public A  
{  
    // body of class D.  
}   

Example:

 // C++ program to implement 
// Hierarchical Inheritance
#include <iostream>
using namespace std;
 // base class
class Vehicle
{
  public:
    Vehicle()
    {
      cout << "This is a Vehicle" << endl;
    }
};
  // first sub class
class Car: public Vehicle
{ 
};
 // second sub class
class Bus: public Vehicle
{
     
};
 // main function
int main()
{
    // creating object of sub class will
    // call the constructor of base class
    Car obj1;
    Bus obj2;
    return 0;
}

Output:

This is a Vehicle
This is a Vehicle

4. Multilevel Inheritance:

In Multilevel inheritance, the derived class inherits from a class, which in turn inherits attributes from some other class. The derived class for one, is base class for another. Inheritance is passed on or it is transitive in nature. Hence, the last derived class will get all the members of all its base classes.

Example:

 // C++ program to implement 
// Multilevel Inheritance
#include <iostream>
using namespace std;
 // base class
class Vehicle
{
  public:
    Vehicle()
    {
      cout << "This is a Vehicle" << endl;
    }
};
 // first sub_class derived from class vehicle
class fourWheeler: public Vehicle
{  public:
    fourWheeler()
    {
      cout<<"Objects with 4 wheels are vehicles"<<endl;
    }
};
// a sub class derived from the derived base class fourWheeler
class Car: public fourWheeler{
   public:
     Car()
     {
       cout<<"Car has 4 Wheels"<<endl;
     }
};
 // main function
int main()
{
    //creating object of sub class will
    //invoke the constructor of base classes
    Car obj;
    return 0;
}

Output:

This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels

5. Hybrid (Virtual) Inheritance:

In Hybrid Inheritance more than one type of inheritance is combined and implemented.

For example, by combining Hierarchical and Mutilevel Inheritance, we get a hybrid inheritance.

Example:

// C++ program for Hybrid Inheritance  


// C++ program for Hybrid Inheritance 
#include <iostream>
using namespace std;
 // base class
class Vehicle
{
  public:
    Vehicle()
    {
      cout << "This is a Vehicle" << endl;
    }
};
 //base class
class Fare
{
    public:
    Fare()
    {
        cout<<"Fare of Vehicle\n";
    }
};
 // first sub class
 class Car:
 public Vehicle
{ 

};
 // second sub class
 class Bus: public Vehicle, public Fare
{

};
 // main function
 int main()
{
      // creating object of sub class will
       // invoke the constructor of base class
       Bus obj2;
       return 0;
}

Output:

This is a Vehicle
Fare of Vehicle

We will be happy to hear your thoughts

Leave a reply

Edusera
Logo
Open chat
1
Scan the code
Hello!👋
Can we help you?