Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Imagine you just left a store with a bag of groceries. You are concerned that the fragile items will not survive the trip home, so

  1. Imagine you just left a store with a bag of groceries. You are concerned that the fragile items will not survive the trip home, so when you reach your car, you place those items into their own bag. Using the templated version of the Bag class from Chapter 1 (one file version with header and implementation combined that is posted on Blackboard) write C++ statements that remove all the fragile items (eggs and bread) from storeBag place them into fragileBag. Print the contents of both bags using a method named displayBagContents that you added to the Bag class.
  2. Again suppose that a bag contains strings that represent various grocery items. Write a 2 argument C++ function that removes and counts all occurrences of a given string from such a bag. Your function should return the number of items removed. Print the count returned and the contents of the bag after the function executes displayBagContents member function that you added to the Bag class.

 @file Bag.h */ //#include  #ifndef _BAG #define _BAG template  class Bag { private: static const int DEFAULT_BAG_SIZE = 6; ItemType items[DEFAULT_BAG_SIZE]; // array of bag items int itemCount; // current count of bag items int maxItems; // max capacity of the bag // Returns either the index of the element in the array items that // contains the given target or -1, if the array does not contain // the target. int getIndexOf(const ItemType& target) const; public: Bag(); int getCurrentSize() const; bool isEmpty() const; bool add(const ItemType& newEntry); bool remove(const ItemType& anEntry); void clear(); bool contains(const ItemType& anEntry) const; int getFrequencyOf(const ItemType& anEntry) const; }; // end Bag template Bag::Bag() : itemCount(0), maxItems(DEFAULT_BAG_SIZE) { } // end default constructor template int Bag::getCurrentSize() const { return itemCount; } // end getCurrentSize template bool Bag::isEmpty() const { return itemCount == 0; } // end isEmpty template bool Bag::add(const ItemType& newEntry) { bool hasRoomToAdd = (itemCount < maxItems); if (hasRoomToAdd) { items[itemCount] = newEntry; itemCount++; } // end if return hasRoomToAdd; } // end add template bool Bag::remove(const ItemType& anEntry) { int locatedIndex = getIndexOf(anEntry); bool canRemoveItem = !isEmpty() && (locatedIndex > -1); if (canRemoveItem) { itemCount--; items[locatedIndex] = items[itemCount]; } // end if return canRemoveItem; } // end remove template void Bag::clear() { itemCount = 0; } // end clear template int Bag::getFrequencyOf(const ItemType& anEntry) const { int frequency = 0; int searchIndex = 0; while (searchIndex < itemCount) { if (items[searchIndex] == anEntry) { frequency++; } // end if searchIndex++; } // end while return frequency; } // end getFrequencyOf template bool Bag::contains(const ItemType& anEntry) const { return getIndexOf(anEntry) > -1; } // end contains /* ALTERNATE 1 template bool Bag::contains(const ItemType& anEntry) const { return getFrequencyOf(anEntry) > 0; } // end contains */ /* ALTERNATE 2 template bool Bag::contains(const ItemType& anEntry) const { bool found = false; for (int i = 0; !found && (i < itemCount); i++) { if (anEntry == items[i]) { found = true; } // end if } // end for return found; } // end contains */ // private template int Bag::getIndexOf(const ItemType& target) const { bool found = false; int result = -1; int searchIndex = 0; // if the bag is empty, itemCount is zero, so loop is skipped while (!found && (searchIndex < itemCount)) { if (items[searchIndex] == target) { found = true; result = searchIndex; } else { searchIndex++; } // end if } // end while return result; } // end getIndexOf #endif

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions