C++ (missing destructor definition) — will this program link and run?\n\n#include<iostream.h>\nclass Tab {\n int x;\npublic:\n Tab();\n ~Tab();\n void Show() const;\n};\nTab::Tab() { x = 25; }\nvoid Tab::Show() const { cout << x; }\nint main(){ Tab objB; objB.Show(); return 0; }\n\nChoose the correct statement.

Difficulty: Medium

Correct Answer: The program will report compile time error.

Explanation:


Introduction / Context:
This question examines the difference between declaration and definition, and the role of the linker. A destructor is declared but not defined. Will the program build successfully when an automatic object is created and then destroyed at scope end?


Given Data / Assumptions:

  • ~Tab() is declared but has no out-of-class definition.
  • An automatic object objB is created in main, so its destructor must be generated/linked.
  • Other members (constructor and Show) are defined.


Concept / Approach:
Because ~Tab() is odr-used (it must be called at scope exit), the linker requires a definition. Absent an inline defaulted definition or compiler-generated implicit destructor (suppressed by the user-declared one), the program fails to link, commonly reported as a “unresolved external symbol ~Tab”. Many toolchains surface this as a build-time (compile/link) failure.


Step-by-Step Solution:

1) Compilation of translation unit succeeds (signatures are known).2) During linking, the call to ~Tab is referenced.3) No definition is found → link error → build fails before execution.


Verification / Alternative check:
Provide a trivial definition ~Tab(){}; and the program will link and run, printing 25.


Why Other Options Are Wrong:

  • Printing 25 requires a successful link.
  • “Runtime error” cannot occur because the program never executes.


Common Pitfalls:
Assuming the compiler will synthesize a destructor despite a user-declared (but undefined) one.


Final Answer:
The program will report compile time error.

More Questions from Constructors and Destructors

Discussion & Comments

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