Difficulty: Medium
Correct Answer: Base OK. Base OK. Derived OK. Derived DEL. Derived DEL.
Explanation:
Introduction / Context:
This program demonstrates construction order for base/derived classes and the consequences of explicitly calling a destructor on an object with automatic storage. While legal to call, doing so leads to the destructor being invoked twice, which is undefined behavior. The question focuses on the typical printed sequence seen in many environments.
Given Data / Assumptions:
Concept / Approach:
Automatic objects are destroyed at scope end. Manually calling the destructor does not change that; the compiler will still call the destructor again when the object goes out of scope. Although the behavior is undefined, the commonly observed effect is two destructor prints for objD.
Step-by-Step Solution:
Verification / Alternative check:
Remove the explicit call; you will see a single "Derived DEL. " at scope exit.
Why Other Options Are Wrong:
Common Pitfalls:
Calling destructors directly on automatic objects; instead, let scope end manage destruction.
Final Answer:
Base OK. Base OK. Derived OK. Derived DEL. Derived DEL.
Discussion & Comments