In which case it is mandatory to provide a destructor in a class?
2018
In which case it is mandatory to provide a destructor in a class?
- A.
Almost in every class
- B.
Class for which two or more than two objects will be created
- C.
Class for which copy constructor is defined
- D.
Class whose objects will be created dynamically
Attempted by 200 students.
Show answer & explanation
Correct answer: D
A destructor is a special member function that is automatically invoked when an object goes out of scope or is explicitly destroyed using delete. When a class creates objects dynamically (using new), it is mandatory to provide a destructor to release the dynamically allocated memory. If memory is not released properly, it may lead to memory leaks.