C++ Programming Blog

 
 
 

Image

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


 //-----------------------------  counter  -------------------------------//


 class counter
    {
       private:
        int count;

       public:
        counter()  { count=0; }
        counter operator++();
        counter operator++(int);
        void showdata()  { cout<<count<<endl; }
     };



 //---------------------------  operator++( )  ---------------------------//


 counter counter::operator++()
    {
       ++count;

       counter temp;

       temp.count=count;

       return temp;
    }


 //---------------------------  operator++(int)  -------------------------//


 counter counter::operator++(int)
    {
       count++;

       counter temp;

       temp.count=count;

       return temp;
    }



 //-----------------------------  Main( )  -------------------------------//



 main( )
    {
       clrscr();

       counter obj1;

       cout<<\"\\n ********* Before Increment ******* \"<<endl;
       cout<<\"Data of obj1 is = \";
       obj1.showdata();

       obj1=++obj1;

       cout<<\"\\n ********* After Increment ******* \"<<endl;
       cout<<\"Data of obj1 is = \";
       obj1.showdata();

      obj1=obj1++;

       cout<<\"\\n ********* After Increment ******* \"<<endl;
      cout<<\"Data of obj1 is = \";
      obj1.showdata();

      getch();
      return 0;
    }