Dana Vrajitoru
C201/I211 Computer Programming II

Homework 8

Due Date: Wednesday, November 1, 2006.

Ex. 1 Download the updated version of the previous homework in a new folder.
product.h
product.cpp
inventory.h
inventory.cpp
main.cpp

Create a project just like for the previous homework and include these three files. If you compile this on Linux, here is a Makefile for this project.

Download the file data8.txt in the folder where you created the project.

Copy all the functions you implemented in the previous homework into this new one. If you haven't turned in homework 7, you can turn in 7 and 8 as a single program.

Ex. 1. a. Uncomment the prototype of the friend function operator>> in the class Product. Implement this function in the file product.cc. This function should do the same thing as the function Input, using the parameter "in" instead of the "cin".

b. Implement an operator += in the class Product as a class method. This operator should take one parameter which should be an integer number representing a quantity. The functionality of this operator should be to add this quantity to the target object, the same way the function Add does. This operator should return a reference to the target object as the returned value, the way the operator = does. Replace the call to the function Add in the function Add_by_name in the class Inventory with a call to this operator you implemented.

c. Implement the operator > in the class Product as a class method. The prototype for this operator should be:

bool operator>(Product &data);
This operator should return true if the price of the target object is greater than the price of the object data.

Ex. 2. a. Add a function to the class Inventory that prints out the most expensive item in the shop. This function does not need any parameter and does not need to return a value. Use the operator > defined in the class Product to implement this function. Add an option in the menu in the main to test this function.

b. Add a function to the class inventory with the following prototype:

bool Output_file(char *filename);
This function should open an output file stream by the specified name (the parameter). It should write the number of items on the first line in the file, then all of the items one by one, in a way that should be compatible with the function Input_file. Do not write any comments around the values, but only the values of the data members of each product in the same order as you find them in the function Input. Close the file before exiting the function. If the file cannot be opened, print an error message and return false. Otherwise return true.

Note. This function should output the type in each item as an integer, to be compatible with the Input_file function. For that you'll have to cast the type as an integer before you output it.

The goal of this function is to write a file that we can later read back into our program and find the same information. Add an option to the menu in the main to save the inventory to a file. You will have to ask the user for a file name and pass it to this function.

Ex. 3 (optional, 4 extra credit points) Add two operators to input and output a value of type Category from the cout or cin. Integrate these operators in the Input and Output functions in the class Product. The input operator should read the category as an integer and convert it to a Category value. The output function should output a string (like food, cloth, etc.).