Home » Hybrid inheritance in C++

Hybrid inheritance in C++

by Online Tutorials Library

Hybrid inheritance in C++

Inheritance is defined as the process in which one class inherits the property of another class. The class whose property is inherited is called as Base class or the parent of that class. The class that inherits the base class’s properties (parent class) is the derived class.

For example, A son inherits the properties of his father. This article will give you a brief introduction to hybrid inheritance and its examples.

Hybrid inheritance in C++

Definition

Combining various types of inheritance like multiple, simple, and hierarchical inheritance is known as hybrid inheritance.

In simple inheritance, one class is derived from a single class which is its base. In multiple inheritances, a class is derived from two classes, where one of the parents is also a derived class. In hierarchical inheritance, more than one derived class is created from a single base class.

In hybrid inheritance, there is a combination of one or more inheritance types. For instance, the combination of single and hierarchical inheritance. Therefore, hybrid inheritance is also known as multipath inheritance.

Example

Hybrid inheritance in C++

The diagram shows the hybrid inheritance that is a combination of single inheritance and multiple inheritance.

Single inheritance – Class B inherits class A. Thus an example of single inheritance.

Multiple inheritance – Class D is inherited from multiple classes( B and C shown above D). Thus an example of multiple inheritance.

Syntax code of the above example

Hybrid inheritance in C++

This example shows the combination of multilevel and single inheritance.

Multilevel inheritance – As seen from the above diagram, Class B inherits class A and class C inherits class B. Thus, it is an example of multilevel inheritance.

Single inheritance – From the above diagram, Class D inherits class B. Thus, it is an example of single inheritance.

Syntax code for the above example

A real-life example of hybrid inheritance

Hybrid inheritance in C++

In a real-world scenario, we all drive a Car. So Car is a class that comes under vehicle class. Thus an instance of single inheritance.

If we talk about the Ferrari, that is a combination of the racing car and a normal car. So class Ferrari is derived from the class Car and Class Racing.

Hence, the above example is a single and multiple inheritance. It is a perfect example of hybrid inheritance (single + multiple).

Code

Output

This is a vehicle  This is a car  This is for Racing  Ferrari is a Racing Car      

You may also like