Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism

Compile Time versus Runtime Polymorphism

C++ Object Oriented Programming

Question: 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

 

 What is compile time 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;

}

Output

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.

 

What is runtime polymorphism ?

• 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();

s2‒>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.

 

Difference between Compiler time polymorphism and Run time polymorphism


Compile time polymorphism

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.

Run time polymorphism

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.

 

Review Question

1. Explain the difference between Compile time polymorphism and Run time polymorphism. Also give the implementation of each type in C++.

 

Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism : Tag: Oops, Computer Programming : C++ Object Oriented Programming - Compile Time versus Runtime Polymorphism


Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation