Compile-Time Polymorphism in C++

 

Compile-Time Polymorphism 

(Static Polymorphism)

Compile-time polymorphism is the type of polymorphism where the function call is resolved at compile time.

  • The decision is made by the compiler
  • No runtime overhead
  • Faster execution

Achieved Using:

  1. Function Overloading
  2. Operator Overloading

Characteristics of Compile-Time Polymorphism

Feature

Description

Binding

Compile time

Speed

Faster

Flexibility

Less than runtime polymorphism

Keyword Used

No virtual keyword

Examples

Function Overloading, Operator Overloading


1.    FUNCTION OVERLOADING

Function Overloading means multiple functions with the same name but different parameter lists.

Conditions for Overloading

Functions must differ by:

  • Number of parameters
  • Type of parameters
  • Order of parameters

Cannot be overloaded by return type alone.

 

Example: Simple Function Overloading

#include <iostream>

using namespace std;

 

class Calculator {

public:

    int add(int a, int b) {

        return a + b;

    }

 

    float add(float a, float b) {

        return a + b;

    }

 

    int add(int a, int b, int c) {

        return a + b + c;

    }

};

 

int main() {

    Calculator c;

    cout << "Add two integers: " << c.add(10, 20) << endl;

    cout << "Add two floats: " << c.add(5.5f, 2.5f) << endl;

    cout << "Add three integers: " << c.add(1, 2, 3) << endl;

    return 0;

}

Output

Add two integers: 30

Add two floats: 8

Add three integers: 6

===================================================================

2.  OPERATOR OVERLOADING

Operator Overloading allows C++ operators to be redefined for user-defined data types (objects).

Example:

  • + for integers → addition
  • + for objects → programmer-defined meaning

8. Rules of Operator Overloading

Existing operators only
At least one operand must be user-defined
 Cannot overload. , ::, sizeof, ?:

Example : Overloading + Operator

#include <iostream>

using namespace std;

 

class Distance {

    int meter;

public:

    Distance(int m) {

        meter = m;

    }

 

    Distance operator + (Distance d) {

        Distance temp(0);

        temp.meter = meter + d.meter;

        return temp;

    }

 

    void display() {

        cout << "Distance: " << meter << " meters" << endl;

    }

};

 

int main() {

    Distance d1(5), d2(10);

    Distance d3 = d1 + d2;

 

    d3.display();

    return 0;

}

Output

Distance: 15 meters


Compile-Time Polymorphism

  • Function call resolved before execution
  • Compiler matches function signature
  • No dynamic memory or virtual table

Compile-Time vs Run-Time Polymorphism

Feature

Compile-Time

Run-Time

Binding

Compile time

Run time

Keyword

None

virtual

Method

Overloading

Overriding

Speed

Faster

Slower

Flexibility

Less

More


Advantages

Faster execution
Simple implementation
Better readability
Efficient memory usage


Limitations

Less flexible
Cannot change behaviour at runtime

=============================================================== 

Post a Comment

0 Comments