Learning  C++
Home
Tutorials
C++  Programs
Contact  us
Sitemap
Your Ad Here
Your Ad Here
Overloading  Unary  Operators  ( member  functions )

Let   us   consider   the   unary   minus   operator.   A   minus   operator,   when  used  as  a unary,  takes  just  one  operand.   We   will  see  here  how  to  overload  this  operator  so that it  can  be  applied  to  an  object  in  much  the  same  way  as  is  applied  to  an  int or float variable.  The  unary  minus  when  applied  to  an  object  should change  the  sign of each  of its  data  items.


For  Example,


                                              class  X
                                              {
                                                                    int  a ;
                                                                    int  b ;

                                                           public :

                                                                    void  get ( int  m,  int  n ) ;
                                                                    void  display ( void ) ;
                                                                    void  operator - ( ) ;       // overloaded  unary  minus
                                              } ;

                                              void  X : : get ( int  m,  int  n )
                                              {

                                                           a = m ;
                                                           b = n ;
                                              }

                                              void  X : : display ( void )
                                              {
                         
                                                       cout  <<  a  << " " ;
                                                       cout  <<  b   << " " ;
           
                                              }

                                              void  X : : operator  -  ( )
                                              {

                                                           a = -a ;
                                                           b = -b ;

                                              }

                                             int  main ( )
                                             {
                                                          X  x ;
                                                          x . get ( 10,  20 ) ;
                                                          - x ;           //  calls  operator - ( )
                                                          x . display ( ) ;
                                                          return  0 ;
                                             }


This  program  produces  the  output  -10  -20.


Note  that  the  function  operator - ( )  takes  no  argument.  It  changes  the  sign  of  data members  of  the  object  x.   Since   this   function  is  a  member  function  of  the  same class, it  can  directly  access  the  members  of  the  object  which  activated  it.



Overloading  Unary  Operators  ( Friend  functions )

It  is  possible  to  overload  a  unary  minus  operator  using  a  friend  function.


                                               friend  void  operator  -  ( X &  obj ) ;    //  declaration

                                               void  operator  -  ( X &  obj )
                                               {

                                                           obj . a = - obj . a ;
                                                           obj . b = - obj . b ;

                                               }


Note  that  the  argument  is  passed  by  reference.  It  will not  work  if  we  pass  argument by value   because   only   a   copy   of  the  object  that  activated  the  call  is  passed  to operator  - ( ) .   Therefore,   the   changes   made   inside   the  operator  function  will  not reflect  in  the  called  object.