Difficulty: Easy
Correct Answer: destructor
Explanation:
Introduction / Context:
C++ provides special member functions that govern object lifetime. A function spelled with the class name prefixed by a tilde (~) is one of these special members and is responsible for cleanup when an object's lifetime ends. This question checks recognition of that naming convention and its role in RAII (Resource Acquisition Is Initialization).
Given Data / Assumptions:
Concept / Approach:
The member named ~ClassName() is the destructor. It performs finalization such as releasing memory, closing file handles, unlocking mutexes, or logging. The constructor, by contrast, shares the class name without the tilde and initializes the object. Recognizing the destructor’s signature is foundational for writing exception-safe, leak-free code in C++.
Step-by-Step Solution:
Verification / Alternative check:
Instrument ~ClassName() with a print; observe calls when objects go out of scope, when vectors shrink, or when delete p; is executed for dynamically allocated objects with virtual destructors where needed.
Why Other Options Are Wrong:
constructor: no tilde and runs at creation.
function/object: too generic and do not identify the special cleanup role.
Common Pitfalls:
Final Answer:
destructor
Discussion & Comments