Table of contents
1.
What is Hybrid Inheritance in C++?
2.
Syntax of Hybrid Inheritance in C++
3.
Example of Hybrid Inheritance in C++
3.1.
Combination of Single Inheritance and Multilevel Inheritance
3.2.
Combination of Single Inheritance and Multiple Inheritance
4.
Frequently Asked Questions
4.1.
Why is hybrid inheritance also called multipath inheritance?
4.2.
What is the difference between hybrid inheritance and hierarchical inheritance?
4.3.
What are the advantages of hybrid inheritance?
4.4.
How many types of inheritance should be used for hybrid?
5.
Conclusion
Last Updated: Dec 30, 2024
Easy

What is Hybrid Inheritance C++?

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
What is Hybrid Inheritance C++

Inheritance is the ability to inherit the features and functionalities from another class. It supports the reuse of the same code. Inheritance is the most important feature of object-oriented programming. A class that inherits the member functions and functionality from another class is called the derived class, and the class from which the derived class acquires is called the parent class. 

 

What is Hybrid Inheritance in C++?

Hybrid inheritance is the process of combining more than one type of Inheritance while deriving subclasses in a program. The usual case in multiple inheritance is when two classes are derived from one parent class which is also a derived class and not a base class.

Hybrid inheritance is also known as multipath inheritance as the derived class can inherit properties of the base class in different paths.

Syntax of Hybrid Inheritance in C++

The typical syntax of hybrid inheritance (single+multiple inheritance)

Class A
{
	//Data members and member functions of Class A
};
Class B: access modifier A
{
	//Data members and member functions of Class B
};
Class C
{
	//Data members and member functions of Class C
};
Class D: access modifier B, access modifier C
{
	//Data members and member functions of Class D
};

Example of Hybrid Inheritance in C++

Combination of Single Inheritance and Multilevel Inheritance

It is a combination of single and multilevel inheritance. In the below diagram Class A, Class B, Class C show Multilevel inheritance, and Class B, Class D show Single Inheritance.

#include <bits/stdc++.h>  
using namespace std;  

class vehicle  
{  
    public:  
        vehicle(){  
            cout<< "This is a vehicle"<<endl;  
        }  
};  
class Car: public vehicle  
{  
    public:  
        Car(){  
            cout<< "This is a 4-wheeler "<<endl;  
        }  
};  
class Sports  
{  
    public:  
        Sports(){  
            cout<< "It is of Sports category"<<endl;  
        }  
};  
class Bmw: public Car, public Sports  
{  
    public:  
        Bmw(){  
                cout<< "BMW is a Sports Car\n";  
            }  
};  
int main() {  
    Bmw obj1;  
} 

Output

This is a vehicle
This is a 4-wheeler 
It is of Sports category
BMW is a Sports Car

Combination of Single Inheritance and Multiple Inheritance

It is a combination of single and multiple inheritance.In the below diagram Class B, Class C, Class D show Multiple inheritance, and Class A, Class B show Single Inheritance.

#include <bits/stdc++.h>
using namespace std;

class A
{
    public:
    int a;
};
class B : public A // single inheritance
{
    public:
        B(){      
       		a = 36; //initialise a
        }
};
class C
{
    public:
        int c;
        C(){
            c = 14; //initialise c
        }
};
class D : public B, public C   //Multiple inheritance
{
    public:
        void sum(){
            cout << "Sum = " << a + c;
        }
};

int main()
{
    D obj1;  
    obj1.sum();
}        

 

Output

Sum = 50

 

Check out Hybrid Inheritance in Java here.

Frequently Asked Questions

Why is hybrid inheritance also called multipath inheritance?

Hybrid inheritance is also referred to as multipath inheritance because it involves the inheritance of attributes and behaviors from multiple parent classes, leading to multiple paths through which a subclass can inherit properties. This results in a complex inheritance hierarchy with multiple inheritance paths.

What is the difference between hybrid inheritance and hierarchical inheritance?

In hierarchical Inheritance, there is only one base class, and it can have many derived classes, but hybrid Inheritance is a combination of more than one inheritance, it can be a combination of single and multiple inheritance or multilevel and multiple inheritance, etc.

What are the advantages of hybrid inheritance?

Hybrid inheritance combines the benefits of multiple inheritance and hierarchical inheritance, offering advantages such as code reusability, flexibility, and the ability to model complex relationships.

How many types of inheritance should be used for hybrid?

For hybrid inheritance, typically, two types of inheritance are utilized: multiple inheritance and hierarchical inheritance. This combination allows for the creation of diverse and robust class structures, incorporating features from both inheritance types.

Conclusion

In this blog, we discussed what hybrid inheritance is and how to define it, then we discussed the different types of hybrid inheritance that are possible along with examples.

Live masterclass