Question: 1. Explain the difference between Compile time polymorphism and Run time polymorphism. Also give the implementation of each type in C++.
Compile
Time Versus Runtime Polymorphism
•
The compile time polymorphism is a mechanism in which the compiler decides
which function to call at compile time. The compile time polymorphism is
achieved using function overloading, operator overloading and templates.
•
Following C++ program implements the compile time polymorphism
#include <iostream>
using namespace std;
class CompileTimePoly {
public:
int
add(int a,int b){
return a+b;
}
int add(int a,int b,int c){
return
a+b+c;
}
};
int main(){
CompileTimePoly
obj;
cout<<obj.add(10,20)<<endl;
cout<<obj.add(10,20,30);
return 0;
}
30
60
Code Explanation :
In above code,
The
compiler inspects the number of arguments passed to the add function and then decides which add function to call. There is no runtime decision.
•
The runtime polymorphism is a mechanism in which the decision of implementation
of function is made at runtime. The runtime achieved using virtual function and pointers to base class.
•
Following is a C++ program that implements runtime polymorphism.
#include <iostream>
using namespace std;
class Shape {
public:
virtual
void draw(){
cout<<"Generic
Shape\n";
}
};
class Circle: public Shape {
public:
void
draw() {
cout<<"This
is circle\n";
}
};
class Rectangle: public Shape {
public:
void draw(){
cout<<"This
is Rectangle\n";
}
};
int main(){
Circle c;
Rectangle
r;
Shape *s1
= &c;
Shape *s2
= &r;
s1‒>draw();
82‒>draw();
return 0;
}
Output
This is circle
This is Rectangle
Code Explanation :
In above program,
The
s1 and s2 are the pointers to the base class. The actual object types i.e.
object of circle and object of rectangle is known at runtime because draw() is virtual. The program
dispatches the appropriate derived draw()
at runtime.

1.
Compile time polymorphism means the function call is resolved by the compiler
at the time of compilation.
2.
It is called static polymorphism or early binding.
3.
It is achieved using function or operator overloading.
4.
The virtual keyword is not required for function definition.
5.
It provides less flexibility because all calls must be known at compile time.
6.
It is generally faster as there is no runtime overload.
1.
Run time polymorphism means the function call is resolved only when the program
is running.
2.
It is also called as dynamic polymorphism or late binding.
3.
It is achieved using virtual functions, and function overriding.
4.
The virtual function is required for function definition.
5.
It provides more flexibility because the behaviour depends on the object type
created at runtime.
6.
It is slightly slower due to dynamic dispatch.
1. Explain the
difference between Compile time polymorphism and Run time polymorphism. Also
give the implementation of each type in C++.
Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism : Tag: Data Structure, C++, C Programing : C++ Program - Compile Time Versus Runtime Polymorphism
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Electron Devices
EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Circuits and Network Analysis
EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Engineering Drawing - Laboratory
ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus - Laboratory
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Devices and Circuits Laboratory
EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation