Programs

What is Hybrid Inheritance In C++? It’s Types With Examples

We often use the term “Inheritance” in a programming context. It’s a feature practised in programming to make the best reuse of the codes.  If you recall the concept of “IS-A” in OOP (Object Oriented Programming), you will find it’s based on the Inheritance feature. The implementation of inheritances will help you gain advanced project ideas in C++.

What is Inheritance?

In the process of Inheritance, the objects of one class acquire the properties and behaviour of another class. Here a new class is derived from the existing base class.  The member class derived is usually called a child class and the base class member of the parent class. After the child class is derived, it inherits all the properties and methods from the parent class.

Additionally, the child class can have its exclusive properties and methods intended for a purpose. The additional features added locally to an existing class will not affect the properties it inherited from the parent class. Thus, the new class will have combined features of both classes.

Inheritance in C++

Often, in reality, a programmer needs to develop an object that has to:
1)  possess all qualities of its parent, and additionally,
2)  in some aspects, persistence is special. 

Coding such exceptional properties in the main class may not necessarily be practical and economical. In that case, a new class is derived by extending the base class. Also, deriving it from another class having additional qualities. In this manner, you can reuse, extend, or modify attributes and behaviours which are defined in other classes. Thus the child class derived from multiple classes gives an object that is easy to maintain and port. The derived class is the specialized class for the base class. 

Technically, the keyword “extends” is used to inherit a class. C++ uses the colon (“:”) symbol to inherit from a class.

Types of Inheritances

The use of a combination of inheritances and implementation differ for a purpose the classes are derived. There are different types of inheritances available in C++, as listed below:

  • Single Inheritance is where a derived class inherits properties and behaviour from a single base class. Example: Class A → Class B. 
  • Hierarchical Inheritance is where more than one derived class is created from a single base class. Example: Class A →  Class B → Class C. 
  • Multiple Inheritance is for deriving a class from multiple base classes. Here, the child objects programmers create will have combined aspects of characteristics and features from multiple parent classes. These objects do follow their hierarchies of base classes.  
  • Multilevel Inheritance is where a child class is derived from another derived class. This feature carries combined aspects of multiple classes and follows their hierarchies.
  • Hybrid Inheritance is a heterogeneous feature of using multiple inheritances. Here a child class is derived from one or more combinations of single, hierarchical, and multilevel inheritances. This inheritance is adopted for programs to mix different types of inheritance; for example, when mixing a single inheritance with multiple inheritances or maybe a situation when multiple inheritances are mixed within a single program.

Simple Example

Here Class B and Class C both are derived from the base class, Class A.

A hierarchical classification of Cars shows two classes. Both “Petrol Cars” and “Electric Cars” are derived from the base Class ‘Cars. 

Hybrid Inheritance In C++

There could be situations where we need to apply two or more types of inheritance combined to design a program. When a program involves more than one type of inheritance, it is called Hybrid Inheritance.

Hybrid inheritance is a combination of simple, multiple inheritance and hierarchical inheritance. Usually, in multiple inheritances, a class is derived from two classes where one of the parent classes is also a derived class and not a base class. 

Hybrid inheritance in C++ is the inheritance where a class is derived from more than one form or combinations of any inheritance. The hybrid inheritance in C++ is also called multipath inheritance, where one derived class can inherit properties of the base class in different paths. Sometimes also called multipath inheritance. For example, it can be achieved with a combination of both multilevel and hierarchical inheritance.

In short, hybrid inheritance is a combination of two or more types of inheritance. For example, by implementing single and multilevel inheritances in the same program.

Block Diagram of Hybrid Inheritance

The diagram represents the hybrid combination of two inheritances; the single inheritance and the multiple inheritances. Here, in single inheritance, class B is derived from class A. Similarly, in multiple inheritances, Class D is inherited from multiple classes. Here class B and class C. So, a mix of single inheritance and multiple inheritances forms a hybrid inheritance.

Hybrid inheritance is applicable in the scenarios where we are required to apply more than one inheritance in a program.

Syntax of Hybrid Inheritance In C++

A typical syntax and semantic for hybrid inheritance in C++ will follow as illustrated below:

Examples of Hybrid Inheritance In C++

Example 1:  Single + Multiple Inheritance

Let’s see how single and multiple inheritances are implemented.

 

Each block in this diagram represents a class, and the corresponding arrow the inheritance of a class.

Example 2: Single + Multilevel Inheritance

We can also implement other types of inheritance to constitute hybrid inheritances.

Let’s consider a Real-time example.

We derive a car subclass from the class Vehicle. Another class characterizes Racing. When we derive a final entity, both from the Car class and the Racing class, it will give a combined output. This derivative is Ferrari – the racing car.

Here is a simple program to illustrate the concept of hybrid inheritance in C++.

The Sample Code

The Output

Learn Software development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Conclusion

Hope this article helped you to understand the concept and types of Hybrid Inheritance in C++. Students in the stream of computer and computing-related programs can enhance their skills and technical credibility, such as OOP concepts. upGrad provides resources and a knowledge base to encourage such professionals to expertise in programming and compete in the industry market. At upGrad, you also get an opportunity to explore professional networking and skill development. 

We hope you will have an excellent learning opportunity in executing these C++ projects. If you are interested to learn more and need mentorship from industry experts, check out upGrad & IIIT Banglore’s Executive PG Program in Full-Stack Software Development.

What is inheritance in object-oriented programming?

Inheritance is a mechanism of reusing code and it helps in simulating the real world. It extends the concept of data abstraction in OOP. It is the process by which a class acquires the properties of another class. It is fundamental in OOP that a class can only be defined in terms of another class. This is because of the concept of abstraction. Inheritance is a mechanism that makes new object classes in the same class hierarchy as other objects. The main benefit of inheritance is code reuse. It is a way of reusing code. If you have a class and it does a particular job, you can use it for another job as well. Just say, a car and a truck do the same work, so you can use the same code to drive them.

What are the 3 scopes of inheritance in C++?

The scopes of inheritance in C++ are private, protected and public. Private inheritance is where one class is inherited from another class but the derived class cannot be accessed outside the class. These types of inheritance can only be used when the base class is not meant to be accessed outside the class. Protected inheritance is used when the base class is not meant to be accessed outside the class and is meant to access outside the class. Public inheritance is used when the base class is meant to be accessed outside the class.

What is multiple inheritance in C++?

Multiple inheritance is the ability to derive a class from more than one class. This feature is not available in C. This is one of the most important features of C++ that supports object-oriented programming. The implementation of multiple inheritance in C++ is based on the concept of inheritance classes. Multiple inheritance is a mechanism that supports object-oriented programming model that can be used to solve some problems, such as behavioral reuse and code reuse. Code reuse has always been a problem in C++, because C++ has only single inheritance support. Multiple inheritance is achieved by combining multiple interfaces into an object.

Want to share this article?

Prepare for a Career of the Future

Leave a comment

Your email address will not be published. Required fields are marked *

Our Popular Software Engineering Courses

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

×
Let’s do it!
No, thanks.