What does C++’s inheritance mean?
Inheritance is used in C++ to support the idea of reusability. By inheriting and deriving from an existing class, we can reuse its properties. The new class that is inherited from the base class is referred to as the derived class, and the old class is referred to as the base class.
The syntax for inheriting a class is
// Derived Class syntax
class derived_class_name : access_modifier base_class_name
{
// body of the derived class
}
Types of inheritance in C++
Individual Inheritance
A derived class inherits from just one base class when there is single inheritance involved.
We have two classes, ClassA and ClassB, for instance. If ClassB inherits from ClassA, then ClassB is now able to use ClassA’s functionalities. This inheritance is a solitary one.
class ClassA
{
//body of ClassA
};
//derived from ClassA
class ClassB : public ClassA
{
//body of ClassB
};
Multiple Inheritance
One derived class may inherit from multiple base classes through a process known as multiple inheritance.
For example, we have three classes ClassA, ClassB, and ClassC. If ClassC is inherited from both ClassA & ClassB, it means that ClassC can now implement the functionalities of both ClassA & ClassB. This is multiple inheritances.
class ClassA
{
//body of ClassA
};
class ClassB
{
//body of ClassB
};
//derived from ClassB and Class C
class ClassC : public ClassA, public ClassB
{
//body of ClassC
};
Hierarchical Inheritance
A type of inheritance known as a hierarchical inheritance occurs when numerous derived classes are descended from a single base class.
For example, we have three classes ClassA, ClassB, and ClassC. If ClassB and Class C are inherited from ClassA, it means that ClassB and ClassC can now implement the functionalities of ClassA. This is hierarchical inheritance.
class ClassA
{
//body of ClassA
};
//derived from ClassA
class ClassB : public ClassA
{
//body of ClassB
};
//derived from ClassA
class ClassC : public ClassA
{
//body of ClassC
};
Multilevel Inheritance
One derived class can inherit from another derived class at multiple levels, or multilevel inheritance.
For example, we have three classes ClassA, ClassB, and ClassC. If ClassB is inherited from ClassA and ClassC is inherited from ClassB, it means that ClassB can now implement the functionalities of ClassA and ClassC can now implement the functionalities of ClassB. This is multilevel inheritance
class ClassA
{
//body of ClassA
};
//derived from ClassA
class ClassB : public ClassA
{
//body of ClassB
};
//derived from ClassB
class ClassC : public ClassB
{
//body of ClassC
};
Hybrid Inheritance
Hybrid inheritance is a combination of different types of inheritances.
For example, we have four classes ClassA, ClassB, ClassC, and ClassD. If ClassC is inherited from both ClassA and ClassB and ClassD is inherited from ClassC, it means that ClassC can now implement the functionalities of both ClassA and ClassB and ClassD can now implement the functionalities of ClassC. This is multilevel inheritance where both multilevel and multiple inheritances are present.
class ClassA
{
//body of ClassA
};
class ClassB
{
//body of ClassB
};
//derived from ClassA and ClassA
class ClassC : public ClassA, public ClassB
{
//body of ClassC
};
//derived from ClassC
class ClassD : public ClassC
{
//body of ClassD
};