What are the Different Types of Inheritance in C++?

Inheritance is one of the most important features of Object Oriented Programming (OOPs). The capability of a class to derive properties and characteristics from another class is called Inheritance.
It is a process in which users can create new classes from the existing class.
The new class is known as the derived class also known as the child class.
Whereas the existing class is defined as the base class or parent class.
Inheritance allows the user to reuse the code. During the inheritance, the data members of the base class get copied into the derived class and they can be accessed depending on the visibility.
The order of inheritance of accessibility is always in decreasing order. From public to protected.

What are child and parent classes?

Child Class: A child class is defined as the class that inherits the features of another class. Also known as derived class. The number of child classes that can be inherited from a single-parent class depends on the type of the inheritance.

Parent Class: The class from which the child class inherits its features and properties is called the parent class. Also known as a base class. A single-parent class can derive multiple child classes which can be a type of Hierarchical Inheritance. This also depends on the different types of inheritance.

Syntax of Inheritance:

Basic Syntax for defining the child class and parent class in all types of inheritance in c++.

class parent_class
{
    //class definition of the parent class
};
class child_class : visibility_mode parent_class
{
   //class definition of the child class
};

Syntax Description:

  • Parent_class: Name of the parent class or the base class.
  • Child_name: Name of the child class or the derived class.
  • Visibility_mode: visibility mode specifies how the data members of the child class inherit from the parent class.

Why do we use Inheritance?

The properties of inheritance make the programming more efficient and are mostly used because of their benefits.
Code Reusability: One of the fine and main reasons for using inheritance is that you can easily reuse your code.
Transitive nature: Inheritance is also used because of its transitive nature.
It is always good to reuse the code that already exists rather than trying to create the one that is already present.
It saves time and increases the readability of the code.

Modes of Inheritance:

There are 3 types of modes in inheritance:

  • Public Mode: In the public mode, we can easily access all the members of the base class.
  • Private Mode: In the private mode, members of the base class become private in the derived class.
  • Protected Mode: In this protected mode, the members of the base class become protected in the derived class and now the members are

Accessible by the derived class and their member functions.

Below is the table that defines the control of the derived class over the members of the base class in different modes.

BASE CLASS DERIVED CLASS DERIVED CLASS DERIVED CLASS
PUBLIC PROTECTED PRIVATE
PUBLIC Public Protected Private
PROTECTED Protected Protected Private
PRIVATE Not Inherited / Remains Private Not Inherited / Remains Private Not Inherited / Remains Private

Types of Inheritance in C++

The following are the 5 types of inheritance in C++

  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

Single Inheritance:

In single inheritance, a class is allowed to inherit from only one class according to data structures in c++.. i.e. one subclass is inherited by one base class only.

Syntax of Single Inheritance in C++:

 class sub_class : access_mode base_class
 {
     //body of subclass
 };

Example of Single Inheritance in C++:

// base class 
class Vehicle { 
    public: Vehicle() { 
        cout << "This is a Vehicle" << endl; 
    } 
}; 
// sub class derived from two base classes 
class Car: public Vehicle { }; 

// main function 
int main() {       
// creating object of sub class will invoke the constructor of base classes     
Car obj;
return 0; }

Output
This is a vehicle

Multiple Inheritance:

In this type of inheritance a single derived class may inherit from two or more than two base classes. Multiple inheritance specifies access specifiers separately for all the child classes at the time of inheritance. The constructors of inherited classes are called in the same order in which they are inherited.


Syntax of Multiple Inheritance in C++

class sub_class : access_mode base_class1, base_class2
{
   //body of subclass
};

Example of Multiple Inheritance in C++:

class A {
  public:
  A() { 
       cout << "A's constructor called" << endl; 
      }
  };
  class B {
    public:
    B() { 
         cout << "B's constructor called" << endl; 
        }
   };
  class C: public B, public A {  // Note the order
    public:
    C() {
         cout << "C's constructor called" << endl; 
         }
    };
  int main() {
     C c;
     return 0;
  }

