Difficulty: Medium
Correct Answer: 65 65 65
Explanation:
Introduction / Context:
This example explores which base-class constructor is chosen when a derived-class initializer passes a single argument and the base has two candidate constructors, one of which provides defaulted parameters. It also shows that calling Base::Display()
prints the base state, not the derived members.
Given Data / Assumptions:
Base(int,int)
and Base(int, int y = 'A', int z = 'B')
.: Base(x)
(a single argument), and then assigns x=65, y=66, z=67
in the body.Display(-1)
is nonzero, so Base::Display()
executes.
Concept / Approach:
With one argument supplied, overload resolution selects Base(int, int y = 'A', int z = 'B')
. That constructor ignores the first parameter value in assignments and uses the defaults for y
and z
to compute stored values: S = y
, A = y + 1 - 1
, M = z - 1
with y='A' (65)
and z='B' (66)
, yielding three 65s.
Step-by-Step Solution:
Verification / Alternative check:
If Display(0)
were called, it would print derived members "65 66 67" after they are assigned in the body; but the code explicitly calls Base::Display()
due to a nonzero argument.
Why Other Options Are Wrong:
Common Pitfalls:
Assuming the member x
value is used by the base. The selected base ctor ignores it and relies on defaults for assignments shown.
Final Answer:
65 65 65
Discussion & Comments