SlideShare a Scribd company logo
Submitted by
Kiran Patel
What Is Polymorphism
Polymorphism means ‘one name, multiple forms’.
Polymorphism is one of the crucial features of OOP.
polymorphism
COMPLIE TIME
(EARLY BINDING OR
STSTIC BINDING)
Function
overloading
Operator
overloading
RUNTIME
(DYNAMIC
BINDING)
Virtual
function
Compile Time Polymorphism:-
For every function call compiler binds the call to one
function definition at compile time. This decision of
binding among several functions is taken by considering
formal arguments of the function, their data type and their
sequence.
There are two type of compile time polymorphism.
Function overloading.
Operator overloading
– Overloading:-
• Two or more methods with different
signatures.
– Overriding:-
• Replacing an inherited method with
another having the same signature.
Example of Function overloading
/* C++ Program to return absolute value of variable types
integer and float using function overloading */
#include <iostream>
using namespace std;
int absolute(int);
float absolute(float);
int main() {
int a = -5;
float b = 5.5;
cout<<"Absolute value of "<<a<<" =
"<<absolute(a)<<endl;
cout<<"Absolute value of "<<b<<" = "<<absolute(b);
return 0;
}
int absolute(int var) {
if (var < 0)
var = -var;
return var;
}
float absolute(float var){
if (var < 0.0)
var = -var;
return var;
}
Output:-
Absolute value of -5 = 5
Absolute value of 5.5 = 5.5
Run time Polymorphism
C++ allows binding to be delayed till run time. When you
have a function with same name, equal number of arguments
and same data type in same sequence in base class as well
derived class and a function call of form: base_class_type_ptr-
>member_function(args); will always call base class member
function. The keyword virtual on a member function in base
class indicates to the compiler to delay the binding till run
time.
Every class with atleast one virtual function has a vtable that
helps in binding at run time.
Run time polymorphism is also known as Dynamic binding
and Also known as late binding.
– In Dynamic binding Calls to overridden methods are
resolved at execution time, based on the type of object
referenced
Example of run time polymorphism:
class Base
{
public:
virtual void display(int i)
{ cout<<"Base::"<<i; }
};
class Derv: public Base
{
public:
void display(int j)
{ cout<<"Derv::"<<j; }
};
int main()
{
Base *ptr=new Derv;
ptr->display(10);
return 0;
}
Output: Derv::10
C++ concept of  Polymorphism

More Related Content

PPT
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
PPTX
Member Function in C++
PPTX
polymorphism
PPTX
Polymorphism
PDF
Polymorphism
PDF
Constructors and Destructors
PDF
Object-oriented Programming-with C#
PPTX
Polymorphism
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
Member Function in C++
polymorphism
Polymorphism
Polymorphism
Constructors and Destructors
Object-oriented Programming-with C#
Polymorphism

What's hot (20)

PDF
Files and streams
PPTX
Pointers, virtual function and polymorphism
PPTX
Templates in C++
PPTX
Virtual base class
PPTX
Polymorphism in c++(ppt)
PPTX
Encapsulation C++
PPT
Basic concepts of object oriented programming
PPT
Function overloading(c++)
PPTX
Exception handling c++
PPTX
PPTX
Chapter 07 inheritance
PDF
C++ Files and Streams
PPTX
Stream classes in C++
PDF
Classes and objects
PPTX
stack & queue
PPT
Input and output in C++
PPTX
Exception handling
PPTX
Dynamic memory allocation in c++
PPTX
Synchronization hardware
PPT
File handling
Files and streams
Pointers, virtual function and polymorphism
Templates in C++
Virtual base class
Polymorphism in c++(ppt)
Encapsulation C++
Basic concepts of object oriented programming
Function overloading(c++)
Exception handling c++
Chapter 07 inheritance
C++ Files and Streams
Stream classes in C++
Classes and objects
stack & queue
Input and output in C++
Exception handling
Dynamic memory allocation in c++
Synchronization hardware
File handling
Ad

Similar to C++ concept of Polymorphism (20)

