Source of Listing1-1.h


  1: //  Created by Frank M. Carrano and Timothy M. Henry.
  2: //  Copyright (c) 2017 Pearson Education, Hoboken, New Jersey.
  3: /** Listing 1-1.
  4:     @file BagInterface.h */
  5: #ifndef BAG_INTERFACE_
  6: #define BAG_INTERFACE_

  8: #include <vector>

 10: template<class ItemType>
 11: class BagInterface
 12: {
 13: public:
 14:    /** Gets the current number of entries in this bag.
 15:     @return  The integer number of entries currently in the bag. */
 16:    virtual int getCurrentSize() const = 0;
 17:    
 18:    /** Sees whether this bag is empty.
 19:     @return  True if the bag is empty, or false if not. */
 20:    virtual bool isEmpty() const = 0;
 21:    
 22:    /** Adds a new entry to this bag.
 23:     @post  If successful, newEntry is stored in the bag and
 24:        the count of items in the bag has increased by 1.
 25:     @param newEntry  The object to be added as a new entry.
 26:     @return  True if addition was successful, or false if not. */
 27:    virtual bool add(const ItemType& newEntry) = 0;
 28:    
 29:    /** Removes one occurrence of a given entry from this bag,
 30:        if possible.
 31:     @post  If successful, anEntry has been removed from the bag
 32:        and the count of items in the bag has decreased by 1.
 33:     @param anEntry  The entry to be removed.
 34:     @return  True if removal was successful, or false if not. */
 35:    virtual bool remove(const ItemType& anEntry) = 0;
 36:    
 37:    /** Removes all entries from this bag.
 38:     @post  Bag contains no items, and the count of items is 0. */
 39:    virtual void clear() = 0;
 40:    
 41:    /** Counts the number of times a given entry appears in this bag.
 42:     @param anEntry  The entry to be counted.
 43:     @return  The number of times anEntry appears in the bag. */
 44:    virtual int getFrequencyOf(const ItemType& anEntry) const = 0;
 45:    
 46:    /** Tests whether this bag contains a given entry.
 47:     @param anEntry  The entry to locate.
 48:     @return  True if bag contains anEntry, or false otherwise. */
 49:    virtual bool contains(const ItemType& anEntry) const = 0;
 50:    
 51:    /** Empties and then fills a given vector with all entries that
 52:        are in this bag.
 53:     @return  A vector containing all the entries in the bag. */
 54:    virtual std::vector<ItemType> toVector() const = 0;
 55:    
 56:    /** Destroys this bag and frees its assigned memory. (See C++ Interlude 2.) */
 57:    virtual ~BagInterface() { }
 58: }; // end BagInterface
 59: #endif