C++ Constructor and Destructors Initializing and Finalizing
#include <iostream> using namespace std; class Food// w ww . ja v a 2s .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; }