C++ (overload resolution with defaults) — which overload is selected and what value of x is printed?\n\n#include<iostream.h>\nclass CuriousTab {\n int x; float y;\npublic:\n void CuriousTabFunction(int = 0, float = 0.00f, char = 'A');\n void CuriousTabFunction(float, int = 10.00, char = 'Z');\n void CuriousTabFunction(char, char, char);\n};\nint main() {\n CuriousTab objCuriousTab;\n objCuriousTab.CuriousTabFunction(10 * 1.0, int(56.0));\n return 0;\n}\nvoid CuriousTab::CuriousTabFunction(int xx, float yy, char zz) {\n x = xx + int(yy);\n cout << "x = " << x << endl;\n}\nvoid CuriousTab::CuriousTabFunction(float xx, int yy, char zz) {\n x = zz + zz;\n y = xx + yy;\n cout << " x = " << x << endl;\n}\nvoid CuriousTab::CuriousTabFunction(char xx, char yy, char zz) {\n x = xx + yy + zz;\n y = float(xx * 2);\n cout << " x = " << x << endl;\n}\n\nWhat 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