What is Hybrid Inheritance in C++?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

When we combine more than one type of inheritance, it is called hybrid inheritance in C++. It is also referred to as a multipath inheritance because many types of inheritances get involved.

For example, multiple inheritances can be combined with the single or multilevel inheritance.

diagram of Hybrid Inheritance in C++

In the diagram above, there is a single inheritance between Class A and Class B, also there is a multiple inheritance between Class B, C, and D. As two different types of inheritance are involved, it is considered hybrid inheritance in C++.

A Real-Life Example of Hybrid Inheritance

real-life example of hybrid inheritance

A real-life example of hybrid inheritance is the flying car, where the car is inherited from the ground vehicle, and then the flying car uses multiple inheritances in the car and flying vehicle and uses the property of both. One can both drive and fly it.

Syntax

The syntax is similar to other types of inheritance, as we need to use them only.

Examples of Hybrid Inheritance in C++

Combination of Multiple Inheritance and Single Inheritance

In the above code, we have used four classes. The class cat does a single inheritance from a class animal and inherits its properties, by multiple inheritances of class cat and pet, a class kitty is formed.

First, the function of class Animal is called (while single inheritance) after that, in the multiple inheritance, as the functions are called according to the order of class used, the function of the class cat will be called first and then the function of the class pet.

At last, the function of the class kitty will be called.

Output :

Combination of Multilevel and Single Inheritance

In the code above, we are doing multilevel inheritance of Class A to Class B , and then Class B to Class C, and if we look closer to each level, we are performing a single inheritance, every time.

Output :

Conclusion

  • The capability of a class to derive properties and characteristics from another class is called Inheritance.
  • Base Class is the class from which data members and member functions are inherited, and the derived class is the class that inherits the properties.
  • When we combine more than one type of inheritance, it is called hybrid inheritance in C++.