SlideShare a Scribd company logo
POLYMORPHISM
Definition…….
• Polymorphism is a Greek term that means ability to
exist in more than one form. For example, an English
word “bat” can have two meanings in two different
contexts., i.e. a cricket bat and an animal bat.
• Similarly, in Object oriented system, polymorphism can
be in functions as well as in existing operators( with
few exceptions). In broader sense, Polymorphism is of
two types:
• Static Polymorphism
• Dynamic Polymorphism
Static Polymorphism
• Also known as early binding or compile time
polymorphism it’s the ability to bind the
function codes with the function calls
(provided all the functions have same name)
at the compilation time. To implement static
polymorphism, we have the concept of
function overloading & operator overloading.
Dynamic Polymorphism-
• Also known as late binding or run time
polymorphism, it is the ability to bind the
function codes with the function calls
(provided all the functions have same name)
at the program run time. To implement
dynamic polymorphism, we have the concept
of virtual functions.
Polymorphism
Static Polymorphism
Function overloading: It’s a phenomenon for
a function with same name to exist with
different number and types of arguments. Eg.
a function called area() can be with two
arguments for finding area of a rectangle and
the same function area() can be with one
argument for finding area of a circle. The
syntax for both the functions will be:
Syntax:
void area( int l, int b)
{
cout<<”area of rectangle”<< l*b;
}
void area(int r)
{
cout <<” area of circle”<< 3.14*r*r;
}
Explanation….
• When the compiler will come across these
two function calls in main(), it will bind the
function call to area() with two arguments to
first function and function call to area() with
one argument to the second function. Hence,
this is called static binding of function call
with function code that is done at compile
time.
Operator overloading:
• In operator overloading, one operator is
capable of existing in more than one forms
without changing the actual meaning of the
operator. Eg. a “+” operator can be used to
add two numbers as well as can be used to
concatenate two strings or can add two
objects of any class. But in any case it will just
add. Any other operation is invalid.
Exceptions….
Except for some operators, all the operators can be
overloaded.
These exceptions are---
. (Member access or dot operator)
.*(Pointer to member operator)
: :(scope resolution operator)
sizeof( ) operator
?: (ternary operator)
Syntax for defining operator function
The operator can be overloaded through
operator function. Its syntax is:
return type class-name : : operator op (argument list)
{
Function body;
}
Here, the operator function is a member function
of any class so it has to be associated with a class
when defined outside the class. “operator” is the
keyword and “op” is the operator that has to be
overloaded.
An example…..
class xyz
{
int a, b;
public:
void getdata( )
{
cin>>a>>b;
}
void operator –( );
void putdata( )
{
cout <<a<<b;
}
};
void xyz : : operator –( )
{
a = -a;
b = -b;
}
int main( )
{
xyz ob; ob.getdata( );
-ob; // calling the operator
function
ob.putdata( );
return 0;
}
• In the previous slide, the example of
overloading unary minus operator was
shown.
• Above operator function can also be used
with friend function. It is shown as: friend
void operator (xyz &ob);
• Here “&” is the reference operator and it
refers to the object ob of the class xyz.
Pointers to function
• Function pointers are widely used in dynamic
binding.
• The concept of pointer to function acts as a
base for pointers to the members(.*).
• They are used to select a function dynamically
at run time.
• General syntax
• Datatype (*function_name)();
An example
• typedef void (*funptr)
(int,int);
• void add(int i, int j)
• { cout<< i<<"+"
<<j<<"="<<i+j;}
• void subtract( int i, int j)
• {
• cout<< i<<"-"
<<j<<"="<<i-j;
• }
• int main() {
• funptr ptr;
• ptr = &add;
• ptr(1,2);
• cout<<"n";
• ptr =&subtract;
• ptr(3,2);
•
• return 0;
• }
Pointer to object
• Void main()
• {
• Item x,*ptr;
• ptr= &x;
• x.getdata();//ptr-> getdata();
• x.show();// ptr->show();//(*ptr).show()
• }
• The parentheses are necessary because the dot
operators has higher precedence than the
indirection operator *.
This Pointer
A pointer to the calling object is referred to as
this pointer. It is passed as a default argument
to any member function that is called by an
object.
But, we can also refer to the calling object
explicitly in a member function by using this
pointer. Eg.
An example
• class sample
• {
• int i;
• public :
• void getdata( )
• {
• cin>> this -> i;
• }
• void putdata( )
• {
• cout << this ->i;
•
• }
• };
Use of pointer to object
• class item {
• int code;
• float price;
• public:
• void getdata()
• { cout<<"n enter code and price of the item:";
cin>>code>>price;}
• void show()
• { cout<<"n Code:"<<code; cout<<"nPrice:"<<
price;}};int main() { item X, *ptr; ptr= &X; ptr-
>getdata(); ptr->show(); return 0;}
Pointer to the object of base class.
• We always create the pointer to the object of
base class.
• Pointer to the object of base class are type
compatible with pointers to object of a
derived class. i.e a single pointer variable can
be made to point to object belonging to
different class.
Virtual function
• Virtual functions are those which are
preceded by a keyword virtual.
• They help in deciding which function has to be
called at run time depending upon the type of
function pointed by the base pointer.
Pure virtual functions
• Virtual functions with no definition and which are
equated to a zero are called pure virtual
functions. They are a basic feature of an Abstract
class.
• Virtual function defined in a base class is seldom
used for performing any task. They servers as a
place holders.
• Such functions are called do nothing functions or
pure virtual functions.
• Ex. Virtual show()=0;
Pure virtual functions
• Pure virtual functions are used
• if a function doesn't have any use in the base class
• but the function must be implemented by all its derived classes
• Let's take an example,
• Suppose, we have derived Triangle, Square and Circle classes
from the Shape class, and we want to calculate the area of all
these shapes.
• In this case, we can create a pure virtual function
named calculateArea() in the Shape. Since it's a pure virtual
function, all derived classes Triangle, Square and Circle must
include the calculateArea() function with implementation.
Continues…………….
• A pure virtual function doesn't have the
function body and it must end with = 0.
class Shape
{
public:
// creating a pure virtual function
virtual void calculateArea() = 0;
};
Abstract class
• Class with pure virtual function in them are
called Abstract class.

More Related Content

PPTX
Class and object
prabhat kumar
 
PPTX
Inheritance
prabhat kumar
 
PPTX
Inheritance, friend function, virtual function, polymorphism
Jawad Khan
 
PPT
Lecture5
Sunil Gupta
 
PDF
Inheritance
Prof. Dr. K. Adisesha
 
PPTX
Friend functions
Megha Singh
 
PPTX
class and objects
Payel Guria
 
PPTX
Friend function & friend class
Abhishek Wadhwa
 
Class and object
prabhat kumar
 
Inheritance
prabhat kumar
 
Inheritance, friend function, virtual function, polymorphism
Jawad Khan
 
Lecture5
Sunil Gupta
 
Friend functions
Megha Singh
 
class and objects
Payel Guria
 
Friend function & friend class
Abhishek Wadhwa
 

What's hot (20)

PPTX
Oops
Jaya Kumari
 
PDF
Chapter23 friend-function-friend-class
Deepak Singh
 
PPTX
Object as function argument , friend and static function by shahzad younas
Shahzad Younas
 
PPTX
Variables in python
Jaya Kumari
 
PDF
Implementation of oop concept in c++
Swarup Kumar Boro
 
PPTX
Learn Concept of Class and Object in C# Part 3
C# Learning Classes
 
PPTX
+2 CS class and objects
khaliledapal
 
PPT
Friends function and_classes
asadsardar
 
PPTX
Chapter 05 classes and objects
Praveen M Jigajinni
 
PPTX
Static Data Members and Member Functions
MOHIT AGARWAL
 
PPTX
C# classes objects
Dr.Neeraj Kumar Pandey
 
PPTX
Abstract class in c++
Sujan Mia
 
PPTX
Object Oriented Programming Using C++
Muhammad Waqas
 
PPS
Inheritance chepter 7
kamal kotecha
 
PDF
A COMPLETE FILE FOR C++
M Hussnain Ali
 
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
PDF
Java unit2
Abhishek Khune
 
PPTX
Classes, objects in JAVA
Abhilash Nair
 
PPTX
pointers,virtual functions and polymorphism
rattaj
 
PPTX
Dynamic method dispatch
yugandhar vadlamudi
 
Chapter23 friend-function-friend-class
Deepak Singh
 
Object as function argument , friend and static function by shahzad younas
Shahzad Younas
 
Variables in python
Jaya Kumari
 
Implementation of oop concept in c++
Swarup Kumar Boro
 
Learn Concept of Class and Object in C# Part 3
C# Learning Classes
 
+2 CS class and objects
khaliledapal
 
Friends function and_classes
asadsardar
 
Chapter 05 classes and objects
Praveen M Jigajinni
 
Static Data Members and Member Functions
MOHIT AGARWAL
 
C# classes objects
Dr.Neeraj Kumar Pandey
 
Abstract class in c++
Sujan Mia
 
Object Oriented Programming Using C++
Muhammad Waqas
 
Inheritance chepter 7
kamal kotecha
 
A COMPLETE FILE FOR C++
M Hussnain Ali
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
Java unit2
Abhishek Khune
 
Classes, objects in JAVA
Abhilash Nair
 
pointers,virtual functions and polymorphism
rattaj
 
Dynamic method dispatch
yugandhar vadlamudi
 
Ad

Similar to Polymorphism (20)

PPT
Basics of cpp
vinay chauhan
 
PPTX
OOPS & C++(UNIT 4)
Dr. SURBHI SAROHA
 
PPTX
11.C++Polymorphism [Autosaved].pptx
AtharvPotdar2
 
PDF
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
ManiMala75
 
PPTX
Presentation on polymorphism in c++.pptx
vishwadeep15
 
PDF
Function in C++
Prof Ansari
 
PDF
OOP_UnitIII.pdf
SuyogSabale1
 
PPTX
Learn more about the concepts Functions of Python
PrathamKandari
 
PPTX
Functions_new.pptx
Yagna15
 
PPTX
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
tony8553004135
 
PDF
Python functions
Prof. Dr. K. Adisesha
 
KEY
Programming with Python - Week 3
Ahmet Bulut
 
PPTX
CH.4FUNCTIONS IN C (1).pptx
sangeeta borde
 
PDF
3-Python Functions.pdf in simple.........
mxdsnaps
 
PPT
Unit ii
snehaarao19
 
PPT
C++ - Constructors,Destructors, Operator overloading and Type conversion
Hashni T
 
PDF
Functions-.pdf
arvdexamsection
 
PDF
Intro to JavaScript - Week 2: Function
Jeongbae Oh
 
PPTX
UNIT 3 python.pptx
TKSanthoshRao
 
Basics of cpp
vinay chauhan
 
OOPS & C++(UNIT 4)
Dr. SURBHI SAROHA
 
11.C++Polymorphism [Autosaved].pptx
AtharvPotdar2
 
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
ManiMala75
 
Presentation on polymorphism in c++.pptx
vishwadeep15
 
Function in C++
Prof Ansari
 
OOP_UnitIII.pdf
SuyogSabale1
 
Learn more about the concepts Functions of Python
PrathamKandari
 
Functions_new.pptx
Yagna15
 
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
tony8553004135
 
Python functions
Prof. Dr. K. Adisesha
 
Programming with Python - Week 3
Ahmet Bulut
 
CH.4FUNCTIONS IN C (1).pptx
sangeeta borde
 
3-Python Functions.pdf in simple.........
mxdsnaps
 
Unit ii
snehaarao19
 
