Write a program to Demonstrate Destructor

/*
    Destructor Demonstrate
*/
#include<iostream>
using namespace std;

class Sample
{
    static int code;

    public:
            Sample()
            {
                code++;
                cout<<"\n Object Created: "<<code;
            }

            ~Sample()
            {
                cout<<"\n Object Destroyed: "<<code;
                code--;
            }
};

int Sample::code;

int main()
{
    cout<<"\n\n Main Body: ";
    Sample S1,S2,S3,S4;

    {
        cout<<"\n\n Block-1: ";
        Sample S5,S6;
    }
    {
        cout<<"\n\n Block-2 Outer Block: ";
        Sample S7;
        {
            cout<<"\n\n Block-2 Inner Block: ";
            Sample S8,S9;
        }
    }

    cout<<"\n\n Re-Entered Main Body: ";

    Sample S10;

    return 0;
}

Previous
Next Post »