Chapter 3
Chapter 3
Chapter 3
EMPLOYEE
MANAGER
Multiple Inheritance
A derived class with more than one base class is called Multiple Inheritance.
VC MD
MANAGER
Hierarchical Inheritance
When more than one class inherits from the same class it is called Hierarchical
Inheritance.
EMPLOYEE
MD VC PM
Multilevel Inheritance
When a derived class is the base class of another class, it is called Multilevel
Inheritance.
EMPLOYEE
PM
TL
Hybrid Inheritance
When a class inherits from multiple base classes and all of its base
classes are subclasses of some other class, it is called Hybrid
Inheritance.
TRAIN
RAJDHANI SHATABDI
EXPRESS
Implementing Inheritance
class A
{ };
class B : public A
{ }; // where class A is a direct base class.
class A
{ };
class B : public A
{ };
class C : public B
{ }; // Here class A is a indirect base class or supper class.
Visibility modes in Inheritance
There are three types of visibility modes.
1. Private
2. Protected
3. Public
The visibility modes control the accessibility for the inherited members of
base-class in the derived class
Visibility mode table
Access rules for Base class members
EXAMPLE:
}
Constructor and Destructor with Inheritance
Constructor and destructor with inheritance
When an object of a derived class is created , the program first calls the constructor
for base class, then the constructor for derived class .
Similarly , when an object expires , the program first calls the derived destructor ,if
any ,and then the base class destructor .
One important point is that constructors and destructors of
base class are not inherited by the derived class.
Whenever the derived class needs to invoke base class’s
constructor or destructor ,it can invoke them through
explicitly calling them.
As long as no base class constructor takes any arguments ,
the derived class need not have a constructor function.
But if any base class contains a constructor with one or
more arguments, then it is mandatory for the derived class
to have a constructor and pass the arguments to the base
class constructors.
Passing Argument to Base from Derived
To pass the base class arguments , the constructor of the derived
class uses a special argument passing mechanism.
Syntax =>
Derived :: Derived (type1 x, type2 y, …) : baseclass(x,y)
{
-------
-------
}
Polymorphism
When an object is bound to its function call at run time is called run time
polymorphism.
It is also called late or dynamic binding.
It’s possible by implementing:-
1. Virtual function
2. Pure virtual function
Run time polymorphism
It is normal practice to declare a function virtual inside the base class and
redefine it in the derived classes.
Sometime the function inside the base class is not used for performing any
task,such functions are called do-nothing functions.
A do-nothing function may be defined as follows:-
virtual ret-type fun-name(parameter-list)=0;
Test t //invalid
Hybrid Inheritance
Lets consider a situation where multilevel ,multiple and hierarchical
inheritance, are involved.
All the public and protected members of ‘grand-Parent’ are inherited into
child twice via two different path.
This means, ‘child’ would have duplicate sets of members inherited from
grand-Parent . This introduces ambiguity.
How to avoid this ambiguity
The duplication of inherited members due to these multiple paths can be
avoided by making the common base class as virtual base class while
declaring the direct or intermediate base classes .
When a class is made a virtual base class , C++ takes necessary care to
see that only one copy of that class is inherited .
The keyword virtual and public may be used in either order.
Virtual Base Class Example:
class base {
public: int i;
};
class derive1 :virtual public base {
public: int j;
};
class derive2 :virtual public base {
public: int k;
};
class derive3 : public derive1, public derive2 {
public:int sum;
};
int main()
{
derive3 ob;
ob.i = 10;
ob.j = 20;
ob.k = 30;
ob.sum = ob.i + ob.j + ob.k;
cout << ob.i << " " << ob.j << " " << ob.k << endl;
cout << ob.sum;
return 0;
}
Thank You