Example of Inheritance
Example of Inheritance
Inheritance means access the properties and features of one class into another class. The class
who is going to provide its features to another class will be called base class and the class who is
using the properties and features of another class will be called derived class.
Example of 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;
void PutData()
{
cout<<"\n\nEmployee Id : "<<Id;
cout<<"\nEmployee Name : "<<Name;
cout<<"\nEmployee Age : "<<Age;
cout<<"\nEmployee Salary : "<<Salary;
}
};
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()
{
Output :
Enter Employee Id : 1
Enter Employee Name : Kumar
Enter Employee Age : 29
Enter Employee Salary : 45000
Employee Id : 1
Employee Name : Kumar
Employee Age : 29
Employee Salary : 45000
Registration No. : 715
Company Name : TutorialDost
Consider the statement 1, we are publically inheriting an Employee class into Company class
using colon(:).Now, The object of Company class can access the member function GetData()
and PutData() of Employee class.
Private Modifier: The scope of private members are restricted to its own class. Private members
can't be accessed by the derived class or in main() function.
Protected Modifier: The scope of protected members are restricted to its own class and derived
class. Protected members can be accessed by the derived class but they can't be accessed in
main() function.
Public Modifier: Public members can be accessed by its own class, derived class and in main()
function.
Private
Protected
Public
Types of Inheritance
C++ supports six types of inheritance as follows:
Single Inheritance
Multilevel Inheritance
Multiple Inheritance
Heirarchical Inheritance
Hybrid Inheritance
Multipath Inheritance
Single Inheritance
A derived class with only one base class is called single inheritance.
Multilevel Inheritance
A derived class with one base class and that base class is a derived class of another is called
multilevel inheritance.
Multiple Inheritance
A derived class with multiple base class is called multiple inheritance.
Heirarchical Inheritance
Multiple derived classes with same base class is called hierarchical inheritance.
Hybrid Inheritance
Combination of multiple and hierarchical inheritance is called hybrid inheritance.
Multipath Inheritance
A derived class with two base classes and these two base classes have one common base class is
called multipath inheritance.
C++ Multipath Inheritance
A derived class with two base classes and these two base classes have one common base class is
called multipath inheritance.
C++ Ambiguity
Ambiguity in C++ occur when a derived class have two base classes and these two base classes
have one common base class. Consider the followling figure:
#include<iostream.h>
#include<conio.h>
class ClassA
{
public:
int a;
};
void main()
{
ClassD obj;
obj.b = 20;
obj.c = 30;
obj.d = 40;
Output :
A from ClassB : 10
A from ClassC : 100
B : 20
C : 30
D : 40
In the above example, both ClassB & ClassC inherit ClassA, they both have single copy of
ClassA. However ClassD inherit both ClassB & ClassC, therefore ClassD have two copies of
ClassA, one from ClassB and another from ClassC.
If we need to access the data member a of ClassA through the object of ClassD, we must specify
the path from which a will be accessed, whether it is from ClassB or ClassC, bco'z compiler
can't differentiate between two copies of ClassA in ClassD.
#include<iostream.h>
#include<conio.h>
class ClassA
{
public:
int a;
};
void main()
{
ClassD obj;
obj.b = 20;
obj.c = 30;
obj.d = 40;
Output :
A : 100
B : 20
C : 30