Class Inheritance in C++

Class Inheritance in C++

Class Inheritance which is a property of OOPs allows a new class to take up the behaviour as well as structure of existing classes, without having to re-design and re-implement this behaviour and structure for the new class.  Hence, the most important advantage of inheritance is code reusability.  A class that has been defined and tested may be used many times, which is extensibility feature supported by inheritance.  Thus the software developed can be reused and can be easily extended.  Every class inherits all the features of the super class.  Only additional features need to be defined, which specify and extend the class further.  The class from which the properties are inherited is referred to as the base class, whereas that class receiving inherited properties is termed as derived class.

Inheritance is achieved by deriving new classes from the existing ones and thus forming a hierarchy of classes.  The following are some of the advantages of inheritance:

a) Reusability:  Inheritance supports the concept of reusability. i.e. reusing existing codes without rewriting the entire code again.  We can derive new classes out of existing ones, without actually modifying it.

b)  Extensibility:  Extensibility of existing codes is the other advantage of using inheritance.  The derived class inherits some or all the properties of the base class and adds some extra features of its own.  This phenomenon of deriving a new class out of existing one i.e. derivation or inheritance helps in extending existing classes by adding new features to them.

c)  Transitive nature:  Inheritance is transitive in nature.  By transitive nature of inheritance we mean that if a child class inherited from a parent class is inherited further then all the sub-child of child class will in turn receive the properties of the parent class too.  For example: If a class named ‘child’ is inherited from another class named ‘father’, then all the sub-classes of ‘child’ will automatically inherit the properties of ‘father’.

d)  We can easily express inheritance relationship through inheritance diagram which is another advantage of inheritance.

All these features make inheritance an important phenomenon / property of OOPs.

Example: Create a class “Employee” of an organization with members Employee-no, Name, Designation, Basic pay.  Define a derived class Executive whose basic_pay is more than Rs. 10,000.

#include <iostream.h>
#include <conio.h>

#define MAX 5

class employee
int employee_no;
char name[25];
char desig[25];
float basic_pay;
void getdata();
float check_sal();
void putdata();

employee :: employee()

void employee :: getdata()
cout<<“\nEnter Employee Code>”;
cout<<“\nEmployee Name>”;
cout<<“\nBasic Salary>”;

void employee :: putdata()
cout<<“\n\n\nEmployee Code:”<<employee_no;
cout<<“\nEmployee Name>”<<name;
cout<<“\nBasic Salary>”<<basic_pay;

float employee :: check_sal()

class executive : public employee
int empno;
float comm;
void getinfo(int i)
{empno=i;cout<<“\nCommission = “;
void putinfo()
{cout<<“\nFor employee no “<<empno;
cout<<“, commission = “<<comm;

void main()
int i,k=0,n=0;
employee emp[MAX];
executive exec[MAX];
cout<<“\nInput Employee Information:”;
cout<<“\nEnter Executive Information:”;
cout<<“\nDisplaying Employee Information:”;