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

  1. // ex07018.cpp
  2. // Class Assignment
  3. #include <iostream.h>
  4. #include <string.h>
  5.  
  6. // ------- date class
  7. class Date {
  8.     int mo, da, yr;
  9.     char *month;
  10. public:
  11.     Date();
  12.     Date(int m, int d, int y);
  13.     ~Date();
  14.     void operator=(Date&); // overloaded assignment operator
  15.     void display();
  16. };
  17.  
  18. // constructor that is called for an uninitialized Date
  19. Date::Date()
  20. {
  21.     mo = 0; da = 0; yr = 0;
  22.     month = NULL;    
  23. }
  24.  
  25. // constructor that is called for an initialized Date
  26. Date::Date(int m, int d, int y)
  27. {
  28.     static char *mos[] = {
  29.         "January", "February", "March", "April", "May", 
  30.         "June",    "July", "August", "September", "October", 
  31.         "November",    "December"
  32.     };
  33.     mo = m; da = d; yr = y;
  34.     month = new char[strlen(mos[m-1])+1];
  35.     strcpy(month, mos[m-1]);
  36. }
  37. // Destructor for a Date
  38. Date::~Date()
  39. {
  40.     if (month != NULL)
  41.         delete month;
  42. }
  43.  
  44. // ----------- display member function
  45. void Date::display() 
  46. {
  47.     if (month != NULL)
  48.         cout << '\n' << month << ' ' << da << ", " 
  49.              << yr+1900;
  50. }
  51.  
  52. // ---------- overloaded Date assignment
  53. void Date::operator=(Date& dt)
  54. {
  55.     mo = dt.mo;
  56.     da = dt.da;
  57.     yr = dt.yr;
  58.     if (month != NULL)
  59.         delete month;
  60.     if (dt.month != NULL)    {
  61.         month = new char [strlen(dt.month)+1];
  62.         strcpy(month, dt.month);
  63.     }
  64.     else
  65.         month = NULL;
  66. }
  67.  
  68. main()
  69. {
  70.     // ------ first date
  71.     Date birthday(6,24,40);
  72.     birthday.display();
  73.     // ------ second date
  74.     Date newday(7,29,41);
  75.     newday.display();
  76.     // ------ assign first to second
  77.     newday = birthday;
  78.     newday.display();
  79. }
  80.