Inheriting from Multiple Classes
Learn how to implement multiple and multilevel inheritance in C++ classes. Discover the diamond problem that arises from multiple inheritance and understand how virtual inheritance resolves this ambiguity, ensuring clear and maintainable code.
We'll cover the following...
Up until now, we have understood how a (child) class can inherit from another (parent) class. But in C++, it’s also possible that a class can inherit from more than one base class. This can be done using different methods. Let’s explore some of these methods and the potential issues that can arise with this.
Multilevel inheritance
To understand multilevel inheritance, imagine a family tree. In a family tree, there can be grandparents, parents, and children. In programming, this is similar to multilevel inheritance.
Multilevel inheritance occurs when a class is derived from another derived class. This means that a class (child class) inherits from a base (parent) class, and this base (parent) class also inherits from another base (grandparent) class. This can be illustrated as: