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;
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
C.GetData(); //Statement 3 : Calling Base Class Method()
C.ReadData();
C.PutData(); //Statement 5 : Calling Base Class Method()
C.WriteData();
}
Output :
Enter Employee Id : 1
Enter Employee Name : Kumar
Enter Employee Age : 29
Enter Employee Salary : 45000
Enter Registration No. : 715
Enter Company Name : TutorialDost
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.
Derived class can inherit Base class as private, protected or public.
C++ Access Modifiers
Access modifiers defines the scope of members of BaseClass into DerivedClass.
C++ supports the followling access modifiers:
Private Modifier
Protected Modifier
Public Modifier
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.
Own class Derived class main()
Private
Protected
Public
Scope of base class members in derived class
Access base class as private
When a derived class inherit base class as private, protected and public members of base class
will become private members of derived class.
Access base class as protected
When a derived class inherit base class as protected, protected and public members of base class
will become protected members of derived class.
Access base class as public
When a derived class inherit base class as public, protected members of base class will become
protected members of derived class and public members of base class will become public
members of derived class.
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:
Example of, occurrence of C++ ambiguity
#include<iostream.h>
#include<conio.h>
class ClassA
{
public:
int a;
};
class ClassB : public ClassA
{
public:
int b;
};
class ClassC : public ClassA
{
public:
int c;
};
class ClassD : public ClassB, public ClassC
{
public:
int d;
};
void main()
{
ClassD obj;
//obj.a = 10; //Statement 1, Error
occur
//obj.a = 100; //Statement 2, Error
occur
obj.ClassB::a = 10; //Statement 3
obj.ClassC::a = 100; //Statement 4
obj.b = 20;
obj.c = 30;
obj.d = 40;
cout<< "\n A from ClassB : "<< obj.ClassB::a;
cout<< "\n A from ClassC : "<< obj.ClassC::a;
cout<< "\n B : "<< obj.b;
cout<< "\n C : "<< obj.c;
cout<< "\n D : "<< obj.d;
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.
There are two ways to avoid c++ ambiguity.
Using scope resolution operator
Using virtual base class
1. Avoid ambiguity using scope resolution operator
Using scope resolution operator we can manually specify the path from which data member a
will be accessed, as shown in statement 3 and 4, in the above example.
obj.ClassB::a = 10; //Statement 3
obj.ClassC::a = 100; //Statement 4
Note : still, there are two copies of ClassA in ClassD.
2. Avoid ambiguity using virtual base class
To remove multiple copies of ClassA from ClassD, we must inherit ClassA in ClassB and
ClassC as virtual class.
Example to avoid ambiguity by making base class as a virtual base class
#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;
Output :
A : 100
B : 20
C : 30