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

  1.  
  2. /*
  3.  * meal1.cpp - - First attempt at Object Oriented Design.
  4.  *
  5.  *                 Taken from The C Users Journal
  6.  *
  7.  *                 "Pricing A Meal: An Object-Oriented Example In C++"
  8.  *                 by Chales Havner
  9.  *
  10.  *                 August 1990 Volume 8, Number 8
  11.  *
  12.  * Comments by the author:
  13.  * "The cost method in the Meal class simply invokes the others, i.e.
  14.  * a.cost() + e.cost () + d.cost () to obtain the total cost.  The little
  15.  * red flag should go up!  This is not good OOD (object-oriented design).
  16.  * Whenever you would use a case statement in the calss implementation,
  17.  * something is probably wrong.  As my OOP (object-oriented programming)
  18.  * instructor often said, 'narrow and deep, narrow and deep'.  The class
  19.  * hierarchy is not deep enough.  We need more objects, one for each kind
  20.  * of Appetizer, etc."
  21.  */
  22.  
  23.  
  24. #include <iostream.h>
  25. #include <stdio.h>
  26.  
  27.  
  28. enum ENTREE        { SteakPlatter, Fish };
  29. enum DESSERT       { Pie, Cake, Jello };
  30. enum APPETIZER     { Melon, ShrimpCocktail };
  31.  
  32.  
  33. class Dessert
  34. {
  35.    int             kind;
  36. public:
  37.    Dessert (int what = Pie) { kind = what; }
  38.    cost ();
  39. };
  40.  
  41. class Entree
  42. {
  43.    int             kind;
  44. public:
  45.    Entree (int what = SteakPlatter) { kind = what; }
  46.    cost ();
  47. };
  48.  
  49.  
  50. class Appetizer
  51. {
  52.    int             kind;
  53. public:
  54.    Appetizer (int what = Melon) { kind = what; }
  55.    cost ();
  56. };
  57.  
  58. class Meal
  59. {
  60.    Appetizer       a;
  61.    Entree          e;
  62.    Dessert         d;
  63. public:
  64.    Meal (APPETIZER = Melon, ENTREE = Fish, DESSERT = Jello);
  65.    cost ();
  66. };
  67.  
  68.  
  69. //////////////////////////////////////////////////////////////////
  70. // class member method definitions
  71.  
  72. int Meal::cost () { return 1; }
  73. Meal::Meal (APPETIZER aval, ENTREE eval, DESSERT dval) :
  74.            a(aval), e(eval), d(dval) { }
  75.  
  76.  
  77. //////////////////////////////////////////////////////////////////
  78.  
  79. void main (void)
  80. {
  81.    Meal            m(Melon, Fish, Jello);
  82.  
  83.  
  84.    printf ("Price %d\n", m.cost());
  85. }
  86.  
  87.