C++ - Constructors,Destructors, Operator overloading and Type conversion
Hashni T
 
Functions-.pdf
arvdexamsection
 
Intro to JavaScript - Week 2: Function
Jeongbae Oh
 
UNIT 3 python.pptx
TKSanthoshRao
 
Ad

Recently uploaded (20)

PPTX
Inventory management chapter in automation and robotics.
atisht0104
 
PPT
Ppt for engineering students application on field effect
lakshmi.ec
 
PDF
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
PDF
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
The Asian School
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
easa module 3 funtamental electronics.pptx
tryanothert7
 
PPT
1. SYSTEMS, ROLES, AND DEVELOPMENT METHODOLOGIES.ppt
zilow058
 
PDF
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
PDF
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
PDF
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
PDF
Software Testing Tools - names and explanation
shruti533256
 
PPTX
Information Retrieval and Extraction - Module 7
premSankar19
 
PDF
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
PDF
July 2025: Top 10 Read Articles Advanced Information Technology
ijait
 
PDF
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
PDF
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
PDF
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
PPTX
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
PDF
JUAL EFIX C5 IMU GNSS GEODETIC PERFECT BASE OR ROVER
Budi Minds
 
Inventory management chapter in automation and robotics.
atisht0104
 
Ppt for engineering students application on field effect
lakshmi.ec
 
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
The Asian School
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
easa module 3 funtamental electronics.pptx
tryanothert7
 
1. SYSTEMS, ROLES, AND DEVELOPMENT METHODOLOGIES.ppt
zilow058
 
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
Software Testing Tools - names and explanation
shruti533256
 
Information Retrieval and Extraction - Module 7
premSankar19
 
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
July 2025: Top 10 Read Articles Advanced Information Technology
ijait
 
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
JUAL EFIX C5 IMU GNSS GEODETIC PERFECT BASE OR ROVER
Budi Minds
 