Output:
B’s constructor called
A’s constructor called
C’s constructor called

Multilevel Inheritance:

In this type of inheritance, a derived class is created from another derived class. Let’s take an example: there are 3 classes. A is the base class that derives from class B. therefore B is the derived class of A. And C class is derived from class B. Therefore, B is the base class for class C.

Example of Multilevel Inheritance:

class A {
    public:
    A() {
         cout << "A's constructor called" << endl;
        }
    };
    class B : public A {
       public:
       B() { 
            cout << "B's constructor called" << endl; 
            }
       };
    class C: public B {
      public:
      C() { 
          cout << "C's constructor called" << endl; 
          }
      };    
    int main() {
       C c;
       return 0;
   }

Output:
A’s constructor called
B’s constructor called
C’s constructor called

Hierarchical Inheritance:

In this type of inheritance more than one subclass inherits from a single base class. This inheritance has a tree-like structure in which every class acts as a base class for one or more child classes.

Example of Hierarchical Inheritance:

// base class 
class A { 
    public: 
    A() { 
       cout<<"This is A’s constructor"<< endl; 
    } 
}; 
// first sub class 
class B: public A { }; 
// second sub class 
class C: public A { }; 
// main function 
int main() {    
    /* creating object of sub class will invoke the constructor of base class */
  B obj1;
  C obj2;
 return 0; 
} 

Output:
This is A’s constructor
This is B’s constructor

Hybrid Inheritance:

Hybrid inheritance is a combination of two or more types of inheritance. For example, the combination of multiple inheritance and multilevel inheritance can be combined together which forms a hybrid inheritance.

Example of Hybrid Inheritance:

// base class
class A {
    public:
    A() {
         cout << "This is A" << endl;
        }
  };
//base class 
class B {
    public:
    B() {
         cout<<"This is B " <<endl;
         }
};
// first sub class
class C: public A {
};
// second sub class
class D: public A, public B {
};
// main function   
int main() {  
// creating object of sub class will invoke the constructor of base //class
 D d;
 return 0;
}

Output:
This is A
This is B

Inheritance in C++ – FAQs

1. Does C have multiple inheritance?

No, as we know, Inheritance is the property of OOPs i.e. Object-oriented programming. Therefore, there is no compiler-level support for inheritance in C.

2. What is the difference between multiple and multilevel inheritance?

Multiple inheritances are a single derived class that may inherit from two or more than two base classes. Multiple inheritance specifies access specifiers separately for all the child classes at the time of inheritance.

In multilevel inheritance, a derived class is created from another derived class. Let’s take an example: there are 3 classes. A is the base class that derives from class B. therefore B is the derived class of A. And C class is derived from class B. Therefore, B is the base class for class C.

3. What are the superclass and subclass?

A superclass is defined as the class from which many subclasses can be created. It is also known as the parent class and base class.
The subclasses can inherit the properties of the superclasses. It inherits the properties of the superclass and creates the attributes for its own.

4. What are the benefits of inheritance?

The benefits of inheritance are as follows:
Inheritance allows the reusability of code.
It saves time as we do not need to rewrite the main code.
The structure of the inheritance is easy to understand.
Inheritance also improves the code readability.

5. What are the features of inheritance?

The basic features of inheritance are
In inheritance, we can easily create new classes from the existing class.
We can reuse the data members of the parent class.

6. What is the difference between inheritance and polymorphism?

Inheritance:
The capability of a class to derive properties and characteristics from another class is called Inheritance.
Inheritance supports the concept of reusability of the code.
Inheritance can be of five types that are discussed above in detail.
Polymorphism:
Polymorphism is that which can be defined in multiple ways. Polymorphism allows the object to decide which form of the function to implement at compile time as well as run time.
They both are used in pattern designing.

So, in this blog, we have tried to explain different types of Inheritance in c++. If you want to improve your foundation in any language including C,C++,Java,Python then you can follow this link Prepbyte Courses.

Leave a Reply

Your email address will not be published. Required fields are marked *