• As explained before that we can overload both unary and binary operators so first of all I will explain how unary operators can be overloaded using member function and friend functions. So what are we waiting for? Lets dive in.
  • Unary operation includes only one operator and one operand. In unary minus operation when we place minus sign before any operand then sign of that operand will be changed. We can apply this unary minus operation to object also with the power operator overloading.
  • We can implement this by using friend function or member function.

Overloading Unary Operator Using Member Function

  • Here we have overloaded unary operator using member function.
  • In this example we have one class ‘minus’ which has one data members ‘x’.
  • We are changing the sign of these data member by using overloaded minus operator.  In this example we are not passing any argument to the operator function.
// Program to implement unary minus operator using member function.
#include "iostream.h"
#include "conio.h"

class minus
{
   int x;
   public :

   void get( );
   void show( );

   void operator – ( );  // overloading function for unary operator
};

void minus :: get( )
{
   cout << “Enter value for X: “;
   cin >> x;
}

void minus :: show( )
{
   cout << “Value of X : “ << x << endl ;
}

// member operator function definition

void minus :: operator – ( )
{
   x = - x;
}

void main( )
{
  minus m;
  clrscr( );
  
  m.read( );

  cout << “Before calling Overloading function : “ <<endl ;

  m.show( );

  // calling operator overloading function

  - m;
  cout << “After calling overloading function : ” <<endl ;

  m.show( );

  getch( );

}

Note: In above program we cannot use

m1 = -m2;

Because our operator function doesn’t return any value so we can simply write ‘–m’.

Overloading Unary Operator Using Friend Function

  • Note that an overloading function we implemented using member function above does not take any argument as member function does not take argument for unary operator.
  • While overloading unary operator using friend function we have to supply one argument which is normally a class object reference and it is because a friend function has only one argument for unary operator.
  • In this example we are using  the same class “minus” that we previously used with member function implementation but this time with implementation of overloading function using friend function.
// Program to implement unary minus operator using friend function.
#include "iostream.h"
#include "conio.h"

class minus
{
   int x;
   public :

   void get( );
   void show( );

   friend void operator – (minus &);  // overloading function for unary operator
};

void minus :: get( )
{
   cout << “Enter value for X: “;
   cin >> x;
}

void minus :: show( )
{
   cout << “Value of X : “ << x << endl ;
}

// overloading function definition using friend function

void operator – (minus &m )
{
   m.x = -m.x;
}

void main( )
{
  minus m;
  clrscr( );
  
  m.read( );

  cout << “Before calling Overloading function : “ <<endl ;

  m.show( );

  // calling operator overloading function

  - m;
  cout << “After calling overloading function : ” <<endl ;

  m.show( );

  getch( );

}

Note:

  • In above program we are passing reference to the object.
  • We cannot pass simple object because when we change the value in called function there will be no change in calling function so to make change in calling function we are using reference to object.
  • Since friend function is not a member of a class, its definition is written without scope resolution ( :: ) operator.