What is Compile Time Polymorphism in C++

What is Compile time Polymorphism in C++?

Polymorphism means to exist in many forms. There are two types of Compile Time Polymorphism in C++ which are function overloading and operator overloading.

Compile time Polymorphism in C++

Let us understand with an example how each works.

Function Overloading – 

  • 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

Run
#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

We use keyword operator while defining an operator that we have to overload.

There are two types of operator overloading –

  1. Unary Operator overloading
  2. Binary 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

Run
#include 
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

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

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription