C++ overload resolution on (float, char, char): compute and print K from character arithmetic.\n\n#include <iostream.h>\n\nclass CuriousTab\n{\n int K;\npublic:\n void CuriousTabFunction(float, int, char);\n void CuriousTabFunction(float, char, char);\n};\n\nint main()\n{\n CuriousTab objIB;\n objIB.CuriousTabFunction(15.09f, 'A', char('A' + 'A'));\n return 0;\n}\n\nvoid CuriousTab::CuriousTabFunction(float, char y, char z)\n{\n K = int(z);\n K = int(y);\n K = y + z; // integer promotions apply\n cout << "K = " << K << endl;\n}\n

Difficulty: Medium

Correct Answer: The program will print the output K = -61.

Explanation:


Introduction / Context:
The call selects the overload CuriousTabFunction(float, char, char) because both the second and third arguments are chars, which is a better match than promoting the second argument to int. The body then performs character arithmetic, relying on integer promotion rules, which can expose the signedness of char on the target platform.



Given Data / Assumptions:

  • Invocation: (15.09f, 'A', char('A' + 'A')).
  • 'A' has code 65.
  • char('A' + 'A') computes a char from 65 + 65 = 130. On implementations where plain char is signed (common on x86), char(130) becomes -126.
  • K is assigned y + z after integer promotions.


Concept / Approach:
Overload selection chooses (float, char, char). Then y = 65 and z is the char resulting from 130, which on signed-char targets is -126. Adding with integer promotion gives 65 + (-126) = -61. The program prints this integer value.



Step-by-Step Solution:

Overload resolution: (float, char, char) is a better match than (float, int, char).Map y = 65.Compute z = char(130) → typically -126 when char is signed.Compute K = y + z = 65 + (-126) = -61.cout prints "K = -61".


Verification / Alternative check:
On systems where plain char is unsigned, char(130) stays 130 and K becomes 65 + 130 = 195. The provided options reflect both possibilities; the conventional assumption for such questions is signed char, yielding -61.



Why Other Options Are Wrong:

  • K = 130: Would be printed only if the function printed the intermediate assignment K = int(z) instead of the final sum.
  • K = 195: Occurs on platforms with unsigned char; not the typical assumption here.
  • K = -21: Does not match any consistent interpretation of char sums here.
  • Compile time error: Both overloads are valid; one is selected unambiguously.


Common Pitfalls:
Forgetting that plain char may be signed or unsigned, expecting cout with char to print a glyph rather than an integer (here it prints an int), or assuming the (float, int, char) overload is chosen.



Final Answer:
The program will print the output K = -61.

More Questions from Functions

Discussion & Comments

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