C++ supports six types of inheritance as follows:
Single Inheritance
Multilevel Inheritance
Multiple Inheritance
Hierarchical Inheritance
Hybrid Inheritance
Multipath Inheritance
A derived class with only one base class is called single inheritance.
A derived class with one base class and that base class is a derived class of another is
called multilevel inheritance.
A derived class with multiple base class is called multiple inheritance.
Multiple derived classes with same base class is called hierarchical inheritance.
Combination of multiple and hierarchical inheritance is called hybrid inheritance.
A derived class with two base classes and these two base classes have one common base
class is called multipath inheritance.
INHERITANCE
#include<iostream.h>
#include<conio.h>
class Employee
{
int Id;
char Name[25];
int Age;
long Salary;
public:
void GetData()
{
cout<<"\n\tEnter Employee Id : ";
cin>>Id;
cout<<"\n\tEnter Employee Name : ";
cin>>Name;
cout<<"\n\tEnter Employee Age : ";
cin>>Age;
cout<<"\n\tEnter Employee Salary : ";
cin>>Salary;
}
void PutData()
{
cout<<"\n\nEmployee Id : "<<Id;
cout<<"\nEmployee Name : "<<Name;
cout<<"\nEmployee Age : "<<Age;
cout<<"\nEmployee Salary : "<<Salary;
}
};
class Company : public Employee //Statement 1
{
int RegNo;
char CName[25];
public:
void ReadData()
{
cout<<"\n\nEnter Registration No. : ";
cin>>RegNo;
cout<<"\nEnter Company Name : ";
cin>>CName;
void WriteData()
{
cout<<"\n\nRegistration No. : "<<RegNo;
cout<<"\nCompany Name : "<<CName;
}
};
void main()
{
Company C; //Statement 2 : Creating Object of Derived Class
[Link](); //Statement 3 : Calling Base Class Method()
[Link]();
[Link](); //Statement 5 : Calling Base Class Method()
[Link]();
}
eXAMPLE OF MULTIPATH INHERITANCE
#include<iostream.h>
#include<conio.h>
class ClassA
{
public:
int a;
};
class ClassB : virtual public ClassA
{
public:
int b;
};
class ClassC : virtual public ClassA
{
public:
int c;
};
class ClassD : public ClassB, public ClassC
{
public:
int d;
};
void main()
{
ClassD obj;
obj.a = 10; //Statement 3
obj.a = 100; //Statement 4
obj.b = 20;
obj.c = 30;
obj.d = 40;
cout<< "\n A : "<< obj.a;
cout<< "\n B : "<< obj.b;
cout<< "\n C : "<< obj.c;
cout<< "\n D : "<< obj.d;