Polymorphism

  • 2. Definition……. • Polymorphism is a Greek term that means ability to exist in more than one form. For example, an English word “bat” can have two meanings in two different contexts., i.e. a cricket bat and an animal bat. • Similarly, in Object oriented system, polymorphism can be in functions as well as in existing operators( with few exceptions). In broader sense, Polymorphism is of two types: • Static Polymorphism • Dynamic Polymorphism
  • 3. Static Polymorphism • Also known as early binding or compile time polymorphism it’s the ability to bind the function codes with the function calls (provided all the functions have same name) at the compilation time. To implement static polymorphism, we have the concept of function overloading & operator overloading.
  • 4. Dynamic Polymorphism- • Also known as late binding or run time polymorphism, it is the ability to bind the function codes with the function calls (provided all the functions have same name) at the program run time. To implement dynamic polymorphism, we have the concept of virtual functions.
  • 6. Static Polymorphism Function overloading: It’s a phenomenon for a function with same name to exist with different number and types of arguments. Eg. a function called area() can be with two arguments for finding area of a rectangle and the same function area() can be with one argument for finding area of a circle. The syntax for both the functions will be:
  • 7. Syntax: void area( int l, int b) { cout<<”area of rectangle”<< l*b; } void area(int r) { cout <<” area of circle”<< 3.14*r*r; }
  • 8. Explanation…. • When the compiler will come across these two function calls in main(), it will bind the function call to area() with two arguments to first function and function call to area() with one argument to the second function. Hence, this is called static binding of function call with function code that is done at compile time.
  • 9. Operator overloading: • In operator overloading, one operator is capable of existing in more than one forms without changing the actual meaning of the operator. Eg. a “+” operator can be used to add two numbers as well as can be used to concatenate two strings or can add two objects of any class. But in any case it will just add. Any other operation is invalid.
  • 10. Exceptions…. Except for some operators, all the operators can be overloaded. These exceptions are--- . (Member access or dot operator) .*(Pointer to member operator) : :(scope resolution operator) sizeof( ) operator ?: (ternary operator)
  • 11. Syntax for defining operator function The operator can be overloaded through operator function. Its syntax is: return type class-name : : operator op (argument list) { Function body; } Here, the operator function is a member function of any class so it has to be associated with a class when defined outside the class. “operator” is the keyword and “op” is the operator that has to be overloaded.
  • 12. An example….. class xyz { int a, b; public: void getdata( ) { cin>>a>>b; } void operator –( ); void putdata( ) { cout <<a<<b; } }; void xyz : : operator –( ) { a = -a; b = -b; } int main( ) { xyz ob; ob.getdata( ); -ob; // calling the operator function ob.putdata( ); return 0; }
  • 13. • In the previous slide, the example of overloading unary minus operator was shown. • Above operator function can also be used with friend function. It is shown as: friend void operator (xyz &ob); • Here “&” is the reference operator and it refers to the object ob of the class xyz.
  • 14. Pointers to function • Function pointers are widely used in dynamic binding. • The concept of pointer to function acts as a base for pointers to the members(.*). • They are used to select a function dynamically at run time. • General syntax • Datatype (*function_name)();
  • 15. An example • typedef void (*funptr) (int,int); • void add(int i, int j) • { cout<< i<<"+" <<j<<"="<<i+j;} • void subtract( int i, int j) • { • cout<< i<<"-" <<j<<"="<<i-j; • } • int main() { • funptr ptr; • ptr = &add; • ptr(1,2); • cout<<"n"; • ptr =&subtract; • ptr(3,2); • • return 0; • }
  • 16. Pointer to object • Void main() • { • Item x,*ptr; • ptr= &x; • x.getdata();//ptr-> getdata(); • x.show();// ptr->show();//(*ptr).show() • } • The parentheses are necessary because the dot operators has higher precedence than the indirection operator *.
  • 17. This Pointer A pointer to the calling object is referred to as this pointer. It is passed as a default argument to any member function that is called by an object. But, we can also refer to the calling object explicitly in a member function by using this pointer. Eg.
  • 18. An example • class sample • { • int i; • public : • void getdata( ) • { • cin>> this -> i; • } • void putdata( ) • { • cout << this ->i; • • } • };
  • 19. Use of pointer to object • class item { • int code; • float price; • public: • void getdata() • { cout<<"n enter code and price of the item:"; cin>>code>>price;} • void show() • { cout<<"n Code:"<<code; cout<<"nPrice:"<< price;}};int main() { item X, *ptr; ptr= &X; ptr- >getdata(); ptr->show(); return 0;}
  • 20. Pointer to the object of base class. • We always create the pointer to the object of base class. • Pointer to the object of base class are type compatible with pointers to object of a derived class. i.e a single pointer variable can be made to point to object belonging to different class.
  • 21. Virtual function • Virtual functions are those which are preceded by a keyword virtual. • They help in deciding which function has to be called at run time depending upon the type of function pointed by the base pointer.
  • 22. Pure virtual functions • Virtual functions with no definition and which are equated to a zero are called pure virtual functions. They are a basic feature of an Abstract class. • Virtual function defined in a base class is seldom used for performing any task. They servers as a place holders. • Such functions are called do nothing functions or pure virtual functions. • Ex. Virtual show()=0;
  • 23. Pure virtual functions • Pure virtual functions are used • if a function doesn't have any use in the base class • but the function must be implemented by all its derived classes • Let's take an example, • Suppose, we have derived Triangle, Square and Circle classes from the Shape class, and we want to calculate the area of all these shapes. • In this case, we can create a pure virtual function named calculateArea() in the Shape. Since it's a pure virtual function, all derived classes Triangle, Square and Circle must include the calculateArea() function with implementation.
  • 24. Continues……………. • A pure virtual function doesn't have the function body and it must end with = 0. class Shape { public: // creating a pure virtual function virtual void calculateArea() = 0; };
  • 25. Abstract class • Class with pure virtual function in them are called Abstract class.