C++

A class can be derived from more than one classes, which means it can inherit data and functions from multiple base classes.

class derived-class: access-specifier base-class

Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default.

Type of Inheritance:

When deriving a class from a base class, the base class may be inherited through public, protected or private inheritance. The type of inheritance is specified by the access-specifier as explained above.

·        Public Inheritance: When deriving a class from a public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class. A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.

·        Protected Inheritance: When deriving from a protected base class, public and protected members of the base class become protected members of the derived class

·        Private Inheritance: When deriving from a private base class, public and protected members of the base class become private members of the derived class.

Types of Inheritance

In C++, we have 5 different types of Inheritance. Namely,

  1. Single Inheritance
  2. Multiple Inheritance
  3. Hierarchical Inheritance
  4. Multilevel Inheritance
  5. Hybrid Inheritance (also known as Virtual Inheritance)


Single Inheritance

In this type of inheritance one derived class inherits from only one base class. It is the most simplest form of Inheritance.

Single1_1.png


Multiple Inheritance

In this type of inheritance a single derived class may inherit from two or more than two base classes.

multiple.png


Hierarchical Inheritance

In this type of inheritance, multiple derived classes inherits from a single base class.

hierarchical.png


Multilevel Inheritance

In this type of inheritance the derived class inherits from a class, which in turn inherits from some other class. The Super class for one, is sub class for the other.

multilevel.png


Hybrid (Virtual) Inheritance

Hybrid Inheritance is combination of Hierarchical and Mutilevel Inheritance.

hybrid.png


Return to top