C++ constructor pre-increment parameters and post-increment in output: what does Show() print? #include<iostream.h> class CuriousTabData { int x, y, z; public: CuriousTabData(int xx, int yy, int zz) { x = ++xx; y = ++yy; z = ++zz; } void Show() { cout << "" << x++ << " " << y++ << " " << z++; } }; int main() { CuriousTabData objData(1, 2, 3); objData.Show(); return 0; }

Difficulty: Easy

Correct Answer: The program will print the output 2 3 4 .

Explanation:

Introduction / Context:This question checks the effect of pre-increment on constructor parameters versus post-increment when streaming values. Understanding sequence and timing of increments is key.

Given Data / Assumptions:

  • Constructor arguments are (1,2,3).
  • Assignments use pre-increment: x=++xx, y=++yy, z=++zz.
  • Printing uses post-increment: x++, y++, z++.

Concept / Approach:Pre-increment increments first, then yields the incremented value for assignment. Post-increment yields the current value for output, then increments afterward.

Step-by-Step Solution:After construction: x=2, y=3, z=4.Streaming with post-increment prints 2, 3, 4 in order.After printing, the internal values become 3, 4, 5 (not shown).

Verification / Alternative check:Add a second call to Show(); it will then print 3 4 5 because of the prior post-increments.

Why Other Options Are Wrong:Option A ignores the constructor’s pre-increment. Option C reflects the internal state after a hypothetical second print. Option D is incorrect because the code is valid C++.

Common Pitfalls:Mixing up pre- and post-increment semantics and assuming both behave identically in assignments and outputs.

Final Answer:The program will print the output 2 3 4 .

More Questions from Objects and Classes

Discussion & Comments

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