Inheritance and Polymorphism
Contents
2
What is inheritance?
Derived and base class
Inheritance types:
Single level
Multi level
Hierarchical
Multiple
Hybrid
Virtual Base Class
By: Kanika Sharma
Cont…
3
Abstract Classes
Pointer to object
This Pointer
Pointer to derived Class
Virtual Function
Pure Virtual function
Early vs Late binding
By: Kanika Sharma
Inheritance
4
Inheritance allows us to define a class in terms of
another class, which makes it easier to create and
maintain an application.
This also provides an opportunity to reuse the code
functionality and fast implementation time.
When creating a class, instead of writing completely
new data members and member functions, the
programmer can designate that the new class should
inherit the members of an existing class.
By: Kanika Sharma
Cont…
5
This existing class is called the base class, and the
new class is referred to as the derived class.
Syntax:
class derived-class: access-specifier base-class
Where access-specifier is one of public,
protected, or private, and base-class is the name
of a previously defined class.
If the access-specifier is not used, then it is private by
default.
By: Kanika Sharma
Access Control and Inheritance
6
A derived class can access all the non-private
members of its base class.
Thus base-class members that should not be
accessible to the member functions of derived classes
should be declared private in the base class.
A derived class inherits all base class methods with
the following exceptions:
Constructors, destructors and copy constructors of the base
class.
Overloaded operators of the base class.
The friend functions of the base class.
By: Kanika Sharma
7
By: Kanika Sharma
8
By: Kanika Sharma
Types of Inheritance
9
By: Kanika Sharma
Single level inheritance
10
In this type of inheritance one
derived class inherits from only
one base class.
By: Kanika Sharma
Program on single level inheritance
11
class B : public A
class A {
{ public:
void display2()
{
cout<<"Derived Class";
public: }
void display()
};
{ int main()
cout<<"Base {
B b;
Class"; b.display();
} b.display2();
return 0;
}; }
By: Kanika Sharma
Multilevel inheritance
12
In this type of
inheritance the derived
class inherits from a
class, which in turn
inherits from some
other class. The Super
class for one, is sub
class for the other.
By: Kanika Sharma
Program on multilevel inheritance
class B : public A 13
{
#include <iostream> public: int main()
int c; {
using namespace std; void sum() C c;
class A { c.getdata();
{ cout<<endl;
c=a+b; c.sum();
public: cout<<endl;
int a,b; } c.msg();
}; cout<<endl;
void getdata()
{ class C : public B return 0;
{ b"<<endl;
cout<<"Enter a and }
cin>>a>>b; public:
} void msg()
}; {
cout<<"Sum is"<<c;
}
By: Kanika Sharma
};
Multiple Inheritance
14
In this type of
inheritance a single
derived class may
inherit from two or
more than two base
classes.
By: Kanika Sharma
Ambiguity in multiple inheritance
15
In multiple inheritance, there may be possibility that
a class may inherit member functions with same
name from two or more base classes and the derived
class may not have functions with same name as
those of its base classes.
If the object of the derived class need to access one of
the same named member function of the base classes
then it result in ambiguity as it is not clear to the
compiler which base’s class member function should
be invoked. The ambiguity simply means the state
when the compiler confused.
By: Kanika Sharma
Solution of ambiguity in multiple
inheritance
16
The ambiguity can be resolved by using the scope
resolution operator to specify the class in which the
member function lies.
By: Kanika Sharma
Hierarchical inheritance
17
In this type of
inheritance, multiple
derived classes inherits
from a single base class.
By: Kanika Sharma
Hybrid Inheritance
18
"Hybrid
Inheritance" is a
method where one or
more types of inheritance
are combined together
and used.
By: Kanika Sharma
Diamond Problem
19
An ambiguity can arise when several
paths exist to a class from the same base
class. This means that a child class could
have duplicate sets of members
inherited from a single base class.
The "diamond problem" is an
ambiguity that arises when two classes B
and C inherit from A, and class D
inherits from both B and C.
C++ solves this issue by introducing a
virtual base class. When a class is made
virtual, necessary care is taken so that
the duplication is avoided regardless of
the number of paths that exist to the
child class.
By: Kanika Sharma
Virtual Base Class
20
When two or more objects are derived from a
common base class, we can prevent multiple copies
of the base class being present in an object derived
from those objects by declaring the base class as
virtual when it is being inherited.
Such a base class is known as virtual base class. This
can be achieved by preceding the base class’ name
with the word virtual.
By: Kanika Sharma
Program on virtual base class
21
class D: public B, public C
#include<iostream> {
using namespace std; public:
class A int sum;
{ };
public: int main()
int i; {
}; D ob;
ob.i = 10;//unambiguous since only one
class B : virtual public A copy of i is inherited.
{ ob.j = 20;
public: ob.k = 30;
int j; ob.sum = ob.i + ob.j + ob.k;
}; cout<<"Value of i is : "<< ob.i<<"\n";
class C: virtual public A cout<<"Value of j is :"<< ob.j<<"\n";
{ cout << "Value of k is :"<< ob.k<<"\n";
public: cout << "Sum is : "<< ob.sum <<"\n";
int k; return 0;
};By: Kanika Sharma }
Abstract Class
22
In C++ an abstract class is one which defines an
interface, but does not necessarily provide
implementations for all its member functions.
An abstract class is meant to be used as the base
class from which other classes are derived.
The derived class is expected to provide
implementations for the member functions that are
not implemented in the base class.
A derived class that implements all the missing
functionality is called a concrete class .
By: Kanika Sharma
Cont…
23
Abstract Class is a class which contains atleast one
Pure Virtual function in it.
Abstract classes are used to provide an Interface for
its sub classes.
Classes inheriting an Abstract Class must provide
definition to the pure virtual function, otherwise they
will also become abstract class.
By: Kanika Sharma
Why we cant create object of abstract class
24
When we create a pure virtual function in Abstract
class, we reserve a slot for a function in the VTABLE,
but doesn't put any address in that slot. Hence the
VTABLE will be incomplete.
As the VTABLE for Abstract class is incomplete,
hence the compiler will not let the creation of object
for such class and will display an errror message
whenever you try to do so.
By: Kanika Sharma
Program on abstract class
25
#include <iostream> int main()
{
using namespace std; Base *b;
class Base //Abstract base class Derived d;
{ b = &d;
public: b->show();
virtual void show() = 0; //Pure Virtual Function }
};
class Derived:public Base
{
public:
void show()
{
cout << "Implementation of Virtual Function in
Derived class"; }
};
By: Kanika Sharma
Characteristics of abstract classes
26
Abstract class cannot be instantiated, but pointers
and references of Abstract class type can be created.
Abstract class can have normal functions and
variables along with a pure virtual function.
Abstract classes are mainly used for Up casting, so
that its derived classes can use its interface.
Classes inheriting an Abstract Class must implement
all pure virtual functions, or else they will become
Abstract too.
By: Kanika Sharma
Virtual Functions
27
Virtual Function is a function in base class, which is
override in the derived class, and which tells the compiler
to perform Late Binding on this function.
Virtual Keyword is used to make a member function of
the base class Virtual.
Virtual functions allow us to create a list of base class
pointers and call methods of any of the derived classes
without even knowing kind of derived class object
A virtual member function for which no implementation
is given is called a pure virtual function . If a C++ class
contains a pure virtual function, it is an abstract class.
By: Kanika Sharma
Points to be remember
28
Only the Base class Method's declaration needs
the Virtual Keyword, not the definition.
If a function is declared as virtual in the base class,
it will be virtual in all its derived classes.
The address of the virtual Function is placed in
the VTABLE and the compiler
uses VPTR(vpointer) to point to the Virtual
Function.
By: Kanika Sharma
Cont…
29
To accomplish late binding, Compiler creates VTABLEs, for each class
with virtual function. The address of virtual functions is inserted into
these tables. Whenever an object of such class is created the compiler
secretly inserts a pointer called vpointer, pointing to VTABLE for that
object. Hence when function is called, compiler is able to resolve the
call by binding the correct function using the vpointer.
By: Kanika Sharma
Without Virtual functions
30
#include<iostream> class B:public A
using namespace std; {
Class A public:
{ void show()
public: {
void show() cout << "Derived Class";
{ }
cout << "Base class"; };
}
}; int main()
{
A * a; //Base class pointer
B b; //Derived class object
a= &b;
a->show(); //Early Binding Ocuurs
}
By: Kanika Sharma
With virtual function
31
#include<iostream>
using namespace std; int main()
{
class A A *a;
{ B b;
public: a = &b;
virtual void show() a -> show();
{ }
cout << "Base class\n";
}
};
class B: public A
{
private:
virtual void show()
{
cout << "Derived class\n";
} Kanika Sharma
By:
};
Pure Virtual Functions
32
Pure virtual Functions are virtual functions with no
definition. They start with virtual keyword and ends
with= 0.
Pure Virtual functions can be given a small definition
in the Abstract class, which you want all the derived
classes to have. Still you cannot create object of
Abstract class.
Also, the Pure Virtual function must be defined
outside the class definition. If you will define it
inside the class definition, complier will give an
error. Inline pure virtual definition is Illegal.
By: Kanika Sharma
Program on pure virtual function
33
class Derived:public Base
#include <iostream>
{
public:
using namespace std;
void show()
{ cout << "Implementation of Vir
class Base //Abstract base class
Function in Derived class"; }
{
};
public:
virtual void show() = 0; //Pure Virtual Function
int main()
};
{
Base *b;
void Base :: show() //Pure Virtual definition
Derived d;
{
b = &d;
cout << "Pure Virtual definition\n";
b->show();
}
}
By: Kanika Sharma
Comparison between virtual and pure virtual
functions
34
Virtual functions Pure Virtual functions
Virtual function have a Pure virtual function
function body. have no function body.
Overloaded can be done Overloading is must in
by the virtual funciton. pure virtual funciton.
(Optional) (Must)
It is define as : virtual int It is define as : virtual
myfunction(); int myfunction() = 0;
By: Kanika Sharma
Pointer to object
35
The pointers pointing to objects are referred to as
Object Pointers.
Just like other pointers, the object pointers are
declared by placing in front of a object pointer's
name. It takes the following general form :
class-name ∗ object-pointer
When accessing members of a class using an object
pointer, the arrow operator (->) is used instead of
dot operator.
By: Kanika Sharma
This pointer
36
As soon as you define a class,
the member functions are
created and placed in the
memory space only once.
That is, only one copy of
member functions is
maintained that is shared by
all the objects of the class.
Only space for data members
is allocated separately for each
object
By: Kanika Sharma
This pointer
37
When a member function is called, it is automatically
passed an implicit (in-built) argument that is a
pointer to the object that invoked the function. This
pointer is called this.
By: Kanika Sharma
Consider a situation
38
The this pointer can be thought of analogous to the
ATM card.
For instance, in a bank there are many accounts.
The account holders can withdraw amount or view
their bank-statements through Automatic-Teller-
Machines.
Now, these ATMs can withdraw from any account
in the bank, but which account are they supposed
to work upon ?
This is resolved by the ATM card, which gives the
identification of user and his accounts, from where
the amount is withdrawn.
Similarly, the this pointer is the ATM cards for
objects, which identifies the currently-calling
object.
The this pointer stores the address of currently-
calling object.
By: Kanika Sharma
Early binding vs late binding
39
Early binding Late binding
Compiler knows at Compiler doesn't know
compile time which until runtime which
function to invoke. function to invoke.
one way to get late
Direct function calls
binding is to use
can be resolved using a function pointers or
process known as early the other way is the use
binding. of virtual functions in
inheritance
By: Kanika Sharma