BIG C++ Cay Horstmann & Timothy Budd
Laboratory Notebook Chapter 26 – An Introduction to Design Patterns
Your name:
Your email address:
Your student ID number:
Once this form has been customized for your institution, you can use this button to send your lab work. Be sure to read the instructions before starting your work.
To gain experience with
· the advantages of iterators
· the pattern concept
· several common design patterns: ITERATOR, ADAPTER, TEMPLATE METHOD, STRATEGY, and COMPOSITE
· patterns used in the standard C++ library
· putting patterns to work in a complex program
P1. Preliminary Programming
Assume you are writing software for a restaurant. In preparation for this task and subsequent problems within this lab, you will need to implement two classes. One for representing the nutritional value of an item of food and the other for representing a drink. First, create a base class called FoodItem. This class should contain a number of items such as calorie count, amount of fat, amount of sodium, and amount of carbohydrates. Include constructors and accessor functions as indicated by the class definition provided below. Also, include a single function called printNutritionLabel(). This function should neatly print out a nutrition label for the food to the screen. The foodName variable should store the name of the food represented by the object. (i.e., french fries, hamburger, etc.).
class FoodItem
{
public:
FoodItem(string foodName, double iSodium, double iFat,
double iCarbohydrates, double iSugar, int iCalories);
string getFoodName();
int getNumCalories();
double getGramsSodium();
double getGramsFat();
double getGramsSugar();
double getGramsCarbohydrates();
private:
string foodName;
int numCalories;
double gramsSodium;
double gramsFat;
double gramsSugar;
double gramsCarbohydrates;
};
Now, implement a class called DrinkItem. A drink item is very similar to a FoodItem. Implement the DrinkItem class according to the definition supplied below.
class DrinkItem
DrinkItem(string drinkName, int iCalories, double iCarbs,
double iSugar);
Supply your answer in the space below
You will now get some practice in writing adapter patterns. A drink may be considered a type of food. Therefore, it may be useful for you to create an adapter for converting a DrinkItem to a FoodItem. Define and implement such as class in the space below.
Write a short main() function to test the three classes written in the previous problems within this lab. You should create both drink and food items and test your adapter class for converting from drink to food.
P3. More Testing
Modify your main() function from Problem P2 such that it tests the new functionality.
Insert all code in the space below.
R3. Explanation
P4. Composite Classes
In this problem, you will learn to program with the composite pattern. Define and implement a ValueMeal class that can store a number of FoodItem objects as specified by the constructor. The ValueMeal class is a composite class. The ValueMeal should contain a function called printNutritionLabel() that adds up the various nutrition fields from each class that ValueMeal is composed of and then prints this totaled nutrition label neatly to the screen.
Do not forget to send your answers when you are finished.