Skip to Content
Classes and Objects

So far, you’ve learned how to create and use objects. But there’s another part of the object lifecycle we need to cover: how to destroy them! Muahahaha.

It’s actually far less nefarious than it sounds; object destruction is really about tidying up and preventing memory leaks. A destructor is a special method that handles object destruction. Like a constructor, it has the same name as the class and no return type, but is preceded by a ~ operator and takes no parameters:

City::~City() { // any final cleanup }

Inside, you add any housekeeping that needs to happen before the object is destroyed. You generally won’t need to call a destructor; the destructor will be called automatically in any of the following scenarios:

  • The object moves out of scope.
  • The object is explicitly deleted.
  • When the program ends.



Create a destructor for Song that prints "Goodbye " plus the song’s title. For example, a song with a title of "Drama" should print the following message when destroyed:

Goodbye Drama!

(You’ll need to include the <iostream> header in song.cpp for this to work.)

Then compile and execute the program in the terminal.

Folder Icon

Take this course for free

Already have an account?