C++ Programming Blog

 
 
 

Image

 /**************************************************************************
  **************************************************************************

  A C++ Program to illustrate unary operator(decrement operator) overloading
  ( with return type ).

  **************************************************************************
  *************************************************************************/

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



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


 class counter
    {
       private:
        int count;

       public:
        counter() { count=10; }
        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( )
    {
       clrscr();

       counter obj1;

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

       obj1=--obj1;

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

       obj1=obj1--;

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

       getch();
       return 0;
    }