Function overloading lets you create multiple functions with the same name but different inputs. This helps keep your code simple and easy to read.
Function overloading in C++
return_type function_name(parameter_list1) {
// code
}
return_type function_name(parameter_list2) {
// code
}Each function must have a different parameter list (different types or number of parameters).
The return type alone cannot distinguish overloaded functions.
add: one adds integers, the other adds doubles.int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; }
print functions: one for integers, one for strings.void print(int a) { std::cout << "Integer: " << a << std::endl; } void print(std::string s) { std::cout << "String: " << s << std::endl; }
This program shows two multiply functions: one for integers and one for doubles. The right one is called based on the input types.
#include <iostream> #include <string> int multiply(int a, int b) { return a * b; } double multiply(double a, double b) { return a * b; } int main() { std::cout << multiply(3, 4) << std::endl; // calls int version std::cout << multiply(2.5, 4.0) << std::endl; // calls double version return 0; }
Function overloading works by matching the function call with the best parameter list.
If no matching function is found, the compiler will give an error.
Overloading helps keep function names simple and meaningful.
Function overloading allows multiple functions with the same name but different parameters.
It makes code easier to read and use by handling different input types or counts.
The compiler chooses the correct function based on the arguments you provide.
Practice
What is the main purpose of function overloading in C++?
Solution
Step 1: Understand function overloading concept
Function overloading means having multiple functions with the same name but different parameter lists.Step 2: Compare options with definition
Only To allow multiple functions with the same name but different parameters correctly describes this concept. Other options describe different features.Final Answer:
To allow multiple functions with the same name but different parameters -> Option BQuick Check:
Function overloading = same name, different parameters [OK]
- Confusing overloading with operator overloading
- Thinking overloading changes return type only
- Mixing up function overloading with function overriding
Which of the following is a correct function overloading declaration in C++?
void print(int x);
void print(double x);
void print(int x, int y);
void print(int x);
Solution
Step 1: Check for duplicate function signatures
The first and last declarations have the same signature:void print(int x);.Step 2: Understand function overloading rules
Multiple declarations with identical signatures are allowed only if they are declarations, but defining the same function multiple times causes compilation error.Final Answer:
The last declaration causes a compilation error -> Option AQuick Check:
Duplicate function definitions cause errors [OK]
- Thinking return type differentiates overloaded functions
- Thinking duplicate declarations cause compilation errors
- Assuming default parameters allow identical signatures
What will be the output of the following code?
void display(int x) { std::cout << "Int: " << x << std::endl; }
void display(double x) { std::cout << "Double: " << x << std::endl; }
int main() {
display(5);
display(5.5);
return 0;
}Solution
Step 1: Identify which function matches each call
Calldisplay(5)matchesdisplay(int). Calldisplay(5.5)matchesdisplay(double).Step 2: Understand output from each function
First call prints "Int: 5", second prints "Double: 5.5".Final Answer:
Int: 5
Double: 5.5 -> Option DQuick Check:
Matching parameter type calls correct function [OK]
- Assuming implicit conversion causes ambiguity
- Confusing int and double overloads
- Expecting compilation error for valid overloads
Find the error in the following code snippet:
void sum(int a, int b) { std::cout << a + b << std::endl; }
void sum(int a, int b, int c) { std::cout << a + b + c << std::endl; }
int main() {
sum(5, 10);
sum(5, 10, 15);
sum(5, 10, 15, 20);
return 0;
}Solution
Step 1: Check function declarations
Two sum functions exist: one with 2 int parameters, one with 3 int parameters.Step 2: Analyze function calls
Calls with 2 and 3 arguments match existing functions. Call with 4 arguments has no matching function, causing error.Final Answer:
Third call to sum causes compilation error -> Option AQuick Check:
Function call must match declared overloads [OK]
- Assuming extra arguments are ignored
- Thinking default parameters exist without declaration
- Ignoring compilation errors on unmatched calls
Consider the following overloaded functions:
int multiply(int a, int b) { return a * b; }
double multiply(double a, double b) { return a * b; }
int multiply(int a, int b, int c = 1) { return a * b * c; }What will be the output of std::cout << multiply(2, 3) << std::endl;?
Solution
Step 1: Identify overloaded functions with matching signatures
Two functions can match callmultiply(2, 3):int multiply(int, int)andint multiply(int, int, int=1)because default parameter allows 2 arguments.Step 2: Understand ambiguity in overload resolution
Compiler cannot decide which function to call because both are equally good matches, causing ambiguity error.Final Answer:
Compilation error due to ambiguous call -> Option CQuick Check:
Default parameters can cause ambiguous overloads [OK]
- Ignoring ambiguity caused by default arguments
- Assuming first declared function is chosen
- Confusing return type with overload resolution
Ready to prove it?
Pass the test and this pattern moves unknown
