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

  1. // ex08004.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.       // --------- overloaded += operator
  13.     friend Date& operator+=(Date& dt, int n) 
  14.         { dt = dt + n; return dt; }
  15. };
  16.  
  17. static int dys[] = {31,28,31,30,31,30,31,31,30,31,30,31};
  18. // -------- overloaded + operator
  19. Date Date::operator+(int n)
  20. {
  21.     Date dt = *this;
  22.     n += dt.da;
  23.     while (n > dys[dt.mo-1])    {
  24.         n -= dys[dt.mo-1];
  25.         if (++dt.mo == 13)    {
  26.             dt.mo = 1;
  27.             dt.yr++;
  28.         }
  29.     }
  30.     dt.da = n;
  31.     return dt;
  32. }
  33.  
  34. main()
  35. {
  36.     Date olddate(2,20,90);
  37.     olddate += 21;            // three weeks hence
  38.     olddate.display();
  39. }
  40.