Sample Assignment: A Bag with Receipts (Using Dynamic Arrays)
receipts.h:The header file for the bag_with_receipts class. Actually, you don't have to write much of this file. Just start with our version and add your name and other information at the top, and add your private member variables at the bottom. If some of your member functions are implemented as inline functions, then you may put those implementations in this file too. . Note that the header file has no CAPACITY constant because the items are stored in a dynamic array that grows as needed. But there is a DEFAULT_CAPACITY constant, which provides the initial size of the arrays created by the default constructor.
receipts.cxx:The implementation file for the new bag_with_receipts class. You will write all of this file, which will have the implementations of all the member functions.
Some helpful notes:
Start by declaring the new bag_with_receipts's private member variables in receipts.h. See the project description in the projects of Chapter 3 for some ideas, but remember that the arrays must be dynamic rather than fixed-size. You will also need two size_t variables to keep track of the number of items in the List and the total size of the dyamic arrays. After you've declared your member variables, write an invariant for the top of receipts.cxx.
Once again, do your work in small pieces. For example, my first version of the bag_with_receipts had only a constructor, insert, and occurrences. My other member functions started out as stubs.
Write an interactive test program and use the debugger to track down errors in your implementation. If you have an error, do not start making changes until you have identified the cause of the error.
When a member functions needs to increase the size of the dynamic array, it is a good idea to increase that size by at least 10% (rather than by just one item).