DYNAMIC
POLYMORPHISM
BY - DHARMISHA SHARMA
1
POLYMORPHISM
POLY MORPH POLYMORPHISM
Poly means Many.
Morph means Forms.
2
3
• In school behaves as student.
• In bus behaves as passenger.
• At malls behaves as customer.
• At home behaves as son.
REAL LIFE EXAMPLE
TYPES OF POLYMORPHISM
POLYMORPHISM
COMPILE TIME
FUNCTION
OVERLOADING
OPERATOR
OVERLOADING
RUN TIME
VIRTUAL
FUNCTIONS
4
COMPILE TIME POLYMORPHISM
Also called early binding, static binding
or static linkage.
An object is bound to its function call at
compile time.
May be implemented using function
overloading or operator overloading.
5
RUN TIME POLYMORPHISM
Also known as late binding, dynamic
polymorphism.
Functions are not selected beforehand
but rather are chosen at run time.
A pointer to a base class holds the
address of the object in derived class.
Implemented using virtual functions.
6
REAL LIFE EXAMPLE OF
DYNAMIC POLYMORPHISM
Animal
Make sound
Walk
Cow Mooo
Dog Bow Wow
Donkey Hee haw
7
VIRTUAL FUNCTIONS
 Virtual means something that exists in effect
but not in reality.
 Can be defined in both classes, base as well as
derived.
 It’s name is the same in every class, but
definition can be different.
 In derived classes it will be executed through
pointer of it’s base class.
 Syntax : virtual return_type function_name
8
NEED OF VIRTUAL FUNCTION
If there are member functions with same
name in base class and derived class ,
virtual functions gives programmer
capability to call member function of
different class by a same function call
depending upon different context.
9
DIFFERENCE BETWEEN FUNCTION
REDEFINITON AND OVERRIDING
FUNCTION REDEFINITION
 A redefined function is a
method in a derived class that
has a different definition than
a non-virtual function in a base
class.
 Since the method is not virtual,
the compiler chooses which
function to call based upon the
static type of the object
reference rather than the actual
type of the object.
FUNCTION OVERIDING
 An overridden function is a
method in a derived class that
has a different definition than
a virtual function in a base
class.
 The compiler chooses which
function is desired based upon
the type of the object being
used to call the function.
10
PURE VIRTUAL FUNCTION
 Does not have a function definition
 Can be declared in two ways
Function does not contain a body
Function may be equated to zero
 Syntax : virtual return_type function_name() { }
or
virtual return_type function_name()=0;
11
ABSTRACT CLASS
 Class which contains only pure virtual
functions
 Cannot be used to declare an object of its
known
12
VIRTUAL DESTRUCTOR
 Making base class destructor virtual guarantees that the
object of derived class is destructed properly, i.e., both
base class and derived class destructors are called.
 Virtual destructor is preceded by the keyword ‘virtual’.
 It should not be pure virtual.
 Syntax : virtual ~ destructor_name
13
14

Dynamic Polymorphism in C++

  • 1.
  • 2.
    POLYMORPHISM POLY MORPH POLYMORPHISM Polymeans Many. Morph means Forms. 2
  • 3.
    3 • In schoolbehaves as student. • In bus behaves as passenger. • At malls behaves as customer. • At home behaves as son. REAL LIFE EXAMPLE
  • 4.
    TYPES OF POLYMORPHISM POLYMORPHISM COMPILETIME FUNCTION OVERLOADING OPERATOR OVERLOADING RUN TIME VIRTUAL FUNCTIONS 4
  • 5.
    COMPILE TIME POLYMORPHISM Alsocalled early binding, static binding or static linkage. An object is bound to its function call at compile time. May be implemented using function overloading or operator overloading. 5
  • 6.
    RUN TIME POLYMORPHISM Alsoknown as late binding, dynamic polymorphism. Functions are not selected beforehand but rather are chosen at run time. A pointer to a base class holds the address of the object in derived class. Implemented using virtual functions. 6
  • 7.
    REAL LIFE EXAMPLEOF DYNAMIC POLYMORPHISM Animal Make sound Walk Cow Mooo Dog Bow Wow Donkey Hee haw 7
  • 8.
    VIRTUAL FUNCTIONS  Virtualmeans something that exists in effect but not in reality.  Can be defined in both classes, base as well as derived.  It’s name is the same in every class, but definition can be different.  In derived classes it will be executed through pointer of it’s base class.  Syntax : virtual return_type function_name 8
  • 9.
    NEED OF VIRTUALFUNCTION If there are member functions with same name in base class and derived class , virtual functions gives programmer capability to call member function of different class by a same function call depending upon different context. 9
  • 10.
    DIFFERENCE BETWEEN FUNCTION REDEFINITONAND OVERRIDING FUNCTION REDEFINITION  A redefined function is a method in a derived class that has a different definition than a non-virtual function in a base class.  Since the method is not virtual, the compiler chooses which function to call based upon the static type of the object reference rather than the actual type of the object. FUNCTION OVERIDING  An overridden function is a method in a derived class that has a different definition than a virtual function in a base class.  The compiler chooses which function is desired based upon the type of the object being used to call the function. 10
  • 11.
    PURE VIRTUAL FUNCTION Does not have a function definition  Can be declared in two ways Function does not contain a body Function may be equated to zero  Syntax : virtual return_type function_name() { } or virtual return_type function_name()=0; 11
  • 12.
    ABSTRACT CLASS  Classwhich contains only pure virtual functions  Cannot be used to declare an object of its known 12
  • 13.
    VIRTUAL DESTRUCTOR  Makingbase class destructor virtual guarantees that the object of derived class is destructed properly, i.e., both base class and derived class destructors are called.  Virtual destructor is preceded by the keyword ‘virtual’.  It should not be pure virtual.  Syntax : virtual ~ destructor_name 13
  • 14.