Function Overloading
·
Multiple
functions à the same name but with different
parameters (different type, number, or order of parameters).
·
A
form of polymorphism (specifically compile-time polymorphism),
·
Increase
code readability and reusability by a function.
Rules for Function Overloading:
- Functions
must have the same name but different parameter lists.
- The
return type is not considered for overloading.
- Overloaded
functions must differ in:
- Number
of parameters.
- Type
of parameters.
- Order
of parameters
(if types differ).
4.
Compile-Time
Resolution: call
based on the arguments at compile time.
- Scope: same scope (e.g., same
namespace or class).
Syntax
return_type
function_name(parameter_list_1) {
// Implementation 1
}
return_type
function_name(parameter_list_2) {
// Implementation 2
}
Examples :-
1. Overloading by Number of
Parameters
#include <iostream>
using namespace std;
void print(int x) {
cout << "Integer: " << x << endl;
}
void print(int x, int y) {
cout << "Two Integers: " << x << " and
" << y << endl;
}
int main() {
print(5); // Calls
print(int)
print(5, 10); // Calls
print(int, int)
return 0;
}
Output
Integer: 5
Two Integers: 5 and 10
2. Overloading by Type of
Parameters
#include <iostream>
using namespace std;
void display(int x) {
cout << "Integer: " << x << endl;
}
void display(double x) {
cout << "Double: " << x << endl;
}
int main() {
display(5); // Calls
display(int)
display(5.5); // Calls
display(double)
return 0;
}
Output
Integer: 5
Double: 5.5
3. Overloading by Order of
Parameters
#include <iostream>
using namespace std;
void show(int x, char c) {
cout << "Int then Char: " << x << "
" << c << endl;
}
void show(char c, int x) {
cout << "Char then Int: " << c << "
" << x << endl;
}
int main() {
show(10, 'A'); // Calls
show(int, char)
show('B', 20); // Calls
show(char, int)
return 0;
}
Output
Int then Char: 10 A
Char then Int: B 20
4. Combining Number and Type
#include <iostream>
using namespace std;
int add(int a, int b) {
return a + b;
}
double add(double a, double b, double c) {
return a + b + c;
}
int main() {
cout << "Sum of 2 ints: " << add(3, 4) <<
endl; // Calls add(int, int)
cout << "Sum of 3 doubles: " << add(1.5, 2.5, 3.5)
<< endl; // Calls add(double, double, double)
return 0;
}
Output
Sum of 2 ints: 7
Sum of 3 doubles: 7.5
Advantages of Function Overloading
- Readability: A single function name can
represent related operations.
- Flexibility: Same function name works
with different inputs.
- Consistency: Reduces the need for
multiple distinct function names.
Disadvantages of Function Overloading
- Complexity: Too many overloads can make
code harder to maintain.
- Ambiguity
Risk: Poor
design can lead to compiler errors or unexpected behavior.
- Compile-Time
Overhead: The
compiler must resolve the correct function, though this doesn’t affect
runtime.
Real-World Use Cases
- Mathematical
Operations:
add(int, int) and add(double, double) for different numeric types.
- Output
Functions:
Like cout << overloading for int, string, etc.
- Constructors
in Classes: A
class can have multiple constructors with different parameters (a special
case of overloading).
==============================================================
0 Comments