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

  1. // meal2.cxx - second attempt at pricing a meal
  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 - never instantiated by itself
  9.     {
  10. public:
  11.     virtual double cost() 
  12.         { printf("Dessert error, no cost() provided\n"); return 0;}
  13.     };
  14.  
  15. class Jello_obj : public Dessert
  16.     {
  17. public:
  18.     double cost() {return .60;}
  19.     };
  20.  
  21. class Pie_obj : public Dessert
  22.     {
  23. public:
  24.     double cost() {return 1.50;}
  25.     };
  26.  
  27. class Cake_obj : public Dessert
  28.     {
  29. public:
  30.     };
  31.  
  32. class Entree
  33.     {
  34. public:
  35.     virtual double cost()
  36.         { printf("Entree error, no cost() provided\n"); return 0;}
  37.     };
  38.  
  39. class Fish_obj : public Entree
  40.     {
  41. public:
  42.     double cost() {return 4.00;}
  43.     };
  44.  
  45. class Steak_obj : public Entree
  46.     {
  47. public:
  48.     double cost() {return 7.50;}
  49.     };
  50.  
  51. class Appetizer
  52.     {
  53. public:
  54.     virtual double cost() 
  55.         { printf("Appetizer error, no cost() provided\n"); return 0;}
  56.     };
  57.  
  58. class Cocktail_obj : public Appetizer
  59.     {
  60. public:
  61.     double cost() { return 2.00;}
  62.     };
  63.  
  64. class Melon_obj : public Appetizer
  65.     {
  66. public:
  67.     double cost() { return .85;}
  68.     };
  69.  
  70. class Meal 
  71.     {
  72.     Appetizer *a;
  73.     Entree *e;
  74.     Dessert *d;
  75. public:
  76.     Meal(APPETIZER=Melon,ENTREE=Fish,DESSERT=Jello);
  77.     ~Meal();
  78.     double cost();
  79.     };
  80.  
  81. //-------------------------------------------
  82. // class member function definitions
  83.  
  84. double Meal::cost() {return d->cost() + a->cost() + e->cost(); }
  85.  
  86. Meal::Meal(APPETIZER aval,ENTREE eval,DESSERT dval)
  87.     {
  88.     if ( dval == Jello ) d = new Jello_obj;
  89.     else if ( dval == Pie ) d = new Pie_obj;
  90.     else d = new Cake_obj;
  91.     if ( eval == Steak ) e = new Steak_obj;
  92.     else e = new Fish_obj;
  93.     if ( aval == Melon ) a = new Melon_obj;
  94.     else a = new Cocktail_obj;
  95.     }
  96. Meal::~Meal() { delete a; delete e; delete d; }
  97. //--------------------------------------------
  98.  
  99. main()
  100.     {
  101.     Meal m1(Melon,Fish,Jello);
  102.     Meal m2(Melon,Steak,Pie);
  103.     Meal m3(ShrimpCocktail,Steak,Cake);
  104.     printf("Meal1 Price %6.2f, Meal2 price %6.2f, Meal3 price %6.2f\n"
  105.         ,m1.cost(),m2.cost(),m3.cost());
  106.     }
  107. // ---- sample output below -----
  108. Dessert error, no cost() provided
  109. Meal1 Price   5.45, Meal2 price   9.85, Meal3 price   9.50
  110.