C++ supports multiple inheritance, multilevel inheritance and hierarchical inheritance, which are the different forms of multiple inheritance in C++ programming language.
- Multiple Inheritance: In multiple inheritance, a class is derived from more than one base class. This allows a class to inherit the properties and methods of multiple classes.
- Multilevel Inheritance: In multilevel inheritance, a class is derived from a class that is derived from another class. It allows you to create a hierarchy of classes where each class is a subclass of another class.
- Hierarchical Inheritance: In hierarchical inheritance, a class is derived from multiple classes, but each class is not derived from the same class. Instead, each class is derived from a different base class.
Multiple Inheritance
Multiple inheritance is a feature of object-oriented programming language in which an object or class can inherit characteristics and features from more than one parent class. In other words, it is a situation where a single class is derived from more than one base class. The class which is derived from multiple base classes is known as the derived class or the sub class.
Example:
Consider the following class structures to demonstrate multiple inheritance.
#include <iostream> using namespace std; class Base1 { public: int publicData1; }; class Base2 { public: int publicData2; }; class Derived: public Base1, public Base2 { }; int main() { Derived d; d.publicData1 = 10; d.publicData2 = 20; cout << "Public Data1: " << d.publicData1 << endl; cout << "Public Data2: " << d.publicData2 << endl; return 0; }Output
Public Data1: 10 Public Data2: 20
Multilevel Inheritance
In multilevel inheritance, a derived class becomes the base class for another derived class. This allows for a hierarchical inheritance structure. The syntax is similar to single and multiple inheritance.
Here is an example of multilevel inheritance in C++:
#include <iostream> using namespace std; class Base { public: int baseVariable; }; class Derived1 : public Base { public: int derived1Variable; }; class Derived2 : public Derived1 { public: int derived2Variable; }; int main() { Derived2 derived2Object; derived2Object.baseVariable = 10; derived2Object.derived1Variable = 20; derived2Object.derived2Variable = 30; cout << "Base Variable: " << derived2Object.baseVariable << endl; cout << "Derived1 Variable: " << derived2Object.derived1Variable << endl; cout << "Derived2 Variable: " << derived2Object.derived2Variable << endl; return 0; }Output
Base Variable: 10 Derived1 Variable: 20 Derived2 Variable: 30
Hierarchical Inheritance
In hierarchical inheritance, multiple derived classes inherit from a single base class. This allows for a clean and organized inheritance structure. The syntax is similar to single and multiple inheritance.
#include <iostream> using namespace std; class Base { public: int baseValue; Base() {} Base(int baseValue) { this->baseValue = baseValue; } }; class Derived1: public Base { public: int derived1Value; Derived1() {} Derived1(int baseValue, int derived1Value): Base(baseValue) { this->derived1Value = derived1Value; } }; class Derived2: public Base { public: int derived2Value; Derived2() {} Derived2(int baseValue, int derived2Value): Base(baseValue) { this->derived2Value = derived2Value; } }; int main() { Derived1 d1(10, 20); Derived2 d2(30, 40); cout << "Derived1's baseValue: " << d1.baseValue << endl; cout << "Derived1's derived1Value: " << d1.derived1Value << endl; cout << "Derived2's baseValue: " << d2.baseValue << endl; cout << "Derived2's derived2Value: " << d2.derived2Value << endl; return 0; }Output
Derived1's baseValue: 10 Derived1's derived1Value: 20 Derived2's baseValue: 30 Derived2's derived2Value: 40
In this example, the class Base is the base class, Derived1 and Derived2 are the derived classes and both the derived classes inherit the baseValue from the base class Base.