home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_09 / 1009099a < prev    next >
Text File  |  1992-07-07  |  706b  |  42 lines

  1.  
  2. Listing 1
  3.  
  4. #include <iostream.h>
  5.  
  6. class X
  7.     {
  8.     friend ostream &operator<<(ostream &os, const X &x);
  9. public:
  10.     X(int i) : a(i), b(0) { }
  11.     X(X &x) : a(x.a), b(x.b) { ++x.b; }
  12.     X(const X &x) : a(x.a), b(x.b+1) { }
  13. private:
  14.     int a, b;
  15.     };
  16.  
  17. ostream &operator<<(ostream &os, const X &x)
  18.     {
  19.     return os << '(' << x.a << ", " << x.b << ')';
  20.     }
  21.  
  22. int main()
  23.     {
  24.     X x1(3);        // uses X(int)
  25.  
  26.     cout << "x1 = " << x1 << '\n';
  27.  
  28.     const X x2(x1); // uses X(X &)
  29.  
  30.     cout << "x1 = " << x1 << '\n';
  31.     cout << "x2 = " << x2 << '\n';
  32.  
  33.     X x3(x2);       // uses X(const X &)
  34.  
  35.     cout << "x2 = " << x2 << '\n';
  36.     cout << "x3 = " << x3 << '\n';
  37.     return 0;
  38.     }
  39.  
  40. ----------
  41.  
  42.