One of the most important topics in Object-Oriented Programming is Inheritance, which is frequently asked in coding interviews. In this article, we will cover in detail multi-level inheritance in C++ with multiple examples as well. Let's begin!
What is Multi-level Inheritance in C++?
Let's first go back to what is inheritance in the programming world. Inheritance means the capability of a class to inherit the properties of another class. The existing class is called the superclass, and the new class is the subclass. When a class is derived from a base class, it inherits all the properties and behavior of the base class. It helps in avoiding redundancy of code.
Now, let's move to Multi-level Inheritance!
Multi-level inheritance is when a class B is derived from a parent class A and another class C is derived from a class B and so on. It is the process of deriving a class from another derived class. This makes a hierarchy, with each level of the hierarchy inheriting properties from the level above it.
We can say it is a father-son relationship. As everyone knows, the theory of evolution that Charles Darwin proposed. The theory says that organisms evolve over generations through the inheritance of physical or behavioral traits. It means humans' physical or behavioral traits are now inherited from their ancestors.
For example, if we take Grandfather as a base class then Father is the derived class that has features of Grandfather, and then Child is the also derived class that is derived from the sub-class Father which inherits all the features of Father. Here is another example:
Syntax of Multi-Level inheritance
Consider the structure given which shows us the syntax of multi-level inheritance:
Its Advantages & Uses
The biggest advantage of multi-level inheritance is Code Reusability. When we create derived classes from other derived classes, it is easier to create a specialized class without rewriting the code from scratch. Also, it supports the Polymorphism and Modularity features of C++, which is the ability of objects to take on multiple forms.
This type of inheritance makes code more efficient, organized, and flexible. However, one needs to be extra careful to design class hierarchies that are easy to maintain.
C++ Implementation of Multi-level Inheritance
Consider the C++ code given which shows us the complete working of the multi-level inheritance in detail:
In int main, first, we create an object of class C named 'obj'. Since class C is derived from a class B which is again derived from class A. So, class C has access to all data members and member functions of class A and class B. Similarly, class B has access to all data members and members functions to class A. Therefore, we will get no error and the output will be as shown below :
Multi-level vs Multiple Inheritance in C++
The following table will showcase the difference between multi-level vs multiple inheritance in C++:
|Multilevel Inheritance||Multiple Inheritance|
|In Multilevel Inheritance, the parent shares inherits with the kid, and the child then becomes the parent of another class, sharing the parent's resources with its offspring.||In Multiple Inheritance, each child can be derived from two or more parents.|
|Multilevel inheritance can have multiple levels of inheritance. The base class is the parent of all the classes.||Multiple Inheritance has two class levels, i.e., the base class (parent class) and the derived class (child class).|
|Multilevel inheritance is easy to implement and is widely used in various applications.||Multiple Inheritance is complex in nature and is not preferred widely.|
So, in this article, we looked at multilevel inheritance, its syntax, and how it is implemented in C++. We also understood the difference Between Multilevel and Multiple Inheritance in C++. Congratulations on getting this far! Now give yourself a pat on the back. Good job!