What Is Hybrid Inheritance in C++: Types and Examples

Introduction To Hybrid Inheritance In C++ 

Inheritance is one of the fundamental concepts of object-oriented programming. A class acquires the
traits of another class through this approach. In C++, hybrid inheritance refers to a technique that combines two or more inheritance types.  

 

With the aid of a sample application, we will learn more about hybrid inheritance in C++ in this article. But let’s first get a handle on the fundamentals of inheritance. 

What Is Inheritance? 

It is creating new classes by deriving from the old ones. The currently existing class is known as the “base class” or “parent class,” and the recently created class is known as the “derived class” or “child class.” Now, when we say “inherited from the base class,” we mean the derived class. 

 

When we say a derived class inherits the base class, we imply that it receives all of the base class’s properties without altering them and has the option to add new features of its own. These new features won’t impact the base class in the derived class. The base class’s specialized class is known as the derived class. 

 

Sub Class or Derived Class: A subclass or derived class is a class that receives properties from another class. 

 

Super Class: The class from which a subclass inherits its properties is referred to as the Base Class or Super Class. 

Inheritance In C++ 

In practice, a programmer frequently has to create an object that possesses all the characteristics of its parent, and in some respects, persistence is unique. 

 

It might not be viable or cost-effective to code such special characteristics in the main class. In that situation, extending the base class results in the creation of a new class. Deriving it from a different class with additional features is another option. You can reuse, expand, or alter characteristics and behaviors defined in other classes in this way. As a result, an object that is straightforward to maintain and port is provided by the child class that is derived from numerous classes. The base class’s specialized class is known as the derived class. 

 

Technically, a class is inherited using the keyword “extends.” The colon (“:”) symbol is how a class is inherited from in C++. 

 

Example: 

  

class Employee{   

 float salary=40000;   

}   

class Programmer extends Employee{   

 int bonus=10000;   

 public static void main(String args[]){   

   Programmer p=new Programmer();   

   System.out.println(“Programmer salary is:”+p.salary);   

   System.out.println(“Bonus of Programmer is:”+p.bonus);   

}   

}   

  

Output: 

Programmer salary is: 40000.0 

Bonus of programmer is:10000 

  

The employee is the superclass, and the Programmer is a subclass. Programmer IS-A Employee is how the two classes are related to one another. It implies that an employee is a type of programmer. In the aforementioned illustration, the Programmer object has access to the Code Reusability field of both its own class and the Employee class. 

Types of Inheritance In C++ 

There are five different types of inheritance in C++: 

  1. Single Inheritance: 

A derived class only has one base class from which to inherit its properties. 

  1. Multiple Inheritance: 

A derived class is built using many basic classes. 

  1. Hierarchical Inheritance : 

In this case, more than one derived class inherits properties from one base class. 

  1. Multilevel Inheritance: 

Multilevel inheritance in c++ is when another derived class inherits property from one derived class. 

  1. Hybrid Inheritance: 

Any combination of the four inheritance types mentioned above is referred to as hybrid inheritance. 

 

Hybrid Inheritance In C++ 

Hybrid inheritance is a combination of several inheritance structures, such as multiple, simple, hierarchical, and multilevel inheritance in C++. 

 

In simple inheritance, one class descends from one base class. A class is descended from two classes in multiple inheritances, where one of the parents is also a descended class. A single base class can produce several derived classes thanks to hierarchical inheritance. 

 

One or more inheritance types are combined in hybrid inheritance. For instance, single inheritance is combined with hierarchical inheritance. As a result, multipath inheritance is another name for hybrid inheritance in C++. 

 

 

The block diagram in the above sentence illustrates the hybrid of single inheritance and multiple inheritances.  

 

Syntax of Hybrid Inheritance In C++ 

 

class A 

{ 

     ……… 

}; 

class B : public A 

{ 

     ………. 

} ; 

class C 

{ 

     ……….. 

}; 

 class D : public B, public C 

{ 

     ……….. 

}; 

 

According to the block diagram, class B is descended from class A through single inheritance, and class D is inherited through multiple inheritances from classes B and C. So, hybrid inheritance in C++  is the product of single inheritance and multiple inheritances working together. 

Hybrid Inheritance Example In C++ 

In the actual world, we all drive cars. Car, then, is a class that belongs to the vehicle class. Thus an instance of single inheritance. 

 

If we discuss the “Ferrari,” that is a cross between a racing vehicle and a regular vehicle. Thus, the classes Car and Racing are the ancestors of the class “Ferrari.” 

 

Thus, both single and multiple inheritances are shown in the example above. It is a perfect example of hybrid inheritance (single + multiple). 

Importance of Hybrid Inheritance In C++ 

 

  • Reusability is promoted by inheritance. A class can utilize all the features of the parent class from which it derives or inherits. 
  • Reuse improved dependability. There will already be tested and debugged base class code. 
  • Reusing old code reduces development and maintenance expenses. 
  • Subclasses are forced to adhere to a common interface by inheritance. 
  • Code extensibility and code redundancy are both supported by inheritance. 
  • Class libraries can be made more easily, thanks to inheritance. 

Conclusion 

It’s a good idea to consult and learn in-depth from numerous resources, study materials, and course books in order to master and understand more about hybrid inheritance in C++ and all the other forms of inheritance. But, here’s something better! Unext Jigsaw offers an exclusive PG Certificate Program in Data Science and Machine Learning featuring a Guaranteed Placement Program* that helps you master concepts such as Data Visualization, Statistical Data Analysis, Artificial Intelligence & Neural Networks. If you are interested in understanding and acquiring knowledge on Hybrid Inheritance in C++ and all the other types of inheritances, get yourself enrolled now. 

 

Related Articles

} }
Request Callback