home *** CD-ROM | disk | FTP | other *** search
-
- /*
- * meal1.cpp - - First attempt at Object Oriented Design.
- *
- * Taken from The C Users Journal
- *
- * "Pricing A Meal: An Object-Oriented Example In C++"
- * by Chales Havner
- *
- * August 1990 Volume 8, Number 8
- *
- * Comments by the author:
- * "The cost method in the Meal class simply invokes the others, i.e.
- * a.cost() + e.cost () + d.cost () to obtain the total cost. The little
- * red flag should go up! This is not good OOD (object-oriented design).
- * Whenever you would use a case statement in the calss implementation,
- * something is probably wrong. As my OOP (object-oriented programming)
- * instructor often said, 'narrow and deep, narrow and deep'. The class
- * hierarchy is not deep enough. We need more objects, one for each kind
- * of Appetizer, etc."
- */
-
-
- #include <iostream.h>
- #include <stdio.h>
-
-
- enum ENTREE { SteakPlatter, Fish };
- enum DESSERT { Pie, Cake, Jello };
- enum APPETIZER { Melon, ShrimpCocktail };
-
-
- class Dessert
- {
- int kind;
- public:
- Dessert (int what = Pie) { kind = what; }
- cost ();
- };
-
- class Entree
- {
- int kind;
- public:
- Entree (int what = SteakPlatter) { kind = what; }
- cost ();
- };
-
-
- class Appetizer
- {
- int kind;
- public:
- Appetizer (int what = Melon) { kind = what; }
- cost ();
- };
-
- class Meal
- {
- Appetizer a;
- Entree e;
- Dessert d;
- public:
- Meal (APPETIZER = Melon, ENTREE = Fish, DESSERT = Jello);
- cost ();
- };
-
-
- //////////////////////////////////////////////////////////////////
- // class member method definitions
-
- int Meal::cost () { return 1; }
- Meal::Meal (APPETIZER aval, ENTREE eval, DESSERT dval) :
- a(aval), e(eval), d(dval) { }
-
-
- //////////////////////////////////////////////////////////////////
-
- void main (void)
- {
- Meal m(Melon, Fish, Jello);
-
-
- printf ("Price %d\n", m.cost());
- }
-