Open In App

Difference Between Compile Time And Run Time Polymorphism In C++

In this article, we will discuss the differences between the compile-time and runtime polymorphism in C++.

What is Polymorphism?

Poly means many and morph means forms or shape. Thus the word Polymorphism comes from Greek and it basically means having many forms.



For example,

Your DAD is your father. He’s your mom’s Husband. He’s your grandma’s son as well. Thus here your DAD is exhibiting many forms. He’s a father, son, husband, brother, etc.



In C++, polymorphism can be divided into two types:

  1. Compile-time Polymorphism
  2. Run-time Polymorphism

Compile Time Polymorphism in C++

In compile-time polymorphism, the compiler determines which function or operation to call based on the number, types, and order of arguments. It is also called Static Polymorphism as the function calls are statically binded to its definition.

It is further categorized into two types:

  1. Function Overloading
  2. Operator Overloading

1. Function Overloading

When multiple functions in a class with the same name but different parameters exist, these functions are said to be overloaded.

Example




// C++ Code to illustrate Function Overloading
#include <iostream>
using namespace std;
  
// Function to be overloaded
int add(int x, int y, int z = 0, int w = 0)
{
    return (x + y + z + w);
}
  
int main()
{
    // Passing different number of arguements
    cout << add(10, 20) << endl;
    cout << add(10, 20, 30) << endl;
    cout << add(10, 20, 30, 40) << endl;
  
    return 0;
}

Output
30
60
100

Operator Overloading

We know that, “+” is used for addition or concatenation. Now, if I want to perform my customized operation i.e. upon calling +, I want to print “Hello Geek” or I want to perform subtraction instead of addition. In such cases, we use operator overloading.

Operator overloading is the process of defining different operations for the operator that vary depending on the argument type.

Example




// C++ Code to illustrate Operator Overloading
#include <iostream>
using namespace std;
  
class B {
public:
    int a, b;
  
public:
    int add() { return a + b; }
  
    // overloading + operator for B class
    void operator+(B& obj)
    {
        int val1 = this->a;
        int val2 = obj.a;
        cout << "Output " << val2 - val1 << endl;
    }
};
  
// driver code
int main()
{
    B obj1, obj2;
    obj1.a = 4;
    obj2.a = 17;
    obj1 + obj2;
    return 0;
}

Output
Output 13

Run Time Polymorphism

In run-time polymorphism, the decision of which function to call is determined at runtime based on the actual object type rather than the reference or pointer type. It is also known as Dynamic Polymorphism because the function calls are dynamically bonded at the runtime.

Run Time Polymorphism can be exhibited by:

Method Overriding

Method overriding refers to the process of creating a new definition of a function in a derived class that is already defined inside its base class. Some rules that must be followed while overriding a method are:

Virtual Function

Example




// C++ Code to illustrate Virtual Function
#include <iostream>
using namespace std;
  
// base class
class Base {
public:
    // virtual function
    virtual void print() { cout << "base"; }
};
  
// derived class
class Derived : public Base {
public:
    // method overriding
    void print() override { cout << "derived"; }
};
  
// driver code
int main()
{
    Base* obj = new Derived();
    obj->print();
    delete obj;
    return 0;
}

Output
derived

Difference Between Compile Time And Run Time Polymorphism

Compile-Time Polymorphism

Run-Time Polymorphism

It is also called Static Polymorphism. It is also known as Dynamic Polymorphism.
In compile-time polymorphism, the compiler determines which function or operation to call based on the number, types, and order of arguments. In run-time polymorphism, the decision of which function to call is determined at runtime based on the actual object type rather than the reference or pointer type.
Function calls are statically binded. Function calls are dynamically binded.

Compile-time Polymorphism can be exhibited by:

1. Function Overloading
2. Operator Overloading

Run-time Polymorphism can be exhibited by Function Overriding.

Faster execution rate. Comparatively slower execution rate.
Inheritance in not involved. Involves inheritance.

Article Tags :