home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 2.ddi / MEALS.ZIP / MEAL2.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-31  |  3.7 KB  |  158 lines

  1.  
  2. /*
  3.  * meal2.cpp - - Second attempt at Object Oriented Design.
  4.  *
  5.  *
  6.  *                 Taken from The C Users Journal
  7.  *
  8.  *                 "Pricing A Meal: An Object-Oriented Example In C++"
  9.  *                 by Chales Havner
  10.  *
  11.  *                 August 1990 Volume 8, Number 8
  12.  *
  13.  * Comments by the author:
  14.  * "Unlike many OOP languages, C++ ensures at compile time that if you invoke
  15.  * the cost () function on a dessert object, that function will exist.  If
  16.  * you don't redeclare the function on a derived class, then the base class
  17.  * stopper function is invoked.  Is there a better way?  Yes, pure abstract
  18.  * classes."
  19.  */
  20.  
  21.  
  22. #include <iostream.h>
  23. #include <stdio.h>
  24.  
  25.  
  26. enum ENTREE        { Steak, Fish};
  27. enum DESSERT       { Pie, Cake, Jello };
  28. enum APPETIZER     { Melon, ShrimpCocktail };
  29.  
  30.  
  31.  
  32. ///////////////////////////////////////////////////////////////////////////
  33. class Dessert      // Absract class - never instantiated by itself
  34.                    // the cost function below is to warn of a programming
  35.                    // problem.
  36. {
  37. public:
  38.    virtual double  cost ()
  39.                        { printf ("Dessert error, no cost () provided\n");
  40.                            return 0; }
  41. };
  42.  
  43.  
  44. class Jello_obj : public Dessert
  45. {
  46. public:
  47.    double          cost () { return .60; }
  48. };
  49.  
  50. class Pie_obj : public Dessert
  51. {
  52. public:
  53.    double          cost () { return 1.50; }
  54. };
  55.  
  56.  
  57. class Cake_obj : public Dessert
  58. {
  59. public:
  60. };
  61.  
  62. ///////////////////////////////////////////////////////////////////////////
  63.  
  64. class Entree
  65. {
  66. public:
  67.    virtual double  cost ()
  68.                        { printf ("Entree error, no cost () provided\n");
  69.                            return 0; }
  70. };
  71.  
  72.  
  73. class Fish_obj : public Entree
  74. {
  75. public:
  76.    double          cost () { return 4.00; }
  77. };
  78.  
  79. class Steak_obj : public Entree
  80. {
  81. public:
  82.    double          cost () { return 7.50; }
  83. };
  84.  
  85.  
  86. ///////////////////////////////////////////////////////////////////////////
  87.  
  88. class Appetizer
  89. {
  90. public:
  91.    virtual double  cost ()
  92.                        { printf ("Appetizer error, no cost () provided\n");
  93.                            return 0; }
  94. };
  95.  
  96.  
  97. class Cocktail_obj : public Appetizer
  98. {
  99. public:
  100.    double          cost () { return 2.00; }
  101. };
  102.  
  103. class Melon_obj : public Appetizer
  104. {
  105. public:
  106.    double          cost () { return .85; }
  107. };
  108.  
  109. ///////////////////////////////////////////////////////////////////////////
  110.  
  111. class Meal
  112. {
  113.    Appetizer       *a;
  114.    Entree          *e;
  115.    Dessert         *d;
  116.  
  117. public:
  118.    Meal (APPETIZER = Melon, ENTREE = Fish, DESSERT = Jello);
  119.    ~Meal ();
  120.    double          cost ();
  121. };
  122.  
  123.  
  124. ///////////////////////////////////////////////////////////////////////////
  125. // Class member method definitions
  126.  
  127. double Meal::cost () { return d->cost () + a->cost () + e->cost (); }
  128.  
  129. Meal::Meal (APPETIZER aval, ENTREE eval, DESSERT dval)
  130. {
  131.    if      (dval == Jello)     d = new Jello_obj;
  132.    else if (dval == Pie)       d = new Pie_obj;
  133.    else                        d = new Cake_obj;
  134.  
  135.    if      (eval == Steak)     e = new Steak_obj;
  136.    else                        e = new Fish_obj;
  137.  
  138.    if      (aval == Melon)     a = new Melon_obj;
  139.    else                        a = new Cocktail_obj;      
  140. }
  141.  
  142. ///////////////////////////////////////////////////////////////////////////
  143.  
  144. Meal::~Meal () { delete a; delete e; delete d; }
  145.  
  146.  
  147. ///////////////////////////////////////////////////////////////////////////
  148.  
  149. void main (void)
  150. {
  151.    Meal            m1 (Melon, Fish, Jello);
  152.    Meal            m2 (Melon, Steak, Pie);
  153.    Meal            m3 (ShrimpCocktail, Steak, Cake);
  154.  
  155.    printf ("Meal1 Price: %6.2f, Meal2 Price: %6.2f, Meal3 Price: %6.2f\n",
  156.            m1.cost (), m2.cost (), m3.cost ());
  157. }
  158.