Polymorphism slides
Polymorphism slides
Muhammad Farooq
GPGC Swabi
Polymorphism
⚫ The term Polymorphism gets derived
from the Greek word where poly + morphos
Where
⚫ Poly means “many”
⚫ Morphos mens “forms”
Real life example of Polymorphism
TEACHER (School)
SON (Home)
CUSTOME (Shop)
R
All-rounder (Cricket)
Types
⚫ Static (Compile-Time) Polymorphism
⚫ Dynamic (Run-Time) Polymorphism
Static Polymorphism
⚫ The type of polymorphism in which the
compiler knows at compile time which
function to be called
⚫ Function Overloading
⚫ Operator Overloading
Dynamic Polymorphism
⚫ The type of polymorphism in which the
compiler knows at run time which function
to be called
Implementation/Examples
⚫ Virtual Function(function overriding)
Differences b/w compile time and run time polymorphism.
Pointer to Objects
⚫ Pointers are used to access members of the
objects
Syntax
p -> member;
Where
p: is pointer
->: member access operator
Pointers & Inheritance
Reason
⚫ The compiler checks the type of pointer at
the time of function call. The type of
pointer is A. That’s why both time base
class function is called.
⚫ Regardless the type of object it refers
⚫ Regardless the content of pointer i.e
address of object
Solution
⚫ Virtual function
Virtual Function
⚫ A C++ virtual function is a member function in
the base class that you redefine in a derived
class.
⚫ It is declared using the virtual keyword.
⚫ It is used to tell the compiler to perform
dynamic linkage or late binding on the
function.
Dynamic/Runtime Binding
⚫ When the function is made virtual, C++
determines which function is to be invoked at
the runtime based on the type of the object
pointed by the base class pointer.
Explanation
⚫ Virtual function cause late/dynamic binding
⚫ In late binding compiler does not know
which function to call at compile time
rather run time
⚫ Compiler call all the function according to
type of object rather type of pointer
⚫ Moreover function is called according to
content of the pointer
Rules for virtual function
⚫ Virtual functions must be members of some class.
⚫ Virtual functions cannot be static members.
⚫ They are accessed through object pointers.
⚫ They can be a friend of another class.
⚫ A virtual function must be defined in the base class,
even though it is not used.
⚫ The prototypes of a virtual function of the base class
and all the derived classes must be identical. If the
two functions with the same name but different
prototypes, C++ will consider them as the
overloaded functions.
⚫ We cannot have a virtual constructor, but we can
have a virtual destructor
⚫ Consider the situation when we don't use the virtual
keyword.
Pure Virtual Function
⚫ A virtual function is not used for performing any task.
⚫ It only serves as a placeholder.
⚫ When the function has no definition, such function is
known as "do-nothing" function.
⚫ The "do-nothing" function is known as a pure virtual
function. A pure virtual function is a function declared
in the base class that has no definition relative to the
base class.
⚫ A class containing the pure virtual function cannot be
used to declare the objects of its own, such classes are
known as abstract base classes.
⚫ The main objective of the base class is to provide the
traits to the derived classes and to create the base
pointer used for achieving the runtime polymorphism.
⚫ Pure virtual function can be defined as:
⚫ virtual void display() = 0;
Abstract VS Concrete Class
S.No Abstract Class Concrete Class