home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 13 / CDA13.ISO / cdactual / demobin / share / program / C / ANSICPP.ZIP / EX08003.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-24  |  793 b   |  35 lines

  1. // ex08003.cpp
  2. // Overloading relational operators
  3. #include <iostream.h>
  4.  
  5. class Date {
  6.     int mo, da, yr;
  7. public:
  8.     Date(int m, int d, int y) { mo = m; da = d; yr = y; }
  9.     void display() { cout << mo << '/' << da << '/' << yr; }
  10.     // ----- overloaded operators
  11.     friend int operator==(Date& d1, Date& d2)
  12.      { return d1.mo==d2.mo && d1.da==d2.da && d1.yr==d2.yr;}
  13.     friend int operator<(Date& d1, Date& d2)
  14.      { return d1.yr < d2.yr ? 1 :
  15.               d1.mo < d2.mo ? 1 :
  16.                  d1.da < d2.da ? 1 : 0; }
  17. };
  18.  
  19. main()
  20. {
  21.     Date date1(12,7,41), date2(2,22,90), date3(12,7,41);
  22.  
  23.     if (date1 < date2)    {
  24.         date1.display();
  25.         cout << " is less than ";
  26.         date2.display();
  27.     }
  28.     cout << '\n';
  29.     if (date1 == date3)    {
  30.         date1.display();
  31.         cout << " is equal to ";
  32.         date3.display();
  33.     }
  34. }
  35.