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.

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

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.

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

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)   

};  

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

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.

Full Stack Java Developer Course

In Partnership with HIRIST and HackerEarthEXPLORE COURSE
Full Stack Java Developer Course

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.

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

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 full-stack web development certification 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.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.