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

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