0
0
C++programming~5 mins

Function overloading in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is function overloading in C++?
Function overloading allows multiple functions to have the same name but different parameters (type or number). The correct function is chosen based on the arguments used when calling it.
Click to reveal answer
beginner
How does the compiler decide which overloaded function to call?
The compiler looks at the number and types of arguments in the function call and matches them to the function signature that best fits those arguments.
Click to reveal answer
beginner
Can two overloaded functions differ only by return type?
No. Overloaded functions must differ in their parameter list. Return type alone is not enough for overloading.
Click to reveal answer
beginner
Example: What will be the output of this code?<br>
void print(int x) { std::cout << "Int: " << x << std::endl; }
void print(double x) { std::cout << "Double: " << x << std::endl; }
print(5);
print(3.14);
Output:<br>Int: 5<br>Double: 3.14<br>The compiler calls the correct print function based on the argument type.
Click to reveal answer
beginner
Why is function overloading useful?
It lets you use the same function name for similar actions with different data types or numbers of inputs, making code easier to read and maintain.
Click to reveal answer
Which of these is a valid function overloading example?
Avoid func(); int func();
Bint func(int x); double func(int x);
Cvoid func(int x); void func(double x);
Dvoid func(int x); void func(int x);
What happens if you call an overloaded function with arguments that match multiple versions equally well?
ACompiler throws an error due to ambiguity.
BCalls the first declared function.
CCalls the last declared function.
DCalls a random function.
Can default arguments affect function overloading?
AYes, they can cause ambiguity if not used carefully.
BNo, default arguments have no effect on overloading.
CYes, they replace the need for overloading.
DNo, default arguments are only for templates.
Which of these is NOT a valid way to overload a function?
AChanging the number of parameters.
BChanging the order of parameters if types differ.
CChanging the types of parameters.
DChanging only the return type.
What is the main benefit of function overloading?
AMaking code run faster.
BUsing the same function name for different tasks based on input.
CReducing the number of functions in a program.
DAvoiding the use of classes.
Explain function overloading and how the compiler chooses which function to call.
Think about how you pick the right tool for a specific job based on what you need.
You got /3 concepts.
    Describe why function overloading cannot be done by changing only the return type.
    Imagine two identical keys with different colors; color alone doesn't open different doors.
    You got /3 concepts.