Compile Time Polymorphism vs Runtime Polymorphism in C++

1. Introduction

Polymorphism, a fundamental concept in C++, can be divided into two types: compile-time polymorphism and runtime polymorphism. Compile-time polymorphism is achieved through features like function overloading and templates, while runtime polymorphism is achieved through inheritance and virtual functions.

2. Key Points

1. Compile-time polymorphism is resolved during the compilation of the program.

2. Runtime polymorphism is resolved during the execution of the program.

3. Function overloading and operator overloading are examples of compile-time polymorphism.

4. Virtual functions and dynamic binding are examples of runtime polymorphism.

3. Differences

Compile-time Polymorphism Runtime Polymorphism
Decided at compile time. Decided at runtime.
Achieved through function overloading and operator overloading. Achieved through virtual functions and dynamic binding.
More efficient, as the decision is made during compilation. Less efficient compared to compile-time, due to runtime decision making.

4. Example

#include <iostream>
using namespace std;

// Compile-time polymorphism - Function Overloading
void print(int i) {
    cout << "Printing int: " << i << endl;
}

void print(double f) {
    cout << "Printing float: " << f << endl;
}

// Runtime polymorphism - Virtual Functions
class Base {
public:
    virtual void show() { cout << "Base class show" << endl; }
};

class Derived : public Base {
public:
    void show() override { cout << "Derived class show" << endl; }
};

int main() {
    // Compile-time polymorphism
    print(5);
    print(5.5);

    // Runtime polymorphism
    Base *b;
    Derived d;
    b = &d;
    b->show();

    return 0;
}

Output:

Printing int: 5
Printing float: 5.5
Derived class show

Explanation:

1. In the function overloading example, print is called with different parameter types, demonstrating compile-time polymorphism.

2. In the virtual functions example, the type of object pointed to by b determines which show method is called, demonstrating runtime polymorphism.

5. When to use?

- Use compile-time polymorphism for efficiency and when the decisions about the executing version of a function can be made at compile time.

- Use runtime polymorphism when you need flexibility and the ability to handle different subclasses dynamically at runtime.

Related C++/CPP Posts:

Difference Between Struct and Class in C++

Difference Between Pointer and Reference in C++

Difference Between null and nullptr in C++

Difference Between Array and Vector in C++

Difference Between const and constexpr in C++

Difference Between List and Vector in C++

Difference Between C and C++

Difference Between Function Overloading and Operator Overloading in C++

Difference Between Array and List in C++

Difference Between a While Loop and a Do-While Loop in C++

Difference Between new and malloc C++

Virtual Function vs Pure Virtual Function in C++

Compile Time Polymorphism vs Runtime Polymorphism in C++

Difference Between Shallow Copy and Deep Copy in C++

Difference Between Stack and Heap in C++

Copy Constructor vs Parameterized Constructor in C++

Comments