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

  1. // ex09015.cpp
  2. // Virtual functions and multiple derived classes
  3. #include <iostream.h>
  4.  
  5. // --------- abstract date class
  6. class Date {
  7. protected:
  8.     int mo, da, yr;
  9. public:
  10.     Date(int m, int d, int y) {mo = m; da = d; yr = y;}
  11.     virtual void display() = 0;
  12. };
  13.  
  14. // --------- derived numeric date class
  15. class NumDate : public Date {
  16. public:
  17.     NumDate(int m, int d, int y) : Date(m, d, y)
  18.         { /* ... */ }
  19.     void display()
  20.     { cout << mo << '/' << da << "/" << yr; }
  21. };
  22.  
  23. // -------- derived alphabetic date class
  24. class AlphaDate : public Date {
  25. public:
  26.     AlphaDate(int m, int d, int y) : Date(m, d, y) 
  27.         { /* ... */ }
  28.     void display();
  29. };
  30.  
  31. // ------ Display function for AlphaDate
  32. void AlphaDate::display()
  33. {
  34.     static char *mos[] = {
  35.        "January","February","March","April","May","June",
  36.        "July", "August","September","October","November",
  37.        "December"
  38.     };
  39.     cout << mos[Date::mo-1] << ' ' << da << ", " << yr+1900;
  40. }
  41.  
  42.  
  43. main()
  44. {
  45.     NumDate nd(7,29,41);
  46.     AlphaDate ad(11,17,41);
  47.  
  48.     Date& dt1 = nd;
  49.     Date& dt2 = ad;
  50.     dt1.display();
  51.     cout << '\n';
  52.     dt2.display();
  53. }
  54.