Difficulty: Medium
Correct Answer: 12 18
Explanation:
Introduction / Context:
Overload resolution in C++ prefers the best match. Here one overload is int Addition(int,int)
with a default second parameter, and the other is float Addition(int,float)
(declared but not defined). Understanding which is chosen in each call determines the output values and whether the code links successfully.
Given Data / Assumptions:
data.Addition(1)
supplies a single int
argument.data.Addition(3, 4)
supplies two int
arguments.float
overload is declared but never used by the calls as written.
Concept / Approach:
For Addition(1)
, the viable candidate is int,int
with default b=10
. For Addition(3,4)
, the exact match is also int,int
. Both calls therefore select the same overload that returns int
. The computation in that overload is a *= (b + 2)
which means a = a * (b + 2)
and returns the updated a
.
Step-by-Step Solution:
Verification / Alternative check:
If the second call had a float
as the second argument (e.g., 4.0f
), the float
overload would be selected and would require a definition to link. As written, no link error occurs because that overload is never used.
Why Other Options Are Wrong:
Common Pitfalls:
Assuming the mere presence of an undefined overload triggers a link error even if it is not selected by any call site.
Final Answer:
12 18
Discussion & Comments