Monday, 8 June 2015

Program to illustrate the role of destructor in classes



Code for Program to illustrate the role of destructor in classes in C++ Programming



 



 #include<iostream.h>
 #include<conio.h>

 /*************************************************************************///------------------------------  Counter  ------------------------------///*************************************************************************/class counter
    {
       private:
        int count;

       public:
        counter()  { count=0;  cout<<"I am a constructor"<<endl; }
        ~counter()  { cout<<"I am a destructor"<<endl; }
        void inc_count()  { count ++; }
        int get_count()  { return count; }
    };


 main( )
    {
       clrscr();

       counter c1;
       counter c2;

       cout<<"c1 = "<<c1.get_count()<<endl;
       cout<<"c2 = "<<c2.get_count()<<endl;

       c1.inc_count();
       c2.inc_count();
       c2.inc_count();

       cout<<"c1 = "<<c1.get_count()<<endl;
       cout<<"c2 = "<<c2.get_count()<<endl;

       getch();
       return 0;
    }
 

No comments:

Post a Comment