In C++ (default arguments and implicit conversions), what does this nested class call print? Pay attention to how the char literal 'b' matches the float defaulted parameter. #include<iostream.h> struct MyStructure { class MyClass { public: void Display(int x, float y = 97.50, char ch = 'a') { cout << x << " " << y << " " << ch; } } Cls; } Struc; int main() { Struc.Cls.Display(12, 'b'); return 0; }

Difficulty: Easy

Correct Answer: The program will print the output 12 98 a.

Explanation:


Introduction / Context:
This question checks how default arguments combine with implicit conversions when actual arguments do not match the declared parameter types exactly. It also confirms that omitted trailing parameters receive their defaults in a nested class method call context.


Given Data / Assumptions:

  • Signature: Display(int x, float y = 97.50, char ch = 'a').
  • Call site: Display(12, 'b') (second argument is a char literal).


Concept / Approach:
The second parameter expects float. Passing 'b' (ASCII 98) converts to an integral value 98 and then to float as 98.0f. The third parameter is omitted, so it takes its default 'a'. Thus, the stream prints the integer-looking representation of the float (implementation typically shows "98") and the char a.


Step-by-Step Solution:

x = 12 y = (float) 'b' → 98.0 ch = default → 'a' Printed: "12 98 a"


Verification / Alternative check:
If the call were Display(12), output would be "12 97.5 a". If it were Display(12, 98.0f, 'b'), you would see "12 98 b".


Why Other Options Are Wrong:

  • 12 97.50 b / a: Would require the second argument to be omitted.
  • Garbage variants: A safe, standard conversion is applied; no garbage is expected.


Common Pitfalls:
Expecting the literal 'b' to bind to char ch; parameters are positional, so it binds to the second (float) parameter.


Final Answer:
The program will print the output 12 98 a.

More Questions from Functions

Discussion & Comments

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