Updated March 23, 2023
Introduction to Multilevel Inheritance in C++
Inheritance is a property wherein an object of one class possesses the properties of another class and can further inherit the properties to other classes. Such type of parent-child relationship between class frames to be an inheritance. Multilevel is a kind of inheritance where a base or child class has more than one parent classes and it can be extended to any level. Therefore, like other inheritance features like hierarchical inheritance and multiple inheritances, we can conclude that those base classes who have more than one parent class been called Multilevel inheritance in C++.
Syntax:
class base_classname
{
Properties;
Methods;
}
class intermediate_classname: visibility_mode
Base_classname
{
Properties;
Methods;
};
class child_classname:visibility_mode
Intermediate_classname
{
Properties;
Methods;
};
Explanation to the above syntax: A base class is created followed by its class name and the base class will have its own properties and methods which will further get inherited by the next intermediate class with the access modifier and some scope with it. An intermediate class will acquire some common properties, or it can be non-common properties also depending on the requirement of the class what it wants to acquire. Finally, the child class will have all the basic and required properties inherited by the parent class and both the base class and intermediate class are acting as a parent class. Therefore, it is said above that child classes having more than one parent class and inheriting the properties are said to be acquiring multilevel inheritance.
How Multilevel Inheritance Works in C++?
- For instance take three classes into consideration class A as a parent class, class B as second class or intermediate class and class C as the third or child class. Depending on the type of relationship it has between the objects of the classes or the classes itself the level of inheritance can be extended to any level.
- The special feature of this type of inheritance is that the inheritance level can be extended to any level of inheritance.
- Based on the access modifier or visibility the scope is caught and the properties of the base class get inherited. Access modifiers can be anything from private, public and protected.
- For example: If we take the example of animals then mammals are its intermediate class further humans are the child class which is derived from intermediate-class mammals and acquires all the properties of mammals. i.e. humans which are the child class will acquire all the properties of the mammal or intermediate class.
Examples to Implement Multilevel Inheritance in C++
Below are the examples to implement in Multilevel Inheritance in C++:
Example #1
Code:
#include <iostream>
using namespace std;
class P
{
public:
void display ()
{
cout<<"All contents of Base Class";
}
};
class Q: public P
{
public:
void display1()
{
cout<<"\nall content of class Q.";
}
};
class R: public Q
{
public:
void display2()
{
cout<<"All contents if class R.";
}
};
int main ()
{
R r;
r.display();
r.display1();
return 0;
}
Output:
Explanation to the above code: In Example1 class P is the base class which have a method to display and get the contents of the base class which will be inherited or acquired by the intermediate class Q which also must display the content i.e. the properties of the method and for that intermediate class Q class P becomes the parent class. Further for class R which is the child class, in this case, will acquire the properties of the intermediate class and object of the child class will get all the contents and features of the parent class.
Example #2
Code:
#include <iostream>
using namespace std;
class Transport
{
public:
Transport ()
{
cout << "This is a Transport" << endl;
}
};
class fourWheelr: public Transport
{
public:
fourWheelr()
{
cout<<"Transport have 4 wheels as object"<<endl;
}
};
class AutoRikshaw: public fourWheelr{
public:
AutoRikshaw()
{
cout<<"AutoRikshaw has 4 Wheels"<<endl;
}
};
int main ()
{
AutoRikshaw obj;
return 0;
}
Output:
Explanation to the above code: In Example2 the base class of transport has the feature or attribute of Transport and its ability which will be passed or inherited to the intermediate class which is the four-wheeler. This four-wheeler can acquire the properties of the Transport and further the auto-rickshaw is a four-wheeler which can inherit the properties of the four-wheeler again it can be extended to any level of inheritance which again depends on the requirements.
Example #3
Code:
#include <iostream>
using namespace std;
class base {
public:
int m;
void getdata ()
{
cout << "Enter the value of m = "; cin >> m;
}
};
class derive1 : public base {
public:
int n;
void readdata ()
{
cout << "Enter the value of n = "; cin >> n;
}
};
class derive2 : public derive1
{
private:
int o;
public:
void indata()
{
cout << "Enter the value of o = "; cin >> o;
}
void product()
{
cout << "Product = " << m * n * o;
}
};
int main ()
{
derive2 p;
p.getdata();
p.readdata();
p.indata();
p.product();
return 0;
}
Output:
Conclusion
Unlike other inheritance multi-level inheritance has a special feature that it can be extended to any level of inheritance besides a condition that it depends on the requirements of the object and the class. Also, a base class can have more than one parent class. This situation can even arise in the real world which can be overcome using multi-level inheritance.
Recommended Articles
This is a guide to Multilevel Inheritance in C++. Here we discuss how Multilevel Inheritance works in C++? along with respective examples. You can also go through our other related articles to learn more –