Question1:
Another way to store a collection of items is in a keyed bag. In this type of bag, whenever an item is added, the programmer using the bag also provides an integer called the key. Each item added to the keyed bag must have a unique key; two items cannot have the same key. So, the insertion function has the specification shown here:
void keyed_bag::insert
(const value_type& entry, int key);
// Precondition: size() < CAPACITY, and the
// bag does not yet contain any item with
// the given key.
// Postcondition: A new copy of entry has
// been added to the bag, with the given key.
When the programmer wants to remove an item from a keyed bag, the key of the item must be specified, rather than the item itself. The keyed bag should also have a boolean member function that can be used to determine whether the bag has an item with a specified key.
A keyed bag differs from the bag with receipts (in the previous project). In a keyed bag, the programmer using the class specifies a particular key when an item is inserted. In contrast, for a bag with receipts, the insert function returns a receipt, and the programmer using the class has no control over what that receipt might be.
For this project, do a complete specification, design, and implementation of a keyed bag.
My answer:
To be continued…
Data Structures and Other Objects Using C++ ( Fourth Edition ) Michael Main, Walter Savitch. p180 ↩︎