Machine-translated by Google. Questions are authored in English.
Top

Discussion

In C++, what is the main purpose of destructors ?

  • A.They initialize an object
  • B.They are used for memory deallocation
  • C.They destroy the class
  • D.They return a value

Answer: B

The main purpose of a C++ destructor is that they are used for memory deallocation.

A destructor, named with a tilde (~ClassName), runs automatically when an object goes out of scope or is deleted. Its job is to release resources the object holds, typically freeing dynamically allocated memory and closing files.

Initialising an object is the constructor's job. Destructors destroy objects, not the class, and they cannot return a value.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.