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;
public:
employee();
void getdata();
float check_sal();
void putdata();
};

employee :: employee()
{
employee_no=0;
basic_pay=0;
}

void employee :: getdata()
{
cout<<“\nEnter Employee Code>”;
cin>>employee_no;
cout<<“\nEmployee Name>”;
cin>>name;
cout<<“\nDesignation>”;
cin>>desig;
cout<<“\nBasic Salary>”;
cin>>basic_pay;
}

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

float employee :: check_sal()
{return(basic_pay);
}

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

void main()
{
int i,k=0,n=0;
employee emp[MAX];
executive exec[MAX];
clrscr();
cout<<“\nInput Employee Information:”;
for(i=0;i<MAX;i++)
{emp[i].getdata();
if(emp[i].check_sal()>10000)
{
cout<<“\nEnter Executive Information:”;
exec[k].getinfo(i+1);
k++;
}
}
cout<<“\nDisplaying Employee Information:”;
for(i=0,k=0;i<MAX;i++)
{
emp[i].putdata();
if(emp[i].check_sal()>10000)
{exec[k++].putinfo();
}
}
getch();
}