0% found this document useful (0 votes)
24 views

Polymorphism slides

The document explains the concept of polymorphism in programming, derived from the Greek words for 'many forms.' It describes two types of polymorphism: static (compile-time) and dynamic (run-time), along with their implementations such as function and operator overloading for static, and virtual functions for dynamic. Additionally, it covers abstract classes, pure virtual functions, and the rules governing virtual functions in C++.

Uploaded by

msajjadjr1246
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Polymorphism slides

The document explains the concept of polymorphism in programming, derived from the Greek words for 'many forms.' It describes two types of polymorphism: static (compile-time) and dynamic (run-time), along with their implementations such as function and operator overloading for static, and virtual functions for dynamic. Additionally, it covers abstract classes, pure virtual functions, and the rules governing virtual functions in C++.

Uploaded by

msajjadjr1246
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Polymorphism

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)

Naveed QARI (Mosque)

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

⚫ The function definition is bounded to the


function call at compile time

⚫ Memory is allocated at compile time


Implementations/Examples

⚫ Function Overloading
⚫ Operator Overloading
Dynamic Polymorphism
⚫ The type of polymorphism in which the
compiler knows at run time which function
to be called

⚫ The function definition is bounded to the


function call at run time

⚫ Memory is allocated at run time

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

1 Base class contains at least one Provide definition to pure


pure virtual function virtual function in the base
class

2 Can not be create instance Used to create object


(object)

3 Provide interface to sub classes Provide implementation to


super class

4 Provide Generalization Provide Specialization


Concrete Concrete
Class Class
Characteristics
⚫ 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
Upcasting, 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.

You might also like