Topic 7

Compile-Time Polymorphism (Function & Operator Overloading)

3 min read3 questions to test yourself

1Definition

Compile-Time Polymorphism (also called Static Polymorphism or Early Binding) is a mechanism where the specific function or operator to be invoked is selected at compile time based on parameter signatures or operand types.

2Simple Explanation

The word Polymorphism originates from Greek (Poly = Many, Morph = Forms). Compile-time polymorphism means giving a single function or operator name multiple definitions, and allowing the compiler to pick the right one based on the arguments provided.

3Real-Life Example / Analogy

  • Function Overloading: The word "Cut". If an actor hears "Cut!", they stop acting. If a hairdresser hears "Cut!", they snip hair. If a surgeon hears "Cut!", they make an incision. The context (parameters) determines the action.
  • Operator Overloading: The + operator. 2 + 3 performs numeric addition (55), while "Hello " + "World" performs string concatenation.

4Diagram / Visual Representation

Compile-time polymorphism

Function overloading

Operator overloading

add(int, int)

add(int, int, int)

add(double, double)

Complex c3 = c1 + c2

Invokes operator+()

5Working / How It Works

  • Function Overloading Rules: Functions share the exact same name, but must differ in:
    1. The number of parameters.
    2. The data types of parameters.
    3. The sequence of parameter types. Note: Differing only by return type is invalid and produces a compilation error.
  • Operator Overloading Rules: Achieved using the operator keyword (e.g., Complex operator + (const Complex& obj)).

6Types / Components / Characteristics

  • Operators That CANNOT Be Overloaded in C++:
    1. Scope Resolution Operator (::)
    2. Member Selection Operator (.)
    3. Member Pointer Selection Operator (.*)
    4. Ternary / Conditional Operator (?:)
    5. sizeof Operator

7Solved Examples / Problems

Problem Statement: Write a C++ program showcasing both Function Overloading and Operator Overloading.

C++
#include <iostream>
using namespace std;
class Calculator {
public:
    // Function Overloading
    int add(int a, int b) { return a + b; }
    int add(int a, int b, int c) { return a + b + c; }
};
class Complex {
public:
    int real, imag;
    Complex(int r = 0, int i = 0) : real(r), imag(i) {}
    // Operator Overloading (+)
    Complex operator + (const Complex& obj) {
        return Complex(real + obj.real, imag + obj.imag);
    }
};
int main() {
    Calculator calc;
    cout << "Add 2 args: " << calc.add(5, 10) << endl;
    cout << "Add 3 args: " << calc.add(5, 10, 15) << endl;
    Complex c1(3, 4), c2(1, 2);
    Complex c3 = c1 + c2;
    cout << "Complex Sum: " << c3.real << " + " << c3.imag << "i" << endl;
    return 0;
}

Output:

Output
Add 2 args: 15
Add 3 args: 30
Complex Sum: 4 + 6i

8Advantages, Disadvantages & Applications

  • Advantages:
    • High execution speed due to early static binding at compile time.
    • Improves code readability by keeping method names intuitive.
  • Disadvantages:
    • Cannot handle runtime decision logic based on dynamic user input.
  • Applications: Matrix math libraries, custom string vector classes.

9Important Points to Remember

  • Static binding occurs during compilation; no runtime lookup overhead is incurred.
  • Overloading function return types alone is not permitted in C++.

10Test Your Understanding

  1. Conceptual Question: Can we overload a function by altering only its return type in C++? Explain why.
    Show answer
    No. The compiler cannot determine which function to invoke based solely on the return type when the function is called without assigning its result.
  2. MCQ: Which of the following C++ operators CANNOT be overloaded?
    • A) +
    • B) ==
    • C) ::
    • D) <<
    Show answer
    C) ::
  3. Application Question: Write a function overload set for area() calculating the area of a circle vs. a rectangle.
    Show answer
    double area(double r) and double area(double l, double b).

Finished this topic?

0 of 14 topics read in Object-Oriented Programming

Handwritten notes for the exam

Unit-wise SPPU notes and PYQs for this subject.

Helpful? Share it with your class

Free for everyone, no login needed.

Share topic