C++ constructor parameters assigned via this-> and no Display call: what prints? #include <iostream.h> class Tab { int x, y; public: Tab(int x, int y) { this->x = x; this->y = y; } void Display() { cout << x << " " << y; } }; int main() { int x = 50; int &y = x; Tab b(y, x); // no call to Display() return 0; }

Difficulty: Easy

Correct Answer: The program will print nothing.

Explanation:


Introduction / Context:
Here, the goal is to check understanding that simply constructing an object does not print anything unless output is explicitly performed (e.g., by calling Display or writing from the constructor). The code initializes members using this-> but never outputs them.


Given Data / Assumptions:

  • Tab::Display exists but is never invoked.
  • Constructor sets internal x and y to 50 and 50 respectively.
  • Main returns immediately.


Concept / Approach:
C++ programs only print when code writes to std::cout. Since Display is not called and the constructor contains no output, nothing is written, and the program produces no console output.


Step-by-Step Solution:

Create x = 50 and reference y = x.Construct Tab b(y, x) → members set to 50 and 50.No cout call occurs; program ends.


Verification / Alternative check:
Adding b.Display(); before return 0 would print 50 50. Likewise, adding output inside the constructor would print during construction.


Why Other Options Are Wrong:

  • 50 50: would require Display() or constructor output.
  • Garbage values: members are initialized deterministically.
  • Compile error: code is valid with old headers.


Common Pitfalls:
Assuming the presence of a Display method implies it is called; expecting output from mere object construction.


Final Answer:
The program will print nothing.

Discussion & Comments

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