C++ (overload resolution with defaults) — which overload is selected and what value of x is printed? #include<iostream.h> class CuriousTab { int x; float y; public: void CuriousTabFunction(int = 0, float = 0.00f, char = 'A'); void CuriousTabFunction(float, int = 10.00, char = 'Z'); void CuriousTabFunction(char, char, char); }; int main() { CuriousTab objCuriousTab; objCuriousTab.CuriousTabFunction(10 * 1.0, int(56.0)); return 0; } void CuriousTab::CuriousTabFunction(int xx, float yy, char zz) { x = xx + int(yy); cout << "x = " << x << endl; } void CuriousTab::CuriousTabFunction(float xx, int yy, char zz) { x = zz + zz; y = xx + yy; cout << " x = " << x << endl; } void CuriousTab::CuriousTabFunction(char xx, char yy, char zz) { x = xx + yy + zz; y = float(xx * 2); cout << " x = " << x << endl; } What is printed?

Difficulty: Medium

Correct Answer: The program will print the output x = 180.

Explanation:


Introduction / Context:
This question examines overload resolution when two viable overloads accept parameters that can be formed from the same call via standard conversions, and where defaults supply missing arguments. It highlights that exact matches and promotions out-rank other standard conversions.


Given Data / Assumptions:

  • Call site: CuriousTabFunction(10 * 1.0, int(56.0)) i.e., arguments are double and int.
  • Two likely candidates: (int, float, char = 'A') and (float, int, char = 'Z').
  • Third overload (char, char, char) is not viable for the given types without narrowing conversions to char.


Concept / Approach:
For the first parameter, both candidates require converting a double (to int or to float). For the second parameter, the (float, int, char) overload has an exact match for the provided int, while the (int, float, char) overload requires converting int to float. Therefore, the chosen overload is CuriousTabFunction(float, int, char) with zz defaulting to 'Z' (ASCII 90).


Step-by-Step Solution:

Select overload: (float, int, char).Compute x = zz + zz = 90 + 90 = 180.The function prints " x = 180".


Verification / Alternative check:
Force the other overload with an explicit cast: obj.CuriousTabFunction(int(10.0), float(56.0)) and the output will reflect x = 10 + 56.


Why Other Options Are Wrong:

  • 65, 66, 130 correspond to other letter sums or different overloads; not chosen here.
  • Compilation failure does not occur; all overloads are valid.


Common Pitfalls:
Overlooking that the second parameter is an exact match for int in the float-int-char overload, which tips the ranking.


Final Answer:
The program will print the output x = 180.

More Questions from Functions

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion