INHERITANCE
Contents
INHERITANCE
The process of defining a new class in terms of existing class is called inheritance. The existing class is called the base class and new class is called the derived class.
It is a concept of linking two or more classes with each other in a hierarchical manner so that their properties and functions can be shared. The public and protected members of the base class will be inherited and are accessible in the derived class depending upon the visibility mode.
This leads to the biggest advantage of re-usability of the members and avoids redundancy.
TYPES OF INHERITANCE
SINGLE INHERITANCE
When one class is inherited from a single base class.
A----B
Here A is the base class and B is the derived class of A.
HIERARCHICAL INHERITANCE
In this type,two classes are derived from a single base case.
A ---- B
A ----C
Here A is the base class of two derived classES i.e. B and C
MULTIPLE INHERITANCE
In this a new class is inherited from two base classes.
A----C
B----C
Here C is the derived class of A and B.
MULTILEVEL INHERITANCE
A----B----C
Here A is the base class of B and B is the base class of A.
HYBRID INHERITANCE
It is the combination of two or more types of inheritance.
QUIZ
class Teacher
{
char TNo[5],
TName[20],
Deptf[l0];
int Workload;
protected:
float Salary;
void AssignSal(float);
public:
Teacher( ) ;
void TEntry( ) ;
void TDisplay( );
};
class Student
{
char Admno[10],
SName[20],
Stream[10];
protected:
int Attendance,
TotMarks;
public:
Student( );
void SEntry( );
void SDisplay( );
};
class School : public Student, public Teacher
{
char SCode[10],
SchName[20];
public:
School ( ) ;
void SchEntry( );
void SchDisplay( );
};