Initializing and Finalizing class with Constructors and Destructors - C++ Class

C++ examples for Class:Destructor

Description

Initializing and Finalizing class with Constructors and Destructors

Demo Code

                                                   
#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;
}

Result


Related Tutorials