











What is Compile Time Polymorphism in C++
What is Compile time Polymorphism in C++?
Polymorphism means to exist in many forms. We have two types of Compile Time Polymorphism in C++ –
- Function Overloading
- Operator Overloading


Let us understand with an example how each works.
Function Overloading –
What is function Overloading
In case we have multiple functions that have same name but, they have different arguments as parameters.
There are two different variations -
1. Change in number of arguments -
Example Function : prepinsta(int a) and prepinsta(int a, int b)
2. Change in the argument type -
Example function : prepinsta(int a) and prepinsta(float a)
There are two different variations -
1. Change in number of arguments -
Example Function : prepinsta(int a) and prepinsta(int a, int b)
2. 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,
- The function call and the function definition is bonded together by the compiler at compile time itself.
Let us look at an example of how this is done –
Code
#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
Issue
Operators don't work with objects. They only work with variables generally.
Example1 - For an object obj, We can't do obj++ to increment all data members in the class.
Example 2 - We can't do obj1 + obj2 to add all data members of two objects.
Example1 - For an object obj, We can't do obj++ to increment all data members in the class.
Example 2 - We can't do obj1 + obj2 to add all data members of two objects.
Solution
Using operator overloading we can use these operators to -
1. To work with objects
2. To alter how these operators may work by providing our own implementation.
Example - We can alter the behaviour of an operator such as "%" or "&" or "+" etc and give our own implementation meaning and work with objects
1. To work with objects
2. To alter how these operators may work by providing our own implementation.
Example - We can alter the behaviour of an operator such as "%" or "&" or "+" etc and give our own implementation meaning and work with objects
We use keyword operator
while defining an operator that we have to overload.
There are two types of operator overloading –
For example, it maybe weird but we can overload operator + to subtract rather than add two values.
void operator +() { z = x - y; }
Let us implement the same with an example –
Code
#include<iostream> using namespace std; class PrepInsta { private: int x; public: // why int x1 = 0 (see explanation at the end of the code) PrepInsta(int x1 = 0){ x = x1; } PrepInsta operator + (PrepInsta const &obj) { PrepInsta temp; // x of obj1 is added with x of obj2 and result it stored in // newly created temp object's x varaible // temp object is returned to be stored in obj3 in main 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(); return 0; } // why PrepInsta(int x1 = 0) in paramterized contructor ? // For obj1(20) & obj2(10) we have directly values 20 & 10 // However, for temp object we didn't use parameterized contructor value // PrepInsta(int x1 = 0) allows us to put default value to 0 // if object is created without parameterized value // However, if parameterized value is passed (as in case of obj1(20) & obj(10)) // these will override default value 0 and place 20 and 10 as values
Output-
30
You can read more about operator overloading here on this post.
Why should you perform overloading
- Naming burden: In some situations where we need to have same functionalities but with a different type of inputs or a different number of inputs, in that situation, if you maintain separate method names for each and every method where functionality is the same the naming burden of remembering this method names increases on the user
Example: In C language we have abs(), sabs(), labs(), fabs() methods, the functionality of all these methods is same, but still the user needs to remind all these method names - Code readability Increases


Quiz Time


Question 1. Compile Time Polymorphism in C++ language are?
- Operator overloading
- Function overloading
- Function overriding
- B Only
- A & B
(Wipro – AMCAT Test)
Operator overloading and Function overloading are the only types of compile time polymorphism in c++ which have early binding as property.
Login/Signup to comment