C++ composition and access: given a contained base-like object and this-> usage, what product does Show() print when constructed as shown?\n\n#include<iostream.h>\nclass CuriousTabBase{ protected: int x, y; public: CuriousTabBase(int xx=0,int yy=0){ x=xx; y=yy; } void Show(){ cout << x * this->y << endl; } };\nclass CuriousTabDerived{ private: CuriousTabBase objBase; public: CuriousTabDerived(int xx,int yy): objBase(xx,yy){ objBase.Show(); } };\nint main(){ CuriousTabDerived objDev(10,20); }

Difficulty: Easy

Correct Answer: 200

Explanation:


Introduction / Context:
This asks about composition (a member object inside another class) and confirms that this->y is simply the member y of the same CuriousTabBase instance, not anything special about inheritance.


Given Data / Assumptions:

  • objBase is constructed with xx=10, yy=20.
  • Show() prints x * this->y.
  • Legacy iostream.h header; ignore modern header conventions.


Concept / Approach:
After member initialization, x=10 and y=20. The product x * this->y equals 10 * 20.


Step-by-Step Solution:
1) Member initialization list sets the contained object's fields.2) Call to objBase.Show() prints 10 * 20 = 200.3) No hidden conversions or pointer tricks are involved.


Verification / Alternative check:
Output x and y separately to confirm they are 10 and 20 before multiplication.


Why Other Options Are Wrong:
0 and 100/400 imply incorrect values; there is no compile error because all members are accessible and properly initialized.


Common Pitfalls:
Confusing composition with inheritance or misreading this->y as something other than the member y.


Final Answer:
200

Discussion & Comments

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