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

  1. // ex07017.cpp
  2. // Destructors
  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 display();
  15. };
  16.  
  17. // constructor that is called for an uninitialized Date
  18. Date::Date()
  19. {
  20.     mo = 0; da = 0; yr = 0;
  21.     month = NULL;    
  22. }
  23.  
  24. // constructor that is called for an initialized Date
  25. Date::Date(int m, int d, int y)
  26. {
  27.     static char *mos[] = {
  28.         "January", "February", "March", "April", "May", 
  29.         "June",    "July", "August", "September", "October", 
  30.         "November",    "December"
  31.     };
  32.     mo = m; da = d; yr = y;
  33.     month = new char[strlen(mos[m-1])+1];
  34.     strcpy(month, mos[m-1]);
  35. }
  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. main()
  53. {
  54.     Date birthday(6,24,40);
  55.     birthday.display();
  56. }
  57.