home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_08 / 8n08053a < prev    next >
Text File  |  1990-07-18  |  3KB  |  132 lines

  1. // meal3.cxx - third attempt at OOP meal pricing program
  2. #include <stream.h>
  3.  
  4. enum ENTREE {Steak,Fish};
  5. enum DESSERT {Pie,Cake,Jello};
  6. enum APPETIZER {Melon,ShrimpCocktail};
  7.  
  8. class Dessert   // An abstract class
  9.                 // - never instantiated by itself
  10.     { 
  11. public:
  12.     virtual double cost() = 0; // pure virtual ==> 
  13.                                // abstract class
  14.     virtual double discount() { return .75; } // 25% off
  15.     virtual const char* text() = 0;
  16.     };
  17.  
  18. class Jello_obj : public Dessert
  19.     {
  20. public:
  21.     double cost() {return .60;}
  22.     const char* text() { return "Jello ";}
  23.     };
  24.  
  25. class Pie_obj : public Dessert
  26.     {
  27. public:
  28.     double cost() {return  1.50;}
  29.     double discount() { return 1.00; } // no discount
  30.     const char* text() { return "Pie ";}
  31.     };
  32.  
  33. class Cake_obj : public Dessert
  34.     {
  35. public:
  36.     double cost() {return  1.00;}
  37.     const char* text() { return "Cake ";}
  38.     };
  39.  
  40. class Entree
  41.     {
  42. public:
  43.     virtual double cost() = 0;
  44.     virtual const char* text() = 0;
  45.     };
  46.  
  47. class Fish_obj : public Entree
  48.     {
  49. public:
  50.     double cost() {return 4.00;}
  51.     const char* text() { return "Fish ";}
  52.     };
  53.  
  54. class Steak_obj : public Entree
  55.     {
  56. public:
  57.     double cost() {return 7.50;}
  58.     const char* text() { return "Steak ";}
  59.     };
  60.  
  61. class Appetizer
  62.     {
  63. public:
  64.     virtual double cost()  = 0;
  65.     virtual const char* text() = 0;
  66.     };
  67.  
  68. class Cocktail_obj : public Appetizer
  69.     {
  70. public:
  71.     double cost() { return  2.00;}
  72.     const char* text() { return "Cocktail ";}
  73.     };
  74.  
  75. class Melon_obj : public Appetizer
  76.     {
  77. public:
  78.     double cost() { return .85;}
  79.     const char* text() { return "Melon ";}
  80.     };
  81.  
  82. class Meal 
  83.     {
  84.     Appetizer *a;
  85.     Entree *e;
  86.     Dessert *d;
  87. public:
  88.     Meal(APPETIZER=Melon,ENTREE=Fish,DESSERT=Jello);
  89.     ~Meal();
  90.     double cost();
  91.     void print();
  92.     };
  93.  
  94. //-------------------------------------------
  95. // class member function definitions
  96.  
  97. double Meal::cost() {return d->cost()*d->discount() +
  98.          a->cost() + e->cost(); }
  99.  
  100. Meal::Meal(APPETIZER aval,ENTREE eval,DESSERT dval)
  101.     {
  102.     if ( dval == Jello ) d = new Jello_obj;
  103.     else if ( dval == Pie ) d = new Pie_obj;
  104.     else d = new Cake_obj;
  105.     if ( eval == Steak ) e = new Steak_obj;
  106.     else e = new Fish_obj;
  107.     if ( aval == Melon ) a = new Melon_obj;
  108.     else a = new Cocktail_obj;
  109.     }
  110.  
  111. Meal::~Meal() { delete a; delete e; delete d; }
  112.  
  113. void Meal::print()
  114.     {
  115.     cout << a->text() << e->text() << d->text() <<
  116.         ", Meal cost = " << a->cost() + e->cost() + 
  117.         d->cost()*d->discount() << "\n";
  118.     }
  119. //--------------------------------------------
  120. main()
  121.     {
  122.     Meal m1(Melon,Fish,Jello);
  123.     Meal m2(Melon,Steak,Pie);
  124.     Meal m3(ShrimpCocktail,Steak,Cake);
  125.     m1.print(); m2.print(); m3.print();
  126.     }
  127. // ---- sample output below -----
  128.  
  129. Melon Fish Jello , Meal cost = 5.3
  130. Melon Steak Pie , Meal cost = 9.85
  131. Cocktail Steak Cake , Meal cost = 10.25
  132.