// FILE: bag4.h // TEMPLATE CLASS PROVIDED: // Bag (a container template class for a collection of items) // The template parameter, Item, is the data type of the items in the Bag. // It may be any of the C++ built-in types (int, char, etc.), or a class // with a default constructor, an assignment operator, and operators to test // for equality (x == y) and non-equality (x != y). // // MEMBER CONSTANT for the Bag template class: // static const size_t DEFAULT_CAPACITY = _____ // Bag::DEFAULT_CAPACITY is the initial capacity of a Bag that is // created by the default constructor. // // CONSTRUCTOR for the Bag template class: // Bag(size_t initial_capacity = DEFAULT_CAPACITY) // Postcondition: The Bag is empty with an initial capacity given by the // parameter. The insert function will work efficiently (without allocating // new memory) until this capacity is reached. // // MODIFICATION MEMBER FUNCTIONS for the Bag template class: // void resize(size_t new_capacity) // Postcondition: The Bag's current capacity is changed to new_capacity // (but not less than the number of items currently in the Bag). The insert // function will work efficiently without allocating new memory) until the // capacity is reached. // // void insert(const Item& entry) // Postcondition: A new copy of entry has been added to the Bag. // // void remove(const Item& target) // Postcondition: If target was in the Bag, then one copy of target has // been removed from the Bag; otherwise the Bag is unchanged. // // void operator +=(const Bag& addend) // Postcondition: Each item in addend has been added to this Bag. // // CONSTANT MEMBER FUNCTIONS for the Bag template class: // size_t size( ) const // Postcondition: Return value is the total number of items in the Bag. // // size_t occurrences(const Item& target) const // Postcondition: Return value is number of times target is in the Bag. // // Item grab( ) const // Postcondition: The return value is a randomly selected item from the Bag. // // NON-MEMBER FUNCTIONS for the Bag template class: // template // Bag operator +(const Bag& b1, const Bag& b2) // Postcondition: The Bag returned is the union of b1 and b2. // // VALUE SEMANTICS for the Bag template class: // Assignments and the copy constructor may be used with Bag objects. // // DYNAMIC MEMORY USAGE by the Bag template class: // If there is insufficient dynamic memory, then the following functions call // new_handler: the constructors, resize, insert, operator += , operator +, // and the assignment operator. #ifndef BAG4_H #define BAG4_H #include // Provides size_t template class Bag { public: // MEMBER CONSTANTS // enum { DEFAULT_CAPACITY = 30 }; // Or: static const size_t DEFAULT_CAPACITY = 30; // CONSTRUCTORS and DESTRUCTOR Bag(size_t initial_capacity = DEFAULT_CAPACITY); Bag(const Bag& source); ~Bag( ); // MODIFICATION MEMBER FUNCTIONS void resize(size_t capacity); void insert(const Item& entry); void remove(const Item& target); void operator +=(const Bag& addend); void operator =(const Bag& source); // CONSTANT MEMBER FUNCTIONS size_t size( ) const { return used; } size_t occurrences(const Item& target) const; Item grab( ) const; // FRIEND FUNCTIONS friend Bag operator +(const Bag& b1, const Bag& b2); private: Item *data; // Pointer to partially filled dynamic array size_t used; // How much of array is being used size_t capacity; // Current capacity of the Bag }; #include "bag4.tem" // Include the implementation #endif