The job of the destructor is to de initialize or

This preview shows page 12 - 17 out of 105 pages.

The job of the destructor is to de-initialize or destruct a class variable when it goes away . class Gassy { public: private: }; Here’s how we define a destructor for our class… ~ Gassy() { } To define a destructor function, place a tilde ~ character in front of the name of the class. // your destructor // code goes here for (int i=0;i < 5;++i) cout << “pffffttt!\n”; It looks just like a constructor function except for the tilde ~ which identifies it as a destructor. Destructors must NOT have any parameters . Destructors must NOT return a value either.
Image of page 12

Subscribe to view the full document.

Destructors If you don’t define your own destructor for a class… Then C++ will define an implicit one for you… class Gassy { public: Gassy() { m_age = 0; m_ateBeans = false; } int getNerdScore(void) { if(m_ateBeans == true) return(100); return(3 * m_age); } ~Gassy() // generated by compiler { } private: int m_age; bool m_ateBeans; }; This ensures that objects of your class properly go away when they go out of scope.
Image of page 13
class SpotifyPlayer { public: SpotifyPlayer () { // Reserve 100MB of disk space to temporarily // cache downloaded MP3 music files. reserveSpaceOnDisk(100000000); } }; Why Do We Need Destructors? void playSong ( string songURL ) { // First download song to reserved space on disk. downloadMP3ToReservedSpace(songURL); // Then play the song that was saved to our disk. playMP3InReservedSpace(); } int main() { // Obsessively play k-pop music! for (int i = 0; i < 100; ++i) { SpotifyPlayer a; a.playSong(“ ;); } } Can anyone see what the problem is? We reserve 100MB of disk space when we construct a new Spotify object. But nothing in our class ever frees this disk space! So every time we create a new object, we waste another 100MB! +100MB +200MB +300MB
Image of page 14

Subscribe to view the full document.

class SpotifyPlayer { public: SpotifyPlayer () { // Reserve 100MB of disk space to temporarily // cache downloaded MP3 music files. reserveSpaceOnDisk(100000000); } }; Why Do We Need Destructors? void playSong ( string songURL ) { // First download song to reserved space on disk. downloadMP3ToReservedSpace(songURL); // Then play the song that was saved to our disk. playMP3InReservedSpace(); } int main() { // Obsessively play k-pop music! for (int i = 0; i < 100; ++i) { SpotifyPlayer a; a.playSong(“ ;); } } Can anyone see what the problem is? +100MB So how do we fix our code??? Let’s add a destructor, of course! ~SpotifyPlayer() { // Free the reserved space on disk. freeReservedSpace(); } +100MB Now every time we construct we reserve 100MB… And every time we destruct we free our 100MB!
Image of page 15
When must you have a destructor? Any time a class allocates a system resource Your class must have a destructor that… Don’t forget or you’ll FAIL ! Reserves memory using the new command Frees the allocated memory with the delete command Opens a disk file Closes the disk file Connects to another computer over the network Disconnects from the other computer
Image of page 16

Subscribe to view the full document.

Destructors So when is a destructor called anyway?
Image of page 17
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern