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

  1. // ex08001.cpp
  2. // Overloading the + operator
  3. #include <iostream.h>
  4.  
  5. class Date {
  6.     int mo, da, yr;
  7. public:
  8.     Date() {}
  9.     Date(int m, int d, int y) { mo = m; da = d; yr = y; }
  10.     void display() { cout << mo << '/' << da << '/' << yr; }
  11.     Date operator+(int);      // overloaded + operator
  12. };
  13.  
  14. static int dys[] = {31,28,31,30,31,30,31,31,30,31,30,31};
  15. // -------- overloaded + operator
  16. Date Date::operator+(int n)
  17. {
  18.     Date dt = *this;
  19.     n += dt.da;
  20.     while (n > dys[dt.mo-1])    {
  21.         n -= dys[dt.mo-1];
  22.         if (++dt.mo == 13)    {
  23.             dt.mo = 1;
  24.             dt.yr++;
  25.         }
  26.     }
  27.     dt.da = n;
  28.     return dt;
  29. }
  30.  
  31. main()
  32. {
  33.     Date olddate(2,20,90);
  34.     Date newdate;
  35.     newdate = olddate + 21;    // three weeks hence
  36.     newdate.display();
  37. }
  38.