In C++, a destructor is a special function that runs automatically when an object is no longer needed. This happens when the object goes out of scope or when delete is called on a pointer to the object. The destructor cleans up resources like memory or files to prevent leaks. In the example, the object 'obj' is created inside main. When main ends, obj goes out of scope, triggering the destructor which prints a message. After that, the object is destroyed and memory is freed. This automatic cleanup is important for safe and efficient programs.