Types of Inheritance in C++
Inheritance Types in C++
Majorly there are 5 different types of classified Inheritance in C++, which are –
- Single inheritance
- Multiple Inheritance
- Multilevel inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
- Also known as Virtual Inheritance
Single Level Inheritance
In such type of inheritance, the derived class can only inherit from one base class.
Example – In the image, B(Derived Class) is only inheriting from A (One Base Class)
In MultiLevel Inheritance, a derived class is inherited from a base class, which in turn is derived from another base class.
Example – In our Example C is derived Class inheriting from B (Base Class to C), which in turn is also a derived Class inheriting from Class A (Base Class to B).
Here Class B Acts has base class to Class C and itself is derived class in relationship with Class A.
In Multiple Inheritance, A derived Class can have more than 1 Base Classes, thus it is deriving properties from various base Classes.
Example – In the image, B is a derived class which is deriving its properties from 3 different base classes, A1, A2 and A3.
It in some terms is opposite of Multiple inheritance. Here a Base Class is parent to various Derived Classes.
Example – In our example, A acts as Base Class to B1, B2 and B3, which all are derived classes of A.
This is essentially a combination of one or more types of inheritance.
For Example – Multiple and Hierarchical inheritance combination.
OOPs Advanced – 1
- Types of Inheritance in c++
- Upcasting and Downcasting in C++
- Operator Overloading (detailed)
- Constructor Overloading in C++