Complete the code to declare a destructor in a C++ class.
class MyClass { public: ~[1](); };
The destructor has the same name as the class, preceded by a tilde (~).
Complete the code to define a destructor that prints a message when called.
MyClass::~MyClass() {
std::cout << [1] << std::endl;
}The message must be a string literal enclosed in double quotes.
Fix the error in the destructor declaration.
class MyClass { public: ~MyClass[1]; };
The destructor declaration must include empty parentheses ().
Fill both blanks to complete the destructor definition that frees a dynamically allocated array.
class MyClass { int* data; public: MyClass() { data = new int[10]; } ~MyClass() { [1] data; data = [2]; } };
Use delete[] to free arrays and set pointer to nullptr to avoid dangling pointers.
Fill all three blanks to complete the destructor that closes a file and resets the pointer.
class FileHandler { FILE* file; public: FileHandler(const char* filename) { file = fopen(filename, "r"); } ~FileHandler() { if ([1] != nullptr) { [2](file); file = [3]; } } };
Check if file is not nullptr, then close it with fclose, and reset pointer to nullptr.
