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?

Answer: D. Class whose objects will be created dynamicallyA 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…

  1. A.

    Almost in every class

  2. B.

    Class for which two or more than two objects will be created

  3. C.

    Class for which copy constructor is defined

  4. D.

    Class whose objects will be created dynamically

Attempted by 377 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.

Explore the full course: Bpsc

Loading lesson…