// FILE: bag4demo.cpp // Demonstration program for the 4th version of the bag (bag4.h and // bag4.tem). // This is a the same as the demonstration program for bag1, // except that we are now using a template class, and we no longer need to // check whether the bag reaches its capacity. #include // Provides cout and cin #include // Provides EXIT_SUCCESS #include "bag4.h" // Provides the Bag template class // PROTOTYPES for functions used by this demonstration program: void get_ages(Bag& ages); // Postcondition: The user has been prompted to type in the ages of family // members. These ages have been read and placed in the ages Bag, stopping // when the user types a negative number. void check_ages(Bag& ages); // Postcondition: The user has been prompted to type in the ages of family // members once again. Each age is removed from the ages Bag when it is typed, // stopping when the Bag is empty. int main( ) { Bag ages; get_ages(ages); check_ages(ages); cout << "May your family live long and prosper." << endl; return EXIT_SUCCESS; } void get_ages(Bag& ages) { int user_input; // An age from the user's family cout << "Type the ages in your family. "; cout << "Type a negative number when you are done:" << endl; cin >> user_input; while (user_input >= 0) { ages.insert(user_input); cin >> user_input; } } void check_ages(Bag& ages) { int user_input; // An age from the user's family cout << "Type those ages again. Press return after each age:" << endl; while (ages.size( ) > 0) { cin >> user_input; if (ages.occurrences(user_input) == 0) cout << "No, that age does not occur!" << endl; else { cout << "Yes, I've got that age and will remove it." << endl; ages.remove(user_input); } } }