In C++, we have 5 different types of Inheritance.
Namely,
Single Inheritance
Multiple Inheritance
Hierarchical Inheritance
Multilevel Inheritance
Hybrid Inheritance (also known as Virtual Inheritance)
1. Single Inheritance in C++
This is the simplest type of inheritance. In the single inheritance, one derived
class can inherit from only one base class.
For example, the class Derived is inheriting from only one Class Base.
class A
{
... .. ...
};
class B: public A
{
... .. ...
};
Here, class B is derived from the base class A.
2. Multiple Inheritance in C++
Multiple Inheritance is a feature of C++ where a single derived class can inherit
property from more than one base class. For example, as explained below, class
Derived inherits property from both Class Base1 and Class Base2.
class A
{
... .. ...
};
class B: public A
{
... .. ...
};
class C: public A, public B
{
... .. ...
};
3. Hierarchical Inheritance in C++
In hierarchical inheritance, more than one sub class is inherited from a single
base class.
In hierarchical inheritance, more than one sub class is inherited from a single
base class. In this, all features that are common in child classes are included in
the base class.
For example: Football, Cricket, Badminton are derived from Sports class.
Syntax of Hierarchical Inheritance
class base_class {
... .. ...
}
class first_derived_class: public base_class {
... .. ...
}
class second_derived_class: public base_class {
... .. ...
}
4. Multilevel Inheritance in C++
In multilevel inheritance, the derived class inherits property from another derived
class.
For example, class B inherits from class A and class C inherits property from class
B.
class A
{
... .. ...
};
class B: public A
{
... .. ...
};
class C: public B
{
... ... ...
};
5. Hybrid (Virtual) Inheritance in C++
Hybrid inheritance is a combination of more than one type of inheritance (Combining
Hierarchical inheritance and Multiple Inheritance.).
For example:- A child and parent class relationship that follows multiple and
hierarchical inheritance both can be called hybrid inheritance.