Overloading in C++
Introduction
Overloading is an important feature of C++ that allows the same name or operator to have different meanings depending on the arguments or operands.
Overloading is a form of compile-time polymorphism.
There are two common types of overloading in C++:
- Function Overloading
- Operator Overloading
1. Function Overloading
Function overloading means creating multiple functions with the same name but different parameter lists.
The functions can differ in:
- Number of parameters
- Type of parameters
- Order of parameter types
Syntax
returnType functionName(parameters); returnType functionName(differentParameters);
Example of Function Overloading
#include <iostream> using namespace std; class Calculator { public: int add(int a, int b) { return a + b; } int add(int a, int b, int c) { return a + b + c; } }; int main() { Calculator c; cout << "Sum of 2 numbers: " << c.add(10, 20) << endl; cout << "Sum of 3 numbers: " << c.add(10, 20, 30) << endl; return 0; }
Output
Sum of 2 numbers: 30 Sum of 3 numbers: 60
The compiler decides which add() function to call based on the arguments.
2. Function Overloading by Different Data Types
Functions can also be overloaded using different parameter types.
Example
#include <iostream> using namespace std; class Display { public: void show(int value) { cout << "Integer: " << value << endl; } void show(double value) { cout << "Double: " << value << endl; } void show(string value) { cout << "String: " << value << endl; } }; int main() { Display d; d.show(10); d.show(10.5); d.show("Anuradha"); return 0; }
Output
Integer: 10 Double: 10.5 String: Anuradha
3. Rules of Function Overloading
Functions can be overloaded when their parameter lists are different.
Valid
void add(int a, int b); void add(int a, int b, int c);
Valid
void show(int x); void show(double x);
Not Valid
Functions cannot be overloaded only by changing the return type.
int add(int a, int b); double add(int a, int b); // Error
The parameter list is identical, so the compiler cannot distinguish these functions.
4. Operator Overloading
Operator overloading allows operators to work with user-defined data types such as classes.
For example, we can define how the + operator should work for two objects.
Common Operators That Can Be Overloaded
+ - * / % == != < > <= >= ++ --
5. Simple Operator Overloading Example
#include <iostream> using namespace std; class Number { public: int value; Number(int v) { value = v; } Number operator+(Number n) { return Number(value + n.value); } }; int main() { Number n1(10); Number n2(20); Number n3 = n1 + n2; cout << "Sum = " << n3.value; return 0; }
Output
Sum = 30
Here:
n1 + n2
calls the overloaded operator+() function.
6. Operator Overloading Using Member Function
An operator can be overloaded as a member function.
Example
#include <iostream> using namespace std; class Point { public: int x; int y; Point(int a, int b) { x = a; y = b; } Point operator+(Point p) { return Point(x + p.x, y + p.y); } }; int main() { Point p1(10, 20); Point p2(30, 40); Point p3 = p1 + p2; cout << "X = " << p3.x << endl; cout << "Y = " << p3.y << endl; return 0; }
Output
X = 40 Y = 60
7. Operator Overloading Using Friend Function
An operator can also be overloaded using a friend function.
Example
#include <iostream> using namespace std; class Number { private: int value; public: Number(int v) { value = v; } friend Number operator+(Number n1, Number n2); void display() { cout << "Sum = " << value << endl; } }; Number operator+(Number n1, Number n2) { return Number(n1.value + n2.value); } int main() { Number n1(10); Number n2(20); Number n3 = n1 + n2; n3.display(); return 0; }
Output
Sum = 30
8. Unary Operator Overloading
A unary operator works with one operand.
Examples:
++ -- - !
Example: Unary ++
#include <iostream> using namespace std; class Number { public: int value; Number(int v) { value = v; } void operator++() { ++value; } void display() { cout << "Value = " << value << endl; } }; int main() { Number n(10); ++n; n.display(); return 0; }
Output
Value = 11
9. Binary Operator Overloading
A binary operator works with two operands.
Examples:
+ - * / ==
Example
Number n3 = n1 + n2;
Here:
n1 → operand 1 + → operator n2 → operand 2
10. Operators That Cannot Be Overloaded
Some operators cannot be overloaded in C++.
Common examples include:
:: . .* ?: sizeof typeid
These operators have fixed language-level meanings.
11. Function Overloading vs Operator Overloading
| Function Overloading | Operator Overloading |
|---|---|
| Same function name with different parameters. | Gives operators additional meaning for user-defined types. |
| Uses function names. | Uses operators such as +, -, ==. |
| Compile-time polymorphism. | Compile-time polymorphism. |
Example: add(int,int) and add(int,int,int). | Example: n1 + n2. |
| Makes functions flexible. | Makes objects work naturally with operators. |
12. Advantages of Overloading
1. Improves Readability
The same function name can be used for related operations.
2. Supports Compile-Time Polymorphism
Overloading is an important form of compile-time polymorphism.
3. Code Reusability
The same interface can handle different types or numbers of arguments.
4. Makes Code Easier to Understand
Operator overloading can make object operations look natural.
5. Reduces Complexity
Related operations can use a common function or operator name.
13. Real-Life Example
Suppose we have different ways to calculate a total:
calculate(10, 20); calculate(10, 20, 30); calculate(10.5, 20.5);
Function overloading allows us to use the same function name:
calculate()
with different parameter lists.
Similarly, operator overloading allows objects such as:
Money m1; Money m2; Money m3 = m1 + m2;
to use familiar operators.
Key Points
- Overloading is a form of compile-time polymorphism.
- Function overloading uses the same function name with different parameter lists.
- Return type alone cannot distinguish overloaded functions.
- Operator overloading gives operators additional meaning for user-defined types.
- Unary operators work with one operand.
- Binary operators work with two operands.
- Some C++ operators cannot be overloaded.
Summary
Overloading | ┌─────────┴─────────┐ ↓ ↓ Function Overloading Operator Overloading | | ↓ ↓ Same function name Same operator different parameters different behavior
Simple Definition
Overloading = Using the same function name or operator for different operations based on the arguments or operands.