Page 150 - DCAP404 _Object Oriented Programming
P. 150

Unit 7: Operator Overloading




          #include  <iostream.h>                                                                Notes
          #include  <conio.h>
          class  integer
          {
                 intx;
                 int  y;
                 intz;
                 public:
                 void  getdata(int  a,  int  b,  int  c);
                 void  disp(void);
                 friend  void  operator-  (integer  &s  );   //  overload  unary  minus
          };
          void  integer::getdata  (int  a,  int  b,  int  c)
          {
                 x = a; y = b; z = c;
          }
          void  integer::disp(void)
          {
                 cout  <<  x  <<  “  “;
                 cout  <<  y  <<“  “;
                 cout  <<  z  <<  “\n”;
          }
          void  operator-  (integer  &s  )           //  Defining  operator-  ()
          {
                 s.x  =  -s.x;
                 s.y  =  -s.y;
                 s.z  =  -s.z;
          }
          void  main  ()
          {
                 integer  S;
                 S.getdata(11  ,-21  ,-31);
                 Cout<<  “S:  “;

                 S.disp();
                 -S;                          //activates  operator-()
                 cout<<“-S:  “;
                 S.disp();
                 getch();
          }



                                           LOVELY PROFESSIONAL UNIVERSITY                                   143
   145   146   147   148   149   150   151   152   153   154   155