Course Name: Object Oriented Programming & Data Structures
Course In-charge: : Prof. VIVEK D. DESHMUKH
Course Seminar on
“Comparison between runtime polymorphism and compile time
polymorphism”
Presented by
CHAITALI UKE
What is Polymorphism
Polymorphism :-
 Poly means ‘Many’
 Morphism means ‘ Forms’
 The word polymorphism means having many forms. In simple words, we
can define polymorphism as the ability of a message to be displayed in
more than one form
Person
Son Student Player
For Example
 + Operator is used for Addition of two integer
For Example:
10 + 20 = 30
 It is also used for joining Two Strings.
For Example:
“key” + “board” = keyboard
Type of Polymorphism
 In C++ polymorphism is mainly divided into two types:
1] Compile time Polymorphism
2] Runtime Polymorphism
Polymorphism
Compile time
Function
Overloading
Operator
Overloading
Virtual functions
Runtime
Type of Polymorphism
Compile time polymorphism
 This type of polymorphism is achieved by function overloading or operator
overloading
Function Overloading: When there are multiple functions with same name but
different parameters then these functions are said to be overloaded.
.
Operator Overloading:
 C++ allows you to specify more than one definition for an operator in the same
scope, which is called operator overloading.
 You can redefine or overload most of the built-in operators available in C++.
 Operator that are not overloaded are follows –
• scope operator (::)
• sizeof
• Member selector (.)
• Member pointer selector (*)
• Ternary operator (?)
 This type of polymorphism is achieved by Function Overriding.
 Virtual Function is a member function which is declared within base class
and is re-defined (overridden) by derived class.
 Function overriding on the other hand occurs when a derived class has a
definition for one of the member functions of the base class. That base
function is said to be overridden.
Runtime polymorphism
// C++ program for function overloading
#include<iostream>
using namespace std;
class program
{
public:
void func(int x)
{
cout << "value of x is " <<
x << endl;
}
void func(double x)
{
cout << "value of x is " <<
x << endl;
}
}
void func(int x, int y)
{
cout << "value of x and y
is " << x << ", " << y << endl;
}
};
int main() {
program obj1;
obj1.func(7);
obj1.func(9.132);
obj1.func(85,64);
return 0;
}
Output:
value of x is 7
value of x is 9.132
value of x and y is 85, 64
//Runtime Polymorphism using Virtual Function:
#include<iostream>
using namespace std;
class Base{
public:
virtual void print(){
cout<<"n
Called Print function in Base Classn";
}
void show(){
cout<<"n
Called Show function in Base Classn";
}
};
class Derived : public Base{
public:
void print(){
cout<<"n
Called Print function in Derived Classn";
}
void show(){
cout<<"n
Called Show function in Derived Classn";
}
};
int main(){
Base *bptr;
Derived D1;
bptr = &D1;
bptr->print();
bptr->show();
return 0;
}
Output
Called Print function in Derived Class
Called Show function in Base Class
 The following table demonstrates the difference between runtime polymorphism
and compile-time polymorphism:
Sr.
No
Compile Time Polymorphism Run time Polymorphism
1
In Compile time Polymorphism, the call is
resolved by the compiler.
In Run time Polymorphism, the call is not resolved
by the compiler.
2
It is also known as Static binding, Early
binding and overloading as well.
It is also known as Dynamic binding, Late binding
and overriding as well.
3
Method overloading is the compile-time
polymorphism where more than one
methods share the same name with different
parameters or signature and different return
type.
Method overriding is the runtime polymorphism
having same method with same parameters or
signature, but associated in different classes.
4
It is achieved by function overloading and
operator overloading.
It is achieved by virtual functions and pointers.
5
It provides fast execution because the method
that needs to be executed is known early at
the compile time.
It provides slow execution as compare to early
binding because the method that needs to be
executed is known at the runtime.
THANK YOU
❋
❋
❋
❋ ❋ ❋
┇
┇

Comparison between runtime polymorphism and compile time polymorphism

  • 1.
    Course Name: ObjectOriented Programming & Data Structures Course In-charge: : Prof. VIVEK D. DESHMUKH Course Seminar on “Comparison between runtime polymorphism and compile time polymorphism” Presented by CHAITALI UKE
  • 2.
    What is Polymorphism Polymorphism:-  Poly means ‘Many’  Morphism means ‘ Forms’  The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form
  • 3.
  • 4.
    For Example  +Operator is used for Addition of two integer For Example: 10 + 20 = 30  It is also used for joining Two Strings. For Example: “key” + “board” = keyboard
  • 5.
    Type of Polymorphism In C++ polymorphism is mainly divided into two types: 1] Compile time Polymorphism 2] Runtime Polymorphism
  • 6.
  • 7.
    Compile time polymorphism This type of polymorphism is achieved by function overloading or operator overloading Function Overloading: When there are multiple functions with same name but different parameters then these functions are said to be overloaded. .
  • 8.
    Operator Overloading:  C++allows you to specify more than one definition for an operator in the same scope, which is called operator overloading.  You can redefine or overload most of the built-in operators available in C++.  Operator that are not overloaded are follows – • scope operator (::) • sizeof • Member selector (.) • Member pointer selector (*) • Ternary operator (?)
  • 9.
     This typeof polymorphism is achieved by Function Overriding.  Virtual Function is a member function which is declared within base class and is re-defined (overridden) by derived class.  Function overriding on the other hand occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden. Runtime polymorphism
  • 10.
    // C++ programfor function overloading #include<iostream> using namespace std; class program { public: void func(int x) { cout << "value of x is " << x << endl; } void func(double x) { cout << "value of x is " << x << endl; } } void func(int x, int y) { cout << "value of x and y is " << x << ", " << y << endl; } }; int main() { program obj1; obj1.func(7); obj1.func(9.132); obj1.func(85,64); return 0; } Output: value of x is 7 value of x is 9.132 value of x and y is 85, 64
  • 11.
    //Runtime Polymorphism usingVirtual Function: #include<iostream> using namespace std; class Base{ public: virtual void print(){ cout<<"n Called Print function in Base Classn"; } void show(){ cout<<"n Called Show function in Base Classn"; } }; class Derived : public Base{ public: void print(){ cout<<"n Called Print function in Derived Classn"; } void show(){ cout<<"n Called Show function in Derived Classn"; } }; int main(){ Base *bptr; Derived D1; bptr = &D1; bptr->print(); bptr->show(); return 0; } Output Called Print function in Derived Class Called Show function in Base Class
  • 12.
     The followingtable demonstrates the difference between runtime polymorphism and compile-time polymorphism: Sr. No Compile Time Polymorphism Run time Polymorphism 1 In Compile time Polymorphism, the call is resolved by the compiler. In Run time Polymorphism, the call is not resolved by the compiler. 2 It is also known as Static binding, Early binding and overloading as well. It is also known as Dynamic binding, Late binding and overriding as well. 3 Method overloading is the compile-time polymorphism where more than one methods share the same name with different parameters or signature and different return type. Method overriding is the runtime polymorphism having same method with same parameters or signature, but associated in different classes. 4 It is achieved by function overloading and operator overloading. It is achieved by virtual functions and pointers. 5 It provides fast execution because the method that needs to be executed is known early at the compile time. It provides slow execution as compare to early binding because the method that needs to be executed is known at the runtime.
  • 13.