Unary Operators Overloading in C++

By | September 29, 2021

Unary operators are those special symbols that can operate on a unary or single operand, for instance, ++(increment), –(decrement) and ! (not) operators.

As we have already discussed in the Operator overloading rules that we can only redefine the operator task for the user-defined class-based object so here the example itself using a class and its object to perform the operator overloading implementation.

Example

#include <iostream>
#include<math.h>
using namespace std;

class Displacement
{
   private :
      int x;

   public:
        Displacement(int initialize) //counstructor
           {
                x=initialize;
                cout<<"you have created an object which need to displace 10 unit";
                cout<<endl;
          }            
      void operator--() //operator overloading for -- operator
          {
                x-=1;
               cout<<"1 unit has been deducted from the Displacement now final displacement is: "<<x;
               cout<<endl;
         }

     void operator++()   //operator overloading for ++ operator
        {
              x+=1;
              cout<<"1 unit has been added to the Displacement and final Displacement is: "<<x;
              cout<<endl;
        }

    void operator!()
       {
              if(x>0){
                  x = -x;
                  cout<<"The the direction of Displacement has been changed final displacement is: "<<x;
                  cout<<endl;
                    }
             else{
                 x= abs(x);           
                 cout<<"The direction of Displacement has been changed final displacement is: "<<x;
                 cout<<endl;
                }
       }
};
int main()
{ 
     Displacement d(10); // create an object
     ++d;       // prefix increment operator on class object
    --d;         //  prefix decrement operator on class object
    !d;                          // not operator on class object
    return 0;
}

Output

you have created an object which need to displace 10 unit
1 unit has been added to the Displacement and final Displacement is: 11
1 unit has been deducted from the Displacement now final displacement is: 10
The direction of Displacement has been changed final displacement is: -10

Behind the code

Vamware

In this above example, we have created 3 Operator Overloading functions, which redefine the task for ++, — and ! operators.

So when we apply these operators on the Displacement class object d the corresponding function gets invoked,

Vamware

Displacement d(10); this statement invoked the Class constructor method or member function Displacement(int initialize).

 ++d; this statement invoked the void operator++() member function.

–d; this statement invoked the void operator–() member function.

And !d; this statement invoked the void operator!() member function.

Note: In unary operator overloading, we do not pass any argument in the operator overloading function because the operator works on a single operand which would be the class object itself.

People are also reading: 

Leave a Reply

Your email address will not be published.