C++ (missing destructor definition) — will this program link and run?
#include
class Tab {
int x;
public:
Tab();
~Tab();
void Show() const;
};
Tab::Tab() { x = 25; }
void Tab::Show() const { cout << x; }
int main(){ Tab objB; objB.Show(); return 0; }
Choose the correct statement.
-
AThe program will print the output 25.
-
BThe program will print the output Garbage-value.
-
CThe program will report compile time error.
-
DThe program will report runtime error.
Answer
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.