home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Utilities / Programming / C Reference Card 3.0 / C Reference Card / C Reference Card.rsrc / TEXT_409_9.txt < prev    next >
Encoding:
Text File  |  1997-07-17  |  2.5 KB  |  107 lines

  1. OPERATOR OVERLOADING
  2. _________________________________________________________________________
  3.  
  4.  
  5. In C++, you can use the 'operator' keyword to overload normal C operators to perform basic functions like adding and subtracting the way you would normally, by using the '+' and '-' symbols.  Thus, instead of creating a function to do something like this:
  6.  
  7.   Vector = myAddTwoVectors( Vector1, Vector2 );
  8.  
  9. you would overload the '+' operator so that the above statement would look like this:
  10.  
  11.   Vector = Vector1 + Vector2;
  12.  
  13. The mechanics for operator overloading are shown in the following code segment:
  14.  
  15.  
  16.   // vector.cp
  17.   #include <iostream.h>
  18.   
  19.   class Vector
  20.   {
  21.     // friend needed to overload ios operators
  22.     friend ostream &operator<<( ostream &os, const Vector &V );
  23.     // note that implicit 'this' object not sent to friend
  24.     // function outside scope of class
  25.     
  26.     public:
  27.       // default constructor
  28.       Vector(){ x=0; y=0; }      // inline
  29.       // shorthand assignment constructor
  30.       Vector( const float a, const float b ) : x(a), y(b) {}
  31.       // overload operators
  32.       Vector operator+( const Vector &V );
  33.       Vector &operator+=( Vector &V );
  34.       Vector &operator=( Vector &V );
  35.       Vector &operator++();      // prefix
  36.       Vector operator++( int );  // postfix - requires dummy argument
  37.  
  38.     private:
  39.       float x;
  40.       float y;
  41.   };
  42.     
  43.   Vector Vector::operator+( const Vector &V )
  44.   {
  45.     return Vector( x + V.x, y + V.y );
  46.   }
  47.   
  48.   Vector &Vector::operator+=( Vector &V )
  49.   {
  50.     x = x + V.x;
  51.     y = y + V.y;    
  52.     return *this;
  53.   }
  54.   
  55.   Vector &Vector::operator=( Vector &V )
  56.   {
  57.     // no different than default operator, but could be
  58.     x = V.x;
  59.     y = V.y;    
  60.     return *this;
  61.   }
  62.   
  63.   Vector &Vector::operator++()
  64.   {
  65.     ++x;
  66.     ++y;
  67.     return *this;
  68.   }
  69.   
  70.   Vector Vector::operator++( int )
  71.   {
  72.     Vector V;
  73.     V = *this;
  74.     x++;
  75.     y++;
  76.     return V;
  77.   }
  78.   
  79.   ostream &operator<<( ostream &os, const Vector &V )
  80.   {
  81.     return os << "Vector[ " << V.x << ", " << V.y << " ]";
  82.   }
  83.     
  84.   main()
  85.   {
  86.     Vector vectorA( 3, 4 );
  87.     Vector vectorB( 2, 7 );
  88.  
  89.     // output line 1
  90.     cout << vectorA << " + " << vectorA << " = ";
  91.     vectorA += vectorA;
  92.     cout << vectorA << endl;
  93.  
  94.     // output line 2
  95.     cout << vectorA << " + " << vectorB << " = "
  96.       << vectorA + vectorB << endl;
  97.  
  98.     // output line 3
  99.     vectorA = vectorB;
  100.     cout << vectorA << endl;
  101.     
  102.     // output line 4
  103.     cout << ++vectorA << endl;
  104.  
  105.     return 0;
  106.   }
  107.   // end vector.cp