Types of Inheritance in C++ - Informatic Point

Types of Inheritance in C++

In this tutorial, you will learn the Types of Inheritance in C++. The ability of a class to derive attributes and properties from another class is called Inheritance. Inheritance is one of the most essential features of Object-Oriented Programming.

There are essentially five different types of inheritance that can be used in C++ which are placed below. Each of the below-mentioned inheritance types is defined as per the way the derived class derives property from the base class.

Types of Inheritance in C++ with Syntax:

Different types of inheritance are below with syntax.

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 base_classname //Base class
{
    properties;
    methods;
};
class derived_classname : visibility_mode base_classname //Derived Class
{
    properties;
    methods;
};

Example:

#include <iostream>
using namespace std;
class A {
public:
  A(){
     cout<<"Constructor of A class"<<endl;
  }
};
class B: public A {
public:
  B(){
     cout<<"Constructor of B class";
  }
};
int main() {
   //Creating object of class B.
   B obj;
   return 0;
}
Output
Constructor of A class Constructor of B class

Multiple Inheritance:

In Multiple inheritances, a single derived class can inherit from one or more base class. For example, as defined below, a Derived class inherits property from both Class Base(A) and Class Base(B).

Syntax:

 class A
 { ........... };
 class B
 { ........... } ;
 class C : acess_specifier A,access_specifier A // derived class from A and B
 { ........... } ;

Example:

#include <iostream>
using namespace std;
class Base {
  public:
    Base()
    {
      cout << "This is a Base Class." << endl;
    }};
class Parent {
  public:
    Parent()
    {
      cout << "This is a Parent Class." << endl;
    }};
class Soft: public Base, public Parent {
};
int main()
{
    Soft codeon;
    return 0;
}
Output
This is a Base Class. This is a Parent Class.

Multilevel Inheritance:

In multilevel inheritance, the derived class inherits property from another derived class. For example, as defined below, the Derived class B inherits property from class Base A and class C inherits property from class B. So, here is syntax and an example.

Syntax:

class A
{ ... .. ... };
class B: public A
{... .. ...};
class C: public B
{... ... ...};

Example:

#include <iostream>
using namespace std;

class A
{    public:
      void display()
      {
          cout<<"Base class content.";
      }};
class B : public A
{  };
class C : public B
{  };
int main()
{
    C obj;
    obj.display();
    return 0;
}
Output
Base class content.

Hierarchical Inheritance

In hierarchical inheritance, more than one derived class obtains property from a single base class. For example, as explained below, Class Derived classes inherit property from a single class Base. So, Learn below.

class base_class {
     ... .. ...
}
class first: public base_class {
     ... .. ...
}
class second: public base_class {
     ... .. ...
}
class third: public base_class {
     ... .. ...
}

Example:

#include <iostream>
using namespace std;
class A {
public:
  A(){
     cout<<"Constructor of A class"<<endl;
  }};
class B: public A {
public:
  B(){ 
     cout<<"Constructor of B class"<<endl;
  }};
class C: public A{
public:
  C(){
     cout<<"Constructor of C class"<<endl;
  }};
int main() {
   //Creating object of class C
   C obj;
   return 0;
}
Output
Constructor of A class Constructor of C class

Hybrid Inheritance in C++

Hybrid inheritance is a combination of both multilevel and hierarchical inheritance. So, Let’s take a look at syntax and example.

syntax:

Class Derived A: access_mode Base
{
//body of Derived1 class which inherit property from the base class
};
Class Derived B: access_mode Base
{
//body of Derived B class which inherit property from Base class
};
Class Derived C: access_mode Derived1, access_mode Derived2
{
//body of Derived C class which inherit property from both Derived A and Derived B class.
};

Example:

#include <iostream>
using namespace std;

class A
{
 	public:
 	int x;
};
class B : public A
{
 	public:
 	B()  //constructor to initialize x in base class A
 	{
 	   x = 776;
 	}};
class C
 {
 	public:
 	int y;
 	C()   //constructor to initialize y
 	{ y = 10;  }};
class D : public B, public C   //D is derived from class B and class C
{ 	public:
 	void sum()
 	{ cout << "Sum= " << x + y;	}};
int main()
{    D obj1;          //object of derived class D
 	obj1.sum();
 	return 0;
}       
Output
Sum= 786

One thought on “Types of Inheritance in C++

Leave a Reply

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