home *** CD-ROM | disk | FTP | other *** search
/ Microsoftware Monthly 19…2 Programming Power Tools / MASO9512.ISO / cpptutor / cpptutor.arj / EXAMPLES / EX0807.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-06  |  1.6 KB  |  67 lines

  1. // \EXAMPLES\EX0807.CPP
  2. // program using nesting classes
  3.  
  4. //--------------------------------------------------------------
  5. #include <iostream.h>
  6.  
  7. //--------------------------------------------------------------
  8. // definition of class Date
  9. // Definition of struct Date::DMY
  10.  
  11. class Date {
  12.   struct DMY {
  13.      int day;
  14.      int month;
  15.      int year;
  16.      int leapyear();
  17.   };
  18.   DMY calendar;
  19.   long days;
  20. public:
  21.   Date(int d, int m, int y);
  22.   Date () {};
  23.   long getdays() {return days; };
  24. };
  25. //--------------------------------------------------------------
  26. // member function of Date
  27.  
  28. Date::Date(int d, int m, int y )
  29. { calendar.day = d;
  30.   calendar.month = m;
  31.   y = y - 50;
  32.   calendar.year = y;
  33.   days = y * 365 + y/4;
  34.   int mdays[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31 };
  35.   if ( calendar.leapyear() && m < 3 ) days--;
  36.   for (int i = 0; i < m; i++ ) days += mdays[i];
  37.   days += d;
  38. }
  39. //--------------------------------------------------------------
  40. // member function of DMY
  41.  
  42. int Date::DMY::leapyear()
  43. // return 1 if year is a leapyear
  44. // return 0 otherwise
  45. { if ( year % 100 == 0 ) return 0;
  46.   if ( year % 4 == 0 ) return 1;
  47.   return 0;
  48. }
  49.  
  50. //--------------------------------------------------------------
  51. // main() to exercise Date and DMY
  52.  
  53. void main()
  54. {
  55.   int d, m, y;
  56.   do
  57.   { cout << "Enter a date as three 2-digit numbers" << endl;
  58.      cout << "   day month year:  ";
  59.      cin >> d >> m >> y;
  60.   } while ( y < 0 || y > 99 );
  61.   Date dmy (d,m,y);
  62.   cout << "The number of days after 1 Jan 1950:  ";
  63.   cout << dmy.getdays() -1;
  64.   cout << endl;
  65. }
  66. //--------------------------------------------------------------
  67.