Difficulty: Easy
Correct Answer: Destructor
Explanation:
Introduction / Context:
Resource management is a crucial responsibility in C++ programming. When an object is created, its constructor initialises any necessary resources. When the object is destroyed, another special member function is responsible for cleaning up those resources. This automatic cleanup is a central part of the RAII (Resource Acquisition Is Initialisation) idiom in C++. The question asks you to identify the name of the special member that runs when an object goes out of scope or is explicitly destroyed.
Given Data / Assumptions:
Concept / Approach:
In C++, every class can define a destructor, which is a special member function that has the same name as the class, prefixed with a tilde character. The destructor is called automatically when an automatic object goes out of scope, when a delete expression is applied to a dynamically allocated object, or when an object is destroyed as part of another object's destruction. Its purpose is to free resources such as dynamic memory, file handles, or locks. This function contrasts with the constructor, which runs at object creation time to set up the object's initial state.
Step-by-Step Solution:
Step 1: Recall the naming pattern: for a class named MyClass, the destructor is written as ~MyClass().
Step 2: Understand that destructors take no parameters and have no return type.
Step 3: Recognise that the destructor is invoked automatically by the runtime when an object's lifetime ends, including when delete is called on a pointer to that object.
Step 4: Compare this role with that of the constructor, which runs at creation time, not at destruction time.
Step 5: Select Destructor as the member function that matches the description in the question.
Verification / Alternative check:
You can verify destructor behaviour with a simple example. Define a class whose destructor writes a message to the output stream. Create an instance of the class in a local scope and observe that the message is printed when the scope ends. Similarly, allocate an instance with new and then call delete; you will see the destructor message at the point of deletion. This experiment confirms that the destructor is the function responsible for actions performed when objects are destroyed.
Why Other Options Are Wrong:
Option Constructor: This is the special member that runs at object creation, not at destruction, so it does not match the description.
Option Friend function: Friendship grants access privileges but does not define lifecycle callbacks; a friend function is not automatically invoked when an object goes out of scope.
Option None of the above: This is incorrect because the term destructor is a standard part of C++ vocabulary for this concept.
Common Pitfalls:
One pitfall is to forget to define a destructor when a class manages resources, leading to resource leaks. Another is to perform overly complex logic in destructors, which can cause problems when exceptions are thrown or when objects are partially constructed. It is also important not to call destructors directly in normal code; instead, you should rely on delete and scope exit to trigger them. Understanding the destructor's role is fundamental for writing safe and efficient C++ code.
Final Answer:
The special member function invoked when an object goes out of scope or is deleted is the Destructor.
Discussion & Comments