Difficulty: Medium
Correct Answer: 6 8
Explanation:
Introduction / Context:
This program tests understanding of member access across different instances of the same class in C++. The function show() constructs its own local object b but then prints the data members x and y of the current object (the one on which show() was called), not of the temporary b created inside show().
Given Data / Assumptions:
Concept / Approach:
Within a non-static member function, an unqualified member name (x, y) refers to this->x and this->y. Creating another instance inside the member function does not change which object this points to. As a member, show() can assign to private members of any Tab instance it has a reference to, but the cout line is explicitly referencing the current object’s fields.
Step-by-Step Solution:
Verification / Alternative check:
Rename the inner object to b2 and print b2.x, b2.y to see 2 4; but the given code does not do that, it prints the caller’s fields.
Why Other Options Are Wrong:
Common Pitfalls:
Assuming that the most recently declared variable b is what gets printed; forgetting that x and y refer to this->x and this->y.
Final Answer:
6 8
Discussion & Comments