home *** CD-ROM | disk | FTP | other *** search
- // ex08003.cpp
- // Overloading relational operators
- #include <iostream.h>
-
- class Date {
- int mo, da, yr;
- public:
- Date(int m, int d, int y) { mo = m; da = d; yr = y; }
- void display() { cout << mo << '/' << da << '/' << yr; }
- // ----- overloaded operators
- friend int operator==(Date& d1, Date& d2)
- { return d1.mo==d2.mo && d1.da==d2.da && d1.yr==d2.yr;}
- friend int operator<(Date& d1, Date& d2)
- { return d1.yr < d2.yr ? 1 :
- d1.mo < d2.mo ? 1 :
- d1.da < d2.da ? 1 : 0; }
- };
-
- main()
- {
- Date date1(12,7,41), date2(2,22,90), date3(12,7,41);
-
- if (date1 < date2) {
- date1.display();
- cout << " is less than ";
- date2.display();
- }
- cout << '\n';
- if (date1 == date3) {
- date1.display();
- cout << " is equal to ";
- date3.display();
- }
- }