Compile Time Polymorphism in C++ (What is)

What is Compile time Polymorphism in C++

Compile time polymorphism is when the relationship between the function call and the various function definition is decided at the compile time itself.

As we all know polymorphism is characteristic of any given program to behave differently in different situations. For example, you may be a friend to someone and brother to someone and you would behave differently with both of them. Similarly, largely functions in classes behave differently with different different objects that are calling them.

There are two different types we achieve compile time polymorphism in C++ which are –

  • Function Overloading
  • Operator Overloading

Let us understand with an example how each works.

Function Overloading – Compile Time Polymorphism

In Function overloading, we have functions which have the same name but different signatures, i.e. different arguments that are passed with the function. Depending upon the arguments in the function call it is decided that which function declaration will be chosen.

There are two different variation –

  • Change in number of arguments
    • Example Function – prepinsta(int a) and prepinsta(int a, int b)
  • Change in the argument type
    • Example function – prepinsta(int a) and prepinsta(float a)

Function overloading is a concept via which we can achieve compile time polymorphism, that is the function call and the function definition is bounded together by the compiler at compile time itself.

Let us look at an example how this is done –

#include <iostream>
using namespace std;

class PrepInsta
{
  int x, y;
  double z;
  public:
  void myFunction()
  {
    cout <<"No arguments passed here" <<endl;
  }
  void myFunction(int x1)
  {
   x = x1;
    cout << x << " was passed here (int value)" <<endl;
  }
  void myFunction(double x1)
  {
  z = x1;
  cout << z << " was passed here (double value)" <<endl;
  }
  void myFunction(int x1, int y1)
  {
  x = x1;
  y = y1;
  cout << x << " and " << y << " were passed here (int values)" <<endl;
  }
};

int main()
{
PrepInsta obj;
obj.myFunction();
obj.myFunction(10);
obj.myFunction(10.00);
obj.myFunction(10,20);
return 0;
}

Output –

No arguments passed 
here 10 was passed here (int value) 
10 was passed here (double value) 
10 and 20 were passed here (int values)

Operator Overloading – Compile Time Polymorphism

Operator Overloading as the name suggests, is the way through which can alter the behaviour of an operator such as “%” or “&” or  “+” etc and give our own implementation meaning.

We use keyword operator while defining an operator that we have to overload. For Example, if we want to overload operator + to rather than add, subtract two input values we can –

void operator +() 
  { 
    z = x - y; 
  }

Let us implement the same with an example –

#include<iostream> 
using namespace std;

class PrepInsta {
private:
int x;
public:
PrepInsta(int x1 = 0)
{
x = x1;
}

PrepInsta operator + (PrepInsta const &obj) {
PrepInsta temp;
temp.x = x - obj.x;
return temp;
}
void print() { cout << x << endl; }
};

int main()
{
PrepInsta obj1(20), obj2(10);
PrepInsta obj3 = obj1 + obj2;
obj3.print();
}

Output- 

10

You can read more about operator overloading here on this post.

Please Login/Signup to comment