Saturday, 7 March 2015

basic program on constructor and destructor

Example on constructor and destructor :


#include<iostream>
#include<conio.h>
using namespace std;
  static int countl;
 class test
 {

 public:
    test()
    {
        countl++;
         cout<<"\t\t  constructor msg:object"                <<countl<<"created....."<<endl;
    }


    ~test()
    {

        cout<<"\t\t  destructor msg:object  "   <<countl<<"destroy....."<<endl;
          countl--;
    }
 };


 int main()
 {

     cout<<"inside block...."<<endl<<endl;
     cout<<"creat object....."<<endl;

    {


         test t1;


      {
         cout<<"inside main block.."<<endl;
          cout<<"creat another two  object....."<<endl;
          test t2,t3;
          cout<<"leave the block....."<<endl;

        }

}

        cout<<"back to main block....."<<endl;
     getch();
     return(0);



 }

No comments:

Post a Comment