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 + 3performs numeric addition (), while"Hello " + "World"performs string concatenation.
4Diagram / Visual Representation
5Working / How It Works
- Function Overloading Rules: Functions share the exact same name, but must differ in:
- The number of parameters.
- The data types of parameters.
- The sequence of parameter types. Note: Differing only by return type is invalid and produces a compilation error.
- Operator Overloading Rules: Achieved using the
operatorkeyword (e.g.,Complex operator + (const Complex& obj)).
6Types / Components / Characteristics
- Operators That CANNOT Be Overloaded in C++:
- Scope Resolution Operator (
::) - Member Selection Operator (
.) - Member Pointer Selection Operator (
.*) - Ternary / Conditional Operator (
?:) sizeofOperator
- Scope Resolution Operator (
7Solved Examples / Problems
Problem Statement: Write a C++ program showcasing both Function Overloading and Operator Overloading.
#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:
Add 2 args: 15
Add 3 args: 30
Complex Sum: 4 + 6i8Advantages, 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
- 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. - MCQ: Which of the following C++ operators CANNOT be overloaded?
- A)
+ - B)
== - C)
:: - D)
<<
Show answer
C):: - A)
- Application Question: Write a function overload set for
area()calculating the area of a circle vs. a rectangle.Show answer
double area(double r)anddouble area(double l, double b).
