Skip to content
  • Destructors · OOPs

Destructors

LearnViaNotes @learnvianotes· 1 min read

The opposite of a constructor. Called automatically when an object goes out of scope to free allocated memory.

C++
class Teacher {

  int* data;

  public:

    Teacher() { data = new int[100]; } // alloc

    ~Teacher() { delete[] data; }       // free — ~symbol

};


// Manual delete for heap objects:

Teacher* t = new Teacher();

delete t;  // calls destructor + frees memory

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report