C++ examples for Class:Destructor
Initializing and Finalizing class with Constructors and Destructors
#include <iostream> using namespace std; class Food/* w w w. j ava 2 s .c o m*/ { public: int Size; }; class Cat { private: Food *food; public: Cat(); ~Cat(); }; Cat::Cat() { cout << "Starting!" << endl; food = new Food; food->Size = 30; } Cat::~Cat() { cout << "Cleaning up my mess!" << endl; delete food; } int main() { Cat *Sam = new Cat; Cat *Sally = new Cat; delete Sam; delete Sally; return 0; }