Before diving deep into a hybrid inheritance in C++, first, see what inheritance is and its different types.

What Is Inheritance?

The process by which one class objects derive the properties of other classes or classes while maintaining its own is called Inheritance. 

The parent class from which other classes are derived is called a super class or base class, whereas the child classes that are generated out of the base classes are called the derived classes or subclasses.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

In this, the derived classes can have their own methods and properties while maintaining the inherited properties of their parent classes. These added properties are confined to these subclasses only and do not manipulate the parent class. In this way, the derived classes have the properties of both, i.e., itself and the parent classes.

Need for Inheritance

Inheritance is needed in Object-Oriented programming because of the following reasons -

  • To enable reusability, extendability, and modifiability easier in code.
  • Makes an object easy to port and maintain.
  • To make exceptional properties economical and practical.

Types of Inheritances

The various types of Inheritances in C++ are listed below -

  • Single or simple Inheritance - In this type of inheritance, a sub class derives properties from a single super class.
  • Multiple Inheritance - In this type of inheritance, a sub class derives properties from multiple super classes.
  • Multilevel Inheritance - In this type of inheritance, a subclass derives properties from another subclass which in turn derives properties from another sub class or the super class.
  • Hierarchical Inheritance - In this type of inheritance, multiple sub classes derive properties from a single super class.
  • Hybrid Inheritance - In this type of Hybrid inheritance in C++, a combination of many Inheritances. For example - mixing Multilevel Inheritance with Multiple Inheritance, etc.

Hybrid Inheritance in C++

The process of combining more than one type of Inheritance together while deriving subclasses in a program is called a Hybrid Inheritance.

Hybrid in C++ follows the following pattern - Multiple Inheritance, Single Inheritance, and Hierarchical Inheritances are combined together. As stated earlier, in Multiple Inheritance, a sub-class derives properties from multiple superclasses.

Hybrid Inheritance in C++ is also known as multipath inheritance. This is known so due to the fact that a sub class derives or inherits properties of the super class following various paths. Therefore, Hybrid Inheritance is generally applied where we need to apply more than one form of Inheritance.

The following diagram demonstrates the process of Hybrid Inheritance well -

Hybrid_Inheritance_In_Cpp_1.

The above diagram follows the following Hybrid Inheritance -

It is Hybrid Inheritance in C++ which is a combination of Multiple Inheritances and Single Inheritance. 

For multiple Inheritance, Class D is inherited from two other classes. It is derived from both Class B and Class C.

In a similar way, for single Inheritance, Class B has derived from Class A. Therefore, this chain of various paths of Inheritances constitutes the Hybrid Inheritance.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Syntax of Hybrid Inheritance in C++

You will now explore the syntax that we need to follow for Hybrid Inheritance in C++. For the previous example where the Hybrid Inheritance in C++ was a combination of Single and Multiple Inheritances, the following syntax can be followed in C++ -

class A  

{  

// block of statement(s)  

}:  

class B: public A  // class B derived from a single class A -- follows single inheritance

{  

// block of statement(s)   

};  

class C  

{  

// block of statement(s) 

};  

class D: public B, public C                                    

// class D derived from two classes, class B and class C -- follows multiple inheritance

{  

// block of statement(s) 

};  

Now you will  see the syntax for Hybrid Inheritance in C++ where a combination of Multilevel Inheritance and Single Inheritance is considered -

class A   

{  

// block of statement(s)    

};  

class B: public A   // class B derived from class A

{  

// block of statement(s)   

};  

class C: public B   // class C derived from class B

{  

// block of statement(s)   

};  

class D: public B   // class D derived from class B

{  

// block of statement(s)   

};  

Examples of Hybrid Inheritance in C++

You will now discuss some examples of Hybrid Inheritance In C++ to understand Hybrid Inheritance in C++ in an even better way -

Example 1 : Combination of Multiple Inheritance and Single Inheritance

Build upon the previous theoretical example by converting them into a real-life scenario. Class A as Animal Class, Class B as Mammals, Class C as Herbivores, Class D as Cow. Mammals can be derived from Animal class, and Cow is a combination of Herbivores and Mammals. This relationship well defines the combination of Multiple Inheritance and Single Inheritance.

Let us see the code for the same -

Code:

#include <iostream>  

using namespace std;  

class Animals // indicates class A  

{  

public:  

Animals()  

    {  

cout<< "This is an animal\n";  

    }  

};  

class Mammals: public Animals // indicates class B derived from class A

{  

public:  

Mammals()  

    {  

cout<< "This is a mammal\n";  

    }  

};  

class Herbivores  // indicates class C

{  

public:  

Herbivores()  

    {  

cout<< "This is a herbivore\n";  

    }  

};  

class Cow: public Mammals, public Herbivores

 // indicates class D derived from class B and class C

{  

public:  

Cow()  

    {  

cout<< "A cow is a herbivore mammal\n";  

    }    

};  

int main() {  

    Cow c;  

    return 0;  

}  

Output 

Hybrid_Inheritance_In_Cpp_2.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Example 2 : Combination of Multilevel and Single Inheritance 

In this type of combination, you might take the real-life example of the food chain process in our environmental ecosystem.

Plants indicate class A, Herbivores or primary consumers indicate class B, class C is indicated by carnivores or secondary consumers, tertiary consumers indicate class D.

Let us see the code for the same -

Code

#include <iostream>  

using namespace std;  

class Plants // indicates class A  

{  

public:  

Plants()  

    {  

cout<< "This is a producer\n";  

    }  

};  

class Herbivores: public Plants // indicates class B derived from class A

{  

public:  

Herbivores()  

    {  

cout<< "This is a primary consumer\n";  

    }  

};  

class Carnivore: public Herbivores  // indicates class C derived from class B

{  

public:  

Carnivore()  

    {  

cout<< "This is a secondary consumer\n";  

    }  

};  

class Secondary_Carnivore: public Carnivore // indicates class D derived from class C

{  

public:  

Secondary_Carnivore()  

    {  

cout<< "This is a tertiary consumer\n";  

    }  

};  

int main() {  

    Secondary_Carnivore s;  

    return 0;  

}  

Output

Hybrid_Inheritance_In_Cpp_3.

Conclusion

Inheritance is a process by which a class is inherited or derives properties of another class while maintaining its own properties and attributes. In this article, you have looked at various types of Inheritances, which include -

  • Single or Simple Inheritance in C++
  • Multiple Inheritance in C++
  • Multilevel Inheritance in C++
  • Hierarchical Inheritance in C++
  • Hybrid Inheritance in C++

Hybrid Inheritance in C++ is the process by which a sub class follows multiple types of inheritance while deriving properties from the base or super class. This is also known as Multipath Inheritance for the same reason.

To master and learn more about Hybrid Inheritance in C++ and all the other types of Inheritances properly and well versed, one might consider referring and learning in-depth from various resources, study materials, and course books.

If you are interested in understanding and acquiring knowledge on Hybrid Inheritance in C++ and all the other types of inheritances in order to become a full-stack web and desktop application developer, Simplilearn offers an exclusive Post Graduate Program in Full Stack Web Development course to master both backend and frontend with tools, like SpringBoot, AngularMVC, JSPs, and Hibernate to start your career as a full-stack developer. 

If you are not up to enrolling yourself in a full certification course yet, Simplilearn also offers free online skill-up courses in several domains, from data science and business analytics to software development, AI, and machine learning. Become a full-stack web developer today!

If you have any questions for us regarding this article, leave them in the comments section and our experts will get back to you right away.

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 17 Jun, 2024

6 Months$ 8,000
Full Stack Developer - MERN Stack

Cohort Starts: 30 Apr, 2024

6 Months$ 1,449
Automation Test Engineer

Cohort Starts: 1 May, 2024

11 Months$ 1,499
Full Stack Java Developer

Cohort Starts: 14 May, 2024

6 Months$ 1,449