C/C++ Arena

Step 1 of 8

Destructors

A destructor runs automatically when an object's lifetime ends, for example when a local variable goes out of scope. It's named ~ClassName():

struct Noisy {
    Noisy()  { std::cout << "hello\n"; }
    ~Noisy() { std::cout << "bye\n"; }
};

int main() {
    Noisy n;              // prints hello
    std::cout << "work\n";
}                         // prints bye here, automatically

Your turn: complete the destructor so the program prints the three lines shown in the tests.

Next: Order of destruction