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:
Base class-> Wood, Intermediate class-> furniture, subclass-> table
Syntax of Multi-Level inheritance
Consider the structure given which shows us the syntax of multi-level inheritance:
class A // base class { ........... }; class B : acess_specifier A // derived class { ........... } ; class C : access_specifier B // derived from derived class B { ........... } ;
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:
// Welcome to FavTutor // C++ program to implement // Multilevel Inheritance #include<bits/stdc++.h> using namespace std; // single base class class A { public: int a; void get_A_data() { cout << "Enter value of a: "; cin >> a; } }; // derived class from base class class B : public A { public: int b; void get_B_data() { cout << "Enter value of b: "; cin >> b; } }; // derived from class derive1 class C : public B { private: int c; public: void get_C_data() { cout << "Enter value of c: "; cin >> c; } // function to print sum void sum() { int ans = a + b + c; cout << "sum: " << ans; } }; int main() { // object of sub class C obj; obj.get_A_data(); obj.get_B_data(); obj.get_C_data(); obj.sum(); return 0; }
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 :
Output:
Enter value of a: 4 Enter value of b: 5 Enter value of c: 9 sum: 18
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. |
Conclusion
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!