Inheritance in C++

This tutorial will talk in detail about the concept of inheritance in C++.

What is inheritance in C++?

In C++ language, the concept of inheritance describes the property by which an object of one class derives the properties of another.

The class which derives the properties is called the derived class, subclass or child class; and the one from which the properties are derived is called the parent class.

Mode of inheritance:

There are three modes of inheritance namely public, private and protected. The default mode in C++ is the private mode in which no elements are inherited from the base class. In the protected mode, only the protected members can be inherited and in the public mode, all public and protected members can be inherited, not the private ones.

You may read: Access Specifiers in C++

The syntax for derived class in C++:

class derived_name:visibility_mode parent_name
{
   //statements;
} 

Types of inheritance in C++:

There are five types of inheritance in C++:

  1. Single
  2. Multiple
  3. Multilevel
  4. Hierarchical
  5. Hybrid

We can understand more about each of the above with some examples:

Single inheritance: When a child class derives its properties from a single parent class, it is known as single inheritance.

The sample code given below will help you understand it better:

#include<iostream>
using namespace std;
class student
{ public:
    int marks1=95,marks2=85,marks3=75,marks4=65,marks5=55;
};

class avge:public student
{
public:
    float avg=(marks1+marks2+marks3+marks4+marks5)/5;


};

int main()

{
    avge A;
    cout<<"average is "<<A.avg;
    return 0;
}

The output for the above program will look like this:

average is 75
Process returned 0 (0x0)   execution time : 0.031 s
Press any key to continue.
  • Multiple inheritances: When a child class derives its properties from multiple parent classes, it is known as multiple inheritance. The sample code given below will help you understand better:
#include<iostream>
using namespace std;
class employee
{
public:
    int basic_sal=20000;
};
class bonus
{
public:
    int bonus=1500;
};
class total_sal:public employee,public bonus
{
public:
    int final_sal=basic_sal+bonus;
};
int main()
{
    total_sal T;
    cout<<"the final salary is "<<T.final_sal;
    return 0;
}

the output for the above code is:

the final salary is 21500
Process returned 0 (0x0)   execution time : 0.140 s
Press any key to continue.
  • Multilevel inheritanceIn this form of inheritance, a class derives its properties from an already derived class, i.e., a given class C derives its properties from B, which in turn derives from class A. The example below explains it better:
#include<iostream>
using namespace std;
class A
{public:
    int per=92;
};
class B:public A
{
public:
    float cgpa=per/10;
};
class C:public B
{
public:
    char grade='A';
};
int main()
{
    C obj;
    cout<<"per is"<<obj.per<<endl;
    cout<<"CGPA is"<<obj.cgpa<<endl;
    cout<<"Grade is"<<obj.grade<<endl;
    return 0;

}

In this example, class C inherits cgpa from class B, and class B inherits per from class A. The output for this code is:

per is92
CGPA is9
Grade isA

Process returned 0 (0x0)   execution time : 0.050 s
Press any key to continue.


  • Hierarchical inheritance: In this form of inheritance, two or more child classes imbibe common properties from the same class.

Given below is the sample code for hierarchical inheritance:

#include<iostream>
using namespace std;
class marks
{
public:

    float physics=89;
    float chemistry=98;
    float biology=90;
};
class gpap:public marks
{
public:
    float cgpap=physics/10;
};
class gpac:public marks
{
public:
    float cgpac=chemistry/10;
};
class gpab:public marks
{
public:
    float cgpab=biology/10;
};
int main()
{
    gpap A;
    gpac B;
    gpab C;
    cout<<"Score for physics: "<<A.cgpap<<endl;
    cout<<"Score for chemistry: "<<B.cgpac<<endl;
    cout<<"Score for biology: "<<C.cgpab<<endl;
    return 0;
}

In the above program, we are calculating the grade point average for each subject on the basis of the marks class, which is the parent class for all these classes. The output will look something like this:

Score for physics: 8.9
Score for chemistry: 9.8
Score for biology: 9

Process returned 0 (0x0)   execution time : 0.016 s
Press any key to continue.

  • Hybrid Inheritance: This the final type of inheritance in which two or more of the above-given types are combined together. For example, a combination of multiple and hierarchical inheritances.

Let us take a look at the code given below:

#include<iostream>
using namespace std;
class rad
{public:
    int rad=10;
};
class circle:public rad
{public:

    float area;
    void ar()
    {
        area=3.14*rad*rad;
        cout<<"area of circle is: "<<area<<endl;
    }
};
class bre
{
public:
    int breadth=25;
};
class rectangle:public rad,public bre
{
public:
    int ar1;
    void rec()
    {
        ar1=rad*breadth;
        cout<<"area of rectangle is: "<<ar1<<endl;
    }

};
int main()
{
    circle c1;
    c1.ar();
    rectangle r1;
    r1.rec();
    return 0;
}

The output is as follows:

area of circle is: 314
area of rectangle is: 250

Process returned 0 (0x0)   execution time : 0.131 s
Press any key to continue.




With this, we come to the end of this tutorial.

One response to “Inheritance in C++”

  1. Rakesh says:

    Thanks. It was really helpful and easy to understand.

Leave a Reply

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