home *** CD-ROM | disk | FTP | other *** search
/ Using Visual C++ 4 (Special Edition) / Using_Visual_C_4_Special_Edition_QUE_1996.iso / ch18 / salad.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-13  |  2.5 KB  |  136 lines

  1. #include <iostream.h>
  2.  
  3. // Some miscellaneous definitions we will need
  4. typedef enum {
  5.     WHOLE,
  6.     SHREDDED,
  7.     GRATED,
  8.     SLICED,
  9.     CHOPPED
  10. } FoodState;
  11.  
  12. // The top of the inheritance tree
  13. class Food {
  14. public:
  15.     // Constructor
  16.     Food(const FoodState = WHOLE);
  17.  
  18.     // Virtual methods - all food
  19.     // must be able to set and return
  20.     // its state. These functions also
  21.     // ensure that Food is polymorphic
  22.     // and can use RTTI.
  23.     virtual FoodState GetState() const;
  24.     virtual void SetState(const FoodState);
  25.  
  26. private:
  27.     // Private member data
  28.     FoodState theFoodState;
  29. };
  30.  
  31. // Food constructor
  32. Food::Food(const FoodState newState)
  33. {
  34.     SetState(newState);
  35. }
  36.  
  37. // Getter and setter virtual methods
  38. FoodState Food::GetState() const
  39. {
  40.     return theFoodState;
  41. }
  42.  
  43. void Food::SetState(const FoodState newState)
  44. {
  45.     theFoodState = newState;
  46. }
  47.  
  48. // Overload << so we can display our state
  49. ostream& operator<<(ostream& outstrm,
  50.                     Food&    theFood)
  51. {
  52.     switch(theFood.GetState()) {
  53.         case WHOLE:    outstrm << "Whole";
  54.                        break;
  55.         case SHREDDED: outstrm << "Shredded";
  56.                        break;
  57.         case GRATED:   outstrm << "Grated";
  58.                        break;
  59.         case SLICED:   outstrm << "Sliced";
  60.                        break;
  61.         case CHOPPED:  outstrm << "Chopped";
  62.                        break;
  63.         default:
  64.             outstrm << "Bad state!";
  65.     }
  66.     return outstrm;
  67. }
  68.  
  69.  
  70. // Individual food types
  71. class Apple : public Food {
  72. public:
  73.     void Chop() { SetState(CHOPPED); }
  74.     void Slice() { SetState(SLICED); }
  75. };
  76.  
  77. class Cheese : public Food {
  78. public:
  79.     void Grate() { SetState(GRATED); }
  80. };
  81.  
  82. class Lettuce : public Food {
  83. public:
  84.     void Shred() { SetState(SHREDDED); }
  85. };
  86.  
  87.  
  88. // Process a single ingredient
  89. void ProcessIngredient(Food* pIngredient)
  90. {
  91.     // Is this an Apple?
  92.     Apple* pApple =
  93.             dynamic_cast<Apple*>(pIngredient);
  94.     if (pApple) {
  95.         pApple->Chop();
  96.         return;
  97.     }
  98.  
  99.     // Is this a head of Lettuce?
  100.     Lettuce* pLettuce =
  101.             dynamic_cast<Lettuce*>(pIngredient);
  102.     if (pLettuce) {
  103.         pLettuce->Shred();
  104.         return;
  105.     }
  106.  
  107.     // Is this a piece of Cheese?
  108.     Cheese* pCheese =
  109.             dynamic_cast<Cheese*>(pIngredient);
  110.     if (pCheese)
  111.         pCheese->Grate();
  112.  
  113.     return;
  114. }
  115.  
  116. // LetÆs prepare a salad
  117. void main()
  118. {
  119.     Lettuce     MyLettuce;
  120.     Apple       MyApple;
  121.     Cheese      MyCheese;
  122.  
  123.     // Process the vegetables
  124.     ProcessIngredient(&MyLettuce);
  125.     ProcessIngredient(&MyApple);
  126.     ProcessIngredient(&MyCheese);
  127.     
  128.     // Show what weÆve done
  129.     cout << "The lettuce is ";
  130.     cout << MyLettuce << "\n";
  131.     cout << "The apple is ";
  132.     cout << MyApple << "\n";
  133.     cout << "The cheese is ";
  134.     cout << MyCheese << "\n";
  135. }
  136.