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.

  • The functions can be overloaded by using a different number of arguments and by using different types of arguments.
  • If two same name and same argument functions just vary in their return type then such function isn’t overloaded.

Example

C++




// 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.

  • Precedence and associativity remain intact in operators.
  • List of operators that cannot be overloaded in C++ are ::, .*, ., ?:
  • Operators = and & are already overloaded in C++, so we should avoid overloading them.

Example

C++




// 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

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++.

Similar Reads

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

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

Run Time Polymorphism

...

Difference Between Compile Time And Run Time Polymorphism

...

Contact Us