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

  1.  
  2. /*
  3.  * meal3.cpp - - Final 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.  * "C++ 2.0 allows virtual function declarations to be followed by =0;.  This
  15.  * somewhat strange syntax means every derived class must implement the
  16.  * function or again define it as pure virtual.  The compiler checks the
  17.  * declarations and issues an error message if this is not so.  The compiler
  18.  * check ensures that we don't inadvertently leave out the cost () function."
  19.  *
  20.  * "This third version adds some new features.  This week all diserts are 25%
  21.  * off except for Pies.  By adding a virtual discout () function to the
  22.  * Dessert abstract class we automatically, via inheritance, apply it to all
  23.  * derived desserts.  The Meal class's cost function was changed to multiply
  24.  * the dessert cost (d->cost ()) by the discount (d->discount())."
  25.  *
  26.  * "Since pies are the only desserts that have no discount, a discount ()
  27.  * multiplier of 1.0 is placed in the Pie_obj class.  Every object has a
  28.  * text () function to print information about itself.  The Meal::print ()
  29.  * member function in Listing 3 uses the standard stream I/O, which is type
  30.  * safe, instead of printf ().  In general, using cout, cin and cerr via the
  31.  * overloaded insertion << asnd extraction >> operators are safer than
  32.  * printf () and you can easily overload the operators for new class types
  33.  * you create; printf () only knowns about built-in types like double and
  34.  * char *.  However, overloading operators is material for another article."
  35.  *
  36.  * "The final design is clean and elegant.  The class framework models the
  37.  * real world problem domain directly.  Adding calorie information would be
  38.  * much like cost.  The class hierarchy actually needs to be deepened further
  39.  * to represent different kinds of cakes and pies.  Instead of an item's cost
  40.  * residing in the object, the cost () member function could access a
  41.  * database to fetch the most recent price increase.  This short example
  42.  * demonstrates just some of what OOP (and C++ in particular) has to offer."
  43.  */
  44.  
  45.  
  46. #include <iostream.h>
  47. #include <stdio.h>
  48.  
  49.  
  50. enum ENTREE        { Steak, Fish};
  51. enum DESSERT       { Pie, Cake, Jello };
  52. enum APPETIZER     { Melon, ShrimpCocktail };
  53.  
  54.  
  55. /////////////////////////////////////////////////////////////////////////
  56.  
  57. class Dessert      // An abstract class
  58.                    // - never instantiated by itself
  59. {
  60. public:
  61.    virtual double  cost () = 0;    // pure virtual ==> abstract class
  62.    virtual double  discount () { return .75; }     // 25% off
  63.    virtual const
  64.            char    *text () = 0;
  65. };
  66.  
  67.  
  68. class Jello_obj : public Dessert
  69. {
  70. public:
  71.    double          cost () { return .60; }
  72.    const char      *text () { return "Jello "; }
  73. };
  74.  
  75. class Pie_obj : public Dessert
  76. {
  77. public:
  78.    double          cost () { return 1.50; }
  79.    double          disccount () { return 1.00; }   // no discount
  80.    const char      *text () { return "Pie "; }
  81. };
  82.  
  83. class Cake_obj : public Dessert
  84. {
  85. public:
  86.    double          cost () { return 1.00; }
  87.    const char      *text () { return "Cake "; }
  88. };
  89.  
  90. /////////////////////////////////////////////////////////////////////////
  91.  
  92. class Entree
  93. {
  94. public:
  95.    virtual double  cost () = 0;
  96.    virtual const
  97.            char    *text () = 0;
  98. };
  99.  
  100. class Fish_obj : public Entree
  101. {
  102. public:
  103.    double          cost () { return 4.00; }
  104.    const char      *text () { return "Fish "; }
  105. };
  106.  
  107. class Steak_obj : public Entree
  108. {
  109. public:
  110.    double          cost () { return 7.50; }
  111.    const char      *text () { return "Steak "; }
  112. };
  113.  
  114. /////////////////////////////////////////////////////////////////////////
  115.  
  116. class Appetizer
  117. {
  118. public:
  119.    virtual double  cost () = 0;
  120.    virtual const
  121.            char    *text () = 0;
  122. };
  123.  
  124. class Cocktail_obj : public Appetizer
  125. {
  126. public:
  127.    double          cost () { return 2.00; }
  128.    const char      *text () { return "Cocktail "; }
  129. };
  130.  
  131. class Melon_obj : public Appetizer
  132. {
  133. public:
  134.    double          cost () { return .85; }
  135.    const char      *text () { return "Melon "; }
  136. };
  137.  
  138. /////////////////////////////////////////////////////////////////////////
  139.  
  140. class Meal
  141. {
  142.    Appetizer       *a;
  143.    Dessert         *d;
  144.    Entree          *e;
  145.  
  146. public:
  147.    Meal (APPETIZER = Melon, ENTREE = Fish, DESSERT = Jello);
  148.    ~Meal ();
  149.    double          cost ();
  150.    void            print ();
  151. };
  152.  
  153.  
  154. /////////////////////////////////////////////////////////////////////////
  155.  
  156. double Meal::cost () { return (d->cost () * d->discount () +
  157.                                a->cost () + e->cost ()); }
  158.  
  159. Meal::Meal (APPETIZER aval, ENTREE eval, DESSERT dval)
  160. {
  161.    if      (aval == Melon)     a = new Melon_obj;
  162.    else                        a = new Cocktail_obj;
  163.  
  164.    if      (dval == Jello)     d = new Jello_obj;
  165.    else if (dval == Pie)       d = new Pie_obj;
  166.    else                        d = new Pie_obj;
  167.  
  168.    if      (eval == Steak)     e = new Steak_obj;
  169.    else                        e = new Fish_obj;
  170.  
  171. }
  172.  
  173. Meal::~Meal () { delete a; delete e; delete d; }
  174.  
  175.  
  176. void Meal::print ()
  177. {
  178.    cout << a->text () << e->text () << d->text () << ", Meal Cost = " <<
  179.        a->cost () + e->cost () + d->cost () * d->discount () << "\n";
  180. }
  181.  
  182. /////////////////////////////////////////////////////////////////////////
  183.  
  184. void main (void)
  185. {
  186.    Meal            m1 (Melon, Fish, Jello);
  187.    Meal            m2 (Melon, Steak, Cake);
  188.    Meal            m3 (ShrimpCocktail, Steak, Cake);
  189.  
  190.  
  191.    m1.print ();
  192.    m2.print ();
  193.    m3.print ();
  194. }
  195.