home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK4 / SAMPLES / CPPTUTOR / DATE.CP$ / DATE
Encoding:
Text File  |  1991-12-11  |  1022 bĀ   |  56 lines

  1. // DATE.CPP
  2.  
  3. #include "date.h"
  4. #include <iostream.h>
  5.  
  6. Date::Date()
  7. {
  8.     month = day = year = 1;   // Initialize data members
  9. }
  10.  
  11. Date::Date( int mn, int dy, int yr )
  12. {
  13.    setMonth( mn );
  14.    setDay( dy );
  15.    setYear( yr );
  16. }
  17.  
  18. void Date::setMonth( int mn )
  19. {
  20.     month = max( 1, mn );
  21.     month = min( month, 12 );
  22. }
  23.  
  24. void Date::setDay( int dy )
  25. {
  26.     static int length[] = { 0, 31, 28, 31, 30, 31, 30,
  27.                                31, 31, 30, 31, 30, 31 };
  28.     day = max( 1, dy );
  29.     day = min( day, length[month] );
  30. }
  31.  
  32. void Date::setYear( int yr )
  33. {
  34.     year = max( 1, yr );
  35. }
  36.  
  37. // -------- Member function to printĀ date
  38. void Date::display() const
  39. {
  40.     static char *name[] =
  41.     {
  42.         "zero", "January", "February", "March", "April", "May",
  43.         "June", "July", "August", "September", "October",
  44.         "November","December"
  45.     };
  46.  
  47.     cout << name[month] << ' ' << day << ", " << year;
  48. }
  49.  
  50. // ---------- The destructor
  51. Date::~Date()
  52. {
  53.    // do nothing
  54. }
  55.  
  56.