home *** CD-ROM | disk | FTP | other *** search
-
- /*
- * meal2.cpp - - Second 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:
- * "Unlike many OOP languages, C++ ensures at compile time that if you invoke
- * the cost () function on a dessert object, that function will exist. If
- * you don't redeclare the function on a derived class, then the base class
- * stopper function is invoked. Is there a better way? Yes, pure abstract
- * classes."
- */
-
-
- #include <iostream.h>
- #include <stdio.h>
-
-
- enum ENTREE { Steak, Fish};
- enum DESSERT { Pie, Cake, Jello };
- enum APPETIZER { Melon, ShrimpCocktail };
-
-
-
- ///////////////////////////////////////////////////////////////////////////
- class Dessert // Absract class - never instantiated by itself
- // the cost function below is to warn of a programming
- // problem.
- {
- public:
- virtual double cost ()
- { printf ("Dessert error, no cost () provided\n");
- return 0; }
- };
-
-
- class Jello_obj : public Dessert
- {
- public:
- double cost () { return .60; }
- };
-
- class Pie_obj : public Dessert
- {
- public:
- double cost () { return 1.50; }
- };
-
-
- class Cake_obj : public Dessert
- {
- public:
- };
-
- ///////////////////////////////////////////////////////////////////////////
-
- class Entree
- {
- public:
- virtual double cost ()
- { printf ("Entree error, no cost () provided\n");
- return 0; }
- };
-
-
- class Fish_obj : public Entree
- {
- public:
- double cost () { return 4.00; }
- };
-
- class Steak_obj : public Entree
- {
- public:
- double cost () { return 7.50; }
- };
-
-
- ///////////////////////////////////////////////////////////////////////////
-
- class Appetizer
- {
- public:
- virtual double cost ()
- { printf ("Appetizer error, no cost () provided\n");
- return 0; }
- };
-
-
- class Cocktail_obj : public Appetizer
- {
- public:
- double cost () { return 2.00; }
- };
-
- class Melon_obj : public Appetizer
- {
- public:
- double cost () { return .85; }
- };
-
- ///////////////////////////////////////////////////////////////////////////
-
- class Meal
- {
- Appetizer *a;
- Entree *e;
- Dessert *d;
-
- public:
- Meal (APPETIZER = Melon, ENTREE = Fish, DESSERT = Jello);
- ~Meal ();
- double cost ();
- };
-
-
- ///////////////////////////////////////////////////////////////////////////
- // Class member method definitions
-
- double Meal::cost () { return d->cost () + a->cost () + e->cost (); }
-
- Meal::Meal (APPETIZER aval, ENTREE eval, DESSERT dval)
- {
- if (dval == Jello) d = new Jello_obj;
- else if (dval == Pie) d = new Pie_obj;
- else d = new Cake_obj;
-
- if (eval == Steak) e = new Steak_obj;
- else e = new Fish_obj;
-
- if (aval == Melon) a = new Melon_obj;
- else a = new Cocktail_obj;
- }
-
- ///////////////////////////////////////////////////////////////////////////
-
- Meal::~Meal () { delete a; delete e; delete d; }
-
-
- ///////////////////////////////////////////////////////////////////////////
-
- void main (void)
- {
- Meal m1 (Melon, Fish, Jello);
- Meal m2 (Melon, Steak, Pie);
- Meal m3 (ShrimpCocktail, Steak, Cake);
-
- printf ("Meal1 Price: %6.2f, Meal2 Price: %6.2f, Meal3 Price: %6.2f\n",
- m1.cost (), m2.cost (), m3.cost ());
- }