In C++, inheritance is a fundamental object-oriented programming (OOP) concept that allows one class to inherit its attributes and functions from another. This implies that in addition to adding new members, the derived class can utilize every member of the base class.
There are two types of inheritance classes in C++:
A derived class inherits the properties and methods of its base class. The derived class may inherit all or a portion of the original class's characteristics and methods. The derived class may also contain its own set of attributes and methods.
A base class is one that other classes derive from. The base class specifies the characteristics and methods that its subclasses inherit. In addition, the base class can define its properties and methods.
C++ supports the 5 types of inheritance listed below:
Single inheritance
A derived class inherits only from one base class. It creates a parent-child relationship, allowing the derived class to access members of the base class.
A derived class can inherit from several base classes. This allows the derived class to access the members and behaviors of several parent classes, resulting in complex class hierarchies.
A chain of inheritance in which one derived class acts as the base class for another. This generates a class hierarchy in which each level inherits characteristics and behaviors from the level above it.
Multiple derived classes inherit from a single base class. Each derived class shares similar traits with the original class but may have its distinct features.
Hybrid inheritance combines many types of inheritance inside a single class hierarchy, including single, multiple, multilevel, and hierarchical inheritance. It enables complex relationships & structures between classes, which may lead to greater code complexity.
In C++, there are 3 Access Modes for Inheritance: