Difference Between Multilevel and Multiple Inheritance in C++
8.
Frequently Asked Questions
8.1.
If there are three classes. Class Z is derived from class Y, and Y is derived from X, Which class destructor will be called at last if the object of Z is destroyed.
8.2.
What are the advantages of multilevel inheritance in C++?
8.3.
What is multilevel inheritance in C++ real-world example?
Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one class to inherit properties and behaviors from another class. In C++, inheritance is a powerful feature used to create a hierarchical relationship between classes, promoting code reuse and extensibility.
The class that inherits any other class's properties is called Derived, Child, or Subclass. The class whose properties are inherited is called base class or parent or superclass. When a class is derived from a class that is also derived from every other class, i.e., a class having a couple of parent classes, such inheritance is called Multilevel Inheritance. The level of inheritance can be extended to any number of levels depending upon the relation. Multilevel inheritance is like the relation between grandfather, father, and child.
C++ Multilevel Inheritance Block Diagram
Syntax for Multilevel Inheritance:
class A // base class
{
...........
};
class B: acess_specifier A // derived class from class A
{
...........
} ;
class C: access_specifier B // derived from derived class B
{
...........
} ;
Example of Multilevel Inheritance in C++
C++
C++
#include <iostream> using namespace std; class base { public: int x; void getdata() { cout << "Enter value of x= "; cin >> x; } }; class derive1: public base { public: int y; void readdata() { cout << "Enter value of y= "; cin >> y; } }; class derived2: public derive1 { private: int z; public: void indata() { cout << "Enter value of z= "; cin >> z; } void product() { cout << "Product= " << x * y * z; } }; int main() { derived2 a; a.getdata(); a.readdata(); a.indata(); a.product(); return 0; }
You can also try this code with Online C++ Compiler
In C++, multilevel inheritance involves a chain of classes where each class inherits from the previous one. This means that a derived class inherits properties and behaviors from its immediate base class and all its ancestor classes.
Here’s a simple example to illustrate multilevel inheritance:
C++
C++
#include <iostream> using namespace std;
class A { public: void displayA() { cout << "Class A" << endl; } };
class B : public A { public: void displayB() { cout << "Class B" << endl; } };
class C : public B { public: void displayC() { cout << "Class C" << endl; } };
int main() { C obj; obj.displayA(); // Inherited from class A obj.displayB(); // Inherited from class B obj.displayC(); // Defined in class C return 0; }
You can also try this code with Online C++ Compiler
In this example, class C inherits from class B, which in turn inherits from class A. Thus, an object of class C can access members of both class A and class B.
Example of Constructor in Multilevel Inheritance
In multilevel inheritance, a class derives from a base class, which itself is derived from another class. Constructors in multilevel inheritance are invoked in a specific order: the constructor of the base class is called first, followed by the derived class constructors.
Example
C++
C++
#include <iostream> using namespace std;
// Base class class Animal { public: Animal() { cout << "Animal constructor called." << endl; } };
// Derived class class Mammal : public Animal { public: Mammal() { cout << "Mammal constructor called." << endl; } };
// Further derived class class Dog : public Mammal { public: Dog() { cout << "Dog constructor called." << endl; } };
int main() { Dog d; // Object of the Dog class return 0; }
You can also try this code with Online C++ Compiler
Animal constructor called.
Mammal constructor called.
Dog constructor called.
Difference Between Multilevel and Multiple Inheritance in C++
Feature
Multilevel Inheritance
Multiple Inheritance
Definition
Involves a chain of classes where a class inherits from another derived class.
Involves a class inheriting from more than one base class.
Hierarchical Structure
Forms a linear hierarchy (e.g., A → B → C).
Forms a non-linear hierarchy (e.g., A, B → C).
Complexity
Less complex, easier to manage.
More complex due to potential for ambiguity.
Example
class C : public B {}; where class B : public A {};
class C : public A, public B {};
Ambiguity Resolution
No ambiguity as inheritance is linear.
Ambiguity may arise and is resolved using scope resolution operator or virtual inheritance.
Frequently Asked Questions
If there are three classes. Class Z is derived from class Y, and Y is derived from X, Which class destructor will be called at last if the object of Z is destroyed.
The destructors are called within the reverse order of the constructors being called. Therefore in multilevel inheritance, the constructors are created from parent to child, which leads to destruction from child to parent. Therefore, class X destructor will be known as at last.
What are the advantages of multilevel inheritance in C++?
The advantage of multilevel inheritance in C++ is code reusability. It lets you inherit from multiple parent classes, reducing redundant code.
What is multilevel inheritance in C++ real-world example?
The multilevel inheritance in C++ real-world example is to imagine a Vehicle class (car, bike) inheriting from Engine, then a SportsCar inheriting from both.
What is multilevel and hybrid inheritance in C++?
Multilevel is a chained inheritance (A -> B -> C). Hybrid inheritance means inheriting from multiple parents at once (like combining animal traits).
Conclusion
We learned what multilevel inheritance is and how to implement it in C++.
The main benefit of multilevel inheritance is that it allows the flow of resources to the depths of the hierarchy of classes. Use Code360 to practice various DSA questions asked in many interviews. You will also getinterview experiencesfrom people working in big companies.