Ads block

Banner 728x90px

Inheritance in C++

Inheritance is an important feature of C++. Basically, Inheritance is a process to transfer properties and behavior of one class (base/parent class) to another class(derived/child class). It is one of the mostly used feature that's plays an important role in object oriented programming language.

To understand inheritance more clearly, we can co-relate it with real world. As one object acquires all the properties and behavior of the parent object, inheritance also works on this factor. In such way, we can reuse, extends or modify the attributes and methods of any class.

Syntax of derived class

class derived_class_name : visibility_mode base_class_name
{
  //some attributes and method
  //of derived class.

};

Some new terms are used here:

Base class: the class being inherited by another class.

Derived class: the class that inherits from another class.

Visibility mode: The visibility mode specifies whether the features of the base class are publicly inherited or privately inherited. It can be public or private.


Note: Private members/attributes of base class are never inherited.
Always remember that in C++, the default mode of visibility is private.

Types Of Inheritance

C++ supports five types of Inheritance:

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

Now, we will discuss about these inheritance and its differences one by one with simple examples.


Single Inheritance

It is called single inheritance because we can derive only one new class that inherit the properties and behaviour of the base class. It means every parent class has single child class, which object can access the attributes and methods of the base class.

#include<iostream.h>
using namespace std;

class Info   // base class
{
  Public:
    int roll_no = 32;
    float fee = 5034.50;
    void display(float fee)
    {
      cout<<"your fee is "<<fee<<endl;
    }
};
class Student:Public Info   // derived class
{
  Public:
    int age = 20;
    void course()
    {
      cout<<"Computer Science"<<endl;
    }
};
int main()
{
  Student S;   // object created
  cout<<"Roll no. is "<<S.roll_no<<endl;
  cout<<"age is "<<S.age<<endl;
  S.display();
  S.course();
  return 0;
}

Roll_no. is 32
age is 20
your fee is 5034.50
Computer Science


Here, Info is a base class and Student is a child class. Public access specifier allow object S to access the method of base class.


Multiple Inheritance

Multiple inheritance can be defined as a class that acquires or inherits the properties and behaviour of the more than one class. It means a derived class has multiple base class.

#include<iostream.h>
using namespace std;

class A
{
  int a1, a2;
  public:
    void getdata()
    {
      cout<<"Enter the value of a1 and a2 :"<<endl;
      cin>> a1 >> a2;
    }
    void putdata()
    {
      cout<<"\nvalue of a1 is :"<< a1 << "and a2 :"<<a2;
    }
};

class B
{
  int b1, b2;
  public:
    void indata()
    {
      cout<<"\nEnter the value of b1 and b2 :" <<endl;
      cin>> b1 >> b2;
    }
    void outdata()
    {
      cout<<"\nvalue of b1 is :"<< b1 <<"and b2 :"<<b2;
    }
};

class C : public A, public B
{
  int c1, c2;
  public:
    void input()
    {
      cout<<"\nEnter the value of c1 and c2 :"<<endl;
      cin>> c1 >> c2;
    }
    void output()
    {
      cout<<"\nvalue of c1 is :"<< c1 <<"and c2 :"<<c2;
    }
};

void main()
{
  C obj;

    obj.getdata();
    obj.indata();
    obj.input();
    obj.putdata();
    obj.outdata();
    obj.output();
}

Enter the value of a1 and a2:
10
20
Enter the value of b1 and b2:
30
40
Enter the value of c1 and c2:
50
60
value of a1 is :10 and a2 is :20
value of b1 is :30 and b2 is:40
value of c1 is :50 and c2 is:60


Multilevel Inheritance

In the Multilevel inheritance, a class derived from another derived class. If a class B is derived from class A and class C derived from class B, like the given figure, then it is called multilevel inheritance. You can also derive class C, and then derive its derived and so on.

We can implement above class hierarchy of multilevel inheritance using following syntax:

Syntax:

class A
{
   // attributes
   // methods
};
class B: public A
{
   // attributes
   // methods
};
class C: public B
{
   // attributes
   // methods
};

In multi-level inheritance a class derived from another derived class

Here we have given some example to understand the concept of Multilevel inheritance.

#include<iostream.h>
using namespace std;

class First
{
  protected:
    int a;

  public:
    void getdata()
    {
      cout<<"\nEnter the value of a :"<<endl;
      cin>> a;
    }
    void putdata()
    {
      cout<<"\nThe value of a is :"<< a;
    }
};

class Second : public First
{
  protected:
    int b;
  public:
    void indata()
    {
      cout<<"\nEnter the value of b :"<<endl;
      cin>> b;
    }
    void outdata()
    {
      cout<<"\nThe value of b is :"<< b;
    }
};

class Third : public Second
{
  int c;
  public:
    void input()
    {
      cout<<"\Enter the value of c :" <<endl;
      cin>> c;
    }
    void output()
    {
      cout<<"\nThe value of c is :"<< c;
    }
    void addition()
    {
      int d = a+b+c;
      cout<<"\nAddition of variables: "<< d;
    }
};

void main()
{
  Third T;

    T.getdata();
    T.indata();
    T.input();
    T.putdata();
    T.outdata();
    T.output();
    T.addition();
}

Enter the value of a:
5
Enter the value of b:
6
Enter the value of c:
8
The value of a is: 5
The value of b is: 6
The value of c is: 6
Addition of variables: 19


Hierarchical Inheritance

In the Hierarchical inheritance, more than one classes derived from only single class. If a class B and class C is derived from class A, like below figure, then it is called hierarchical inheritance. Because there are more than one derived classes of a single base class thus both classes inherits the features of class A.

We can implement above class hierarchical inheritance using following stntax:

Syntax:

class A
{
   // attributes
   // methods
};
class B: public A
{
   // attributes
   // methods
};
class C: public A
{
   // attributes
   // methods
};

In hierarchical inheritance more than on classes derived from only one base class.

Here, we have given some example to understand the concept of Hierarchical inheritance.

#include<iostream.h>
using namespace std;

class A
{
  int a1, a2;
  public:
    void getdata()
    {
      cout<<"Enter the value of a1 and a2 :"<<endl;
      cin>> a1 >> a2;
    }
    void putdata()
    {
      cout<<"\nvalue of a1 is :"<< a1 << "and a2 :"<<a2;
    }
};

class B : public A
{
  int b1, b2;
  public:
    void indata()
    {
      cout<<"\nEnter the value of b1 and b2 :" <<endl;
      cin>> b1 >> b2;
    }
    void outdata()
    {
      cout<<"\nvalue of b1 is :"<< b1 <<"and b2 :"<<b2;
    }
};

class C : public A
{
  int c1, c2;
  public:
    void input()
    {
      cout<<"\nEnter the value of c1 and c2 :"<<endl;
      cin>> c1 >> c2;
    }
    void output()
    {
      cout<<"\nvalue of c1 is :"<< c1 <<"and c2 :"<<c2;
    }
};

void main()
{
  B Ob1;
  C Ob2;

    Ob1.getdata();
    Ob1.indata();

    ob2.getdata();
    ob2.input();

    ob1.putdata();
    ob1.outdata();

    Ob2.putdata();
    Ob2.output();
}

Enter the value of a1 and a2:
2
3
Enter the value of b1 and b2:
4
5
Enter the value of a1 and a2:
10
15
Enter the value c1 and c2:
6
7
The value of a1 is:2 and a2 is:3
value of b1 is:4 and b2 is:5
value of a1 is:10 and a2 is:15
value of c1 is:6 and c2 is:7