PDF
Comparison between runtime polymorphism and compile time polymorphism
PPTX
Polymorphism & Templates
PDF
polymorphism for b.tech iii year students
PPTX
Polymorphism and its types
PDF
polymorphism.pdf
PPTX
Virtual function
PPTX
Presentation on polymorphism in c++.pptx
PDF
Learn C# Programming Polymorphism & Operator Overloading
PDF
2nd puc computer science chapter 8 function overloading
PDF
Vladymyr Bahrii Understanding polymorphism in C++ 16.11.17
PPTX
Polymorphism Using C++
PPTX
C++ first s lide
PPTX
polymorphism and virtual function
PPTX
Polymorphism
PPTX
Polymorphism
PPTX
Polymorphism & inheritence ppt
PPTX
Polymorphism ppt
PPTX
Polymorphism 140527082302-phpapp01
PPTX
07. Virtual Functions
PDF
Polymorphism in Java by Animesh Sarkar
Comparison between runtime polymorphism and compile time polymorphism
Polymorphism & Templates
polymorphism for b.tech iii year students
Polymorphism and its types
polymorphism.pdf
Virtual function
Presentation on polymorphism in c++.pptx
Learn C# Programming Polymorphism & Operator Overloading
2nd puc computer science chapter 8 function overloading
Vladymyr Bahrii Understanding polymorphism in C++ 16.11.17
Polymorphism Using C++
C++ first s lide
polymorphism and virtual function
Polymorphism
Polymorphism
Polymorphism & inheritence ppt
Polymorphism ppt
Polymorphism 140527082302-phpapp01
07. Virtual Functions
Polymorphism in Java by Animesh Sarkar
Ad

More from kiran Patel (11)

PPTX
2017 Union budget of India
PPTX
Database Management System( Normalization)
PPTX
B tree (computer Science)
PPTX
Effort estimation( software Engineering)
PPTX
Thread (Operating System)
PPTX
Library management (use case diagram Software engineering)
PPTX
Brain Computer Interface
PPTX
Artificial Inteligence
PPTX
Smart buckets ppt
PPTX
Linked list using Dynamic Memory Allocation
PPTX
Dynamic memory Allocation in c language
2017 Union budget of India
Database Management System( Normalization)
B tree (computer Science)
Effort estimation( software Engineering)
Thread (Operating System)
Library management (use case diagram Software engineering)
Brain Computer Interface
Artificial Inteligence
Smart buckets ppt
Linked list using Dynamic Memory Allocation
Dynamic memory Allocation in c language

Recently uploaded (20)

PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
composite construction of structures.pdf
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Sustainable Sites - Green Building Construction
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Geodesy 1.pptx...............................................
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
CYBER-CRIMES AND SECURITY A guide to understanding
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
composite construction of structures.pdf
Mechanical Engineering MATERIALS Selection
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
CH1 Production IntroductoryConcepts.pptx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Foundation to blockchain - A guide to Blockchain Tech
Sustainable Sites - Green Building Construction
Internet of Things (IOT) - A guide to understanding
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Geodesy 1.pptx...............................................
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf

C++ concept of Polymorphism

  • 2. What Is Polymorphism Polymorphism means ‘one name, multiple forms’. Polymorphism is one of the crucial features of OOP.
  • 3. polymorphism COMPLIE TIME (EARLY BINDING OR STSTIC BINDING) Function overloading Operator overloading RUNTIME (DYNAMIC BINDING) Virtual function
  • 4. Compile Time Polymorphism:- For every function call compiler binds the call to one function definition at compile time. This decision of binding among several functions is taken by considering formal arguments of the function, their data type and their sequence. There are two type of compile time polymorphism. Function overloading. Operator overloading
  • 5. – Overloading:- • Two or more methods with different signatures. – Overriding:- • Replacing an inherited method with another having the same signature.
  • 6. Example of Function overloading /* C++ Program to return absolute value of variable types integer and float using function overloading */ #include <iostream> using namespace std; int absolute(int); float absolute(float); int main() { int a = -5; float b = 5.5;
  • 7. cout<<"Absolute value of "<<a<<" = "<<absolute(a)<<endl; cout<<"Absolute value of "<<b<<" = "<<absolute(b); return 0; } int absolute(int var) { if (var < 0) var = -var; return var; } float absolute(float var){ if (var < 0.0) var = -var; return var; } Output:- Absolute value of -5 = 5 Absolute value of 5.5 = 5.5
  • 8. Run time Polymorphism C++ allows binding to be delayed till run time. When you have a function with same name, equal number of arguments and same data type in same sequence in base class as well derived class and a function call of form: base_class_type_ptr- >member_function(args); will always call base class member function. The keyword virtual on a member function in base class indicates to the compiler to delay the binding till run time. Every class with atleast one virtual function has a vtable that helps in binding at run time.
  • 9. Run time polymorphism is also known as Dynamic binding and Also known as late binding. – In Dynamic binding Calls to overridden methods are resolved at execution time, based on the type of object referenced
  • 10. Example of run time polymorphism: class Base { public: virtual void display(int i) { cout<<"Base::"<<i; } }; class Derv: public Base { public: void display(int j) { cout<<"Derv::"<<j; } }; int main() { Base *ptr=new Derv; ptr->display(10); return 0; } Output: Derv::10