An object can have a destructor: a special member function called when the object's lifetime ends. Its name is a tilde followed by the class name, such as ~Notice(). It takes no parameters and has no return type. You do not call it manually for an ordinary local object; leaving the block performs destruction automatically.
A destructor is often responsible for releasing a resource. Here it prints a message so you can observe exactly when destruction happens. This is a teaching trace, not a requirement to print from every destructor in real programs. Standard containers already release their own storage without needing you to write such messages.
Within one block, local objects are destroyed in reverse order of completed construction. Inner-block objects finish before execution continues after that block. The destructor body runs while the object's members are still available, so it can read the stored label used in the example.
#include <iostream>
#include <string>
class Notice {
private:
std::string label;
public:
Notice(std::string text) : label(text) {}
~Notice() { std::cout << "End " << label << '\n'; }
};
int main() {
{
Notice first("A");
Notice second("B");
std::cout << "Inside\n";
}
std::cout << "Outside\n";
}
Read the output alongside the closing brace: Inside prints first, then leaving the block destroys second and first, producing End B and End A. Only then does Outside print. In the starter, put the new message inside ~Session(), not in main. The existing inner block triggers that destructor automatically between the two supplied messages.
~Session() print Session closed followed by a newline. The complete output should be Working, Session closed, and After block, in that order.Edit main.cpp, then press Run to inspect the Console. Press Submit to check your work. Keep the supplied probes in place.