// z5.3-override.cc // #include #include using namespace std; class GenericItem { public: void SetName(string newName) { itemName = newName; }; void SetQuantity(int newQty) { itemQuantity = newQty; }; void PrintItem() { cout << itemName << " " << itemQuantity << endl; }; protected: string itemName; int itemQuantity; }; class ProduceItem : public GenericItem { // Derived from GenericItem public: void SetExpiration(string newDate) { expirationDate = newDate; }; string GetExpiration() { return expirationDate; }; void PrintItem() { cout << itemName << " " << itemQuantity << " (Expires: " << expirationDate << ")" << endl; }; private: string expirationDate; }; int main() { GenericItem miscItem; ProduceItem perishItem; miscItem.SetName("Smith Cereal"); miscItem.SetQuantity(9); miscItem.PrintItem(); // Calls GenericItem's PrintItem() perishItem.SetName("Apples"); perishItem.SetQuantity(40); perishItem.SetExpiration("May 5, 2012"); perishItem.PrintItem(); // Calls ProduceItem's PrintItem() return 0; }