home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C12 / Opover.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  752 b   |  35 lines

  1. //: C12:Opover.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Operator overloading syntax
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. class Integer {
  11.   int i;
  12. public:
  13.   Integer(int ii) { i = ii; }
  14.   const Integer
  15.   operator+(const Integer& rv) const {
  16.     cout << "operator+" << endl;
  17.     return Integer(i + rv.i);
  18.   }
  19.   Integer&
  20.   operator+=(const Integer& rv){
  21.     cout << "operator+=" << endl;
  22.     i += rv.i;
  23.     return *this;
  24.   }
  25. };
  26.  
  27. int main() {
  28.   cout << "built-in types:" << endl;
  29.   int i = 1, j = 2, k = 3;
  30.   k += i + j;
  31.   cout << "user-defined types:" << endl;
  32.   Integer I(1), J(2), K(3);
  33.   K += I + J;
  34. } ///:~
  35.