Answers to Exercises

1.Here is the new function declaration that needs to be added to the StockItem interface definition (from codeitem6.h):
void Write(ofstream& s);

and the one to be added to the Inventory interface definition (from codeinvent2.h):

void StoreInventory(ofstream& OutputStream);

2.Figure 6.36 shows the implementation of the Write member function for StockItem, and Figure 6.37 is the implementation of the StoreInventory member function of the Inventory class. As you can see, neither of these functions is tremendously complex or, for that matter, very different from the Display function.
Figure 6.36. The Write member function for the StockItem class (from codeitem6.cpp
void StockItem::Write(ofstream& os)
{
  os << m_Name << endl;
  os << m_InStock << endl;
  os << m_Price << endl;
  os << m_Distributor << endl;
  os << m_UPC << endl;
  return;
}

Figure 6.37. The StoreInventory member function for the Inventory class (from codeinvent2.cpp)
void Inventory::StoreInventory(ofstream& os)
{
  short i;

  for (i = 0; i < m_StockCount; i ++)
     m_Stock[i].Write(os);
}

Finally, Figure 6.38 shows the changes needed to the application program to write the updated inventory back to a new file.

Figure 6.38. The changes to the application program (from codeitemtst6.cpp)
ofstream OutputStream("shop2.out");
MyInventory.StoreInventory(OutputStream);

Of course in a real program, it would probably be better to write the updated inventory back to the original file, so that the next time we ran the program the updated inventory would be used. However, in the case of a test application, it's simpler to avoid modifying the input file so we can run the same test again if necessary.

Assuming that you've installed the software from the CD in the back of this book, you can try out this program. First, you have to compile it by following the compilation instructions on the CD. Then type itemtst6 to run the program. When the program asks for a UPC you can use 7904886261, which is the (made-up) UPC for "antihistamines". When the program asks you for a transaction code, type S for "sale" or P for "price check" and then hit ENTER.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset