Updated March 23, 2023
Introduction on Types of Inheritance in C++
In this article, we will go through different types of inheritance. There are mainly five different types of inheritance that can be used in C++ which are listed below. Each of the below mentioned inheritance type is defined as per the way derived class inherits property from the base class.
Types of Inheritance in C++ with Syntax
Here are the different types of inheritance which are explained below with syntax.
1. Single Inheritance
This is the simplest type of inheritance. In the single inheritance, one derived class can inherit property from only one base class. For example, as explained below, the class Derived is inheriting property from only one Class Base.
Syntax:
class Derived: access_mode Base
{
//body of Derived class which inherit property from only one base class
// access_mode can be public, private or protected
};
2. Multiple Inheritance
In Multiple inheritance, 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.
Syntax:
class Derived: access_mode Base1, access_mode Base2
{
//body of Derived class which inherit property from more than one base class that is Base1 & Base2
};
3. Multilevel Inheritance
In multilevel inheritance, the derived class inherits property from another derived class. For example, as explained below, class Derived1 inherits property from class Base and class Derived2 inherits property from class Derived1.
Syntax:
class Derived1: access_mode Base
{
//body of Derived1 class which inherit property from base class
};
class Derived2: access_mode Derived1
{
//body of Derived2 class which inherit property from Derived1 class
};
4. Hierarchical Inheritance
In hierarchical inheritance, more than one(multiple) derived classes inherit property from a single base class. For example, as explained below, Class Derived1 and Derived2 both inherit property from a single class Base.
Syntax:
class Derived1: access_mode Base
{
//body of Derived1 class which inherit property from base class
};
class Derived2: access_mode Base
{
//body of Derived2 class which inherit property from Base class
};
5. Hybrid Inheritance
Hybrid inheritance is a combination of both multilevel and hierarchical inheritance.
Syntax:
class Derived1: access_mode Base
{
//body of Derived1 class which inherit property from the base class
};
class Derived2: access_mode Base
{
//body of Derived2 class which inherit property from Base class
};
class Derived3: access_mode Derived1, access_mode Derived2
{
//body of Derived3 class which inherit property from both Derived1 and Derived2 class.
};
Recommended Articles
This is a guide to Types of Inheritance in C++. Here we discuss the basic concept, different types of inheritance in C++ along with their syntax. You can also go through our other related articles to learn more –