Question
C++ code Imagine you just left a store with a bag of groceries. You are concerned that the fragile items will not survive the trip
C++ code
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 template version of the Bag class from Chapter 1 (one file version with .h and .cpp combined posted on Blackboard) do the following:
- Create 2 instances of Bag objects that hold string values that represent grocery items.
- Add grocery items to storeBag (input from the keyboard or file).
- Write C++ statements in main that remove all the fragile items (eggs and bread) from storeBag and place them into fragileBag. Note: Your code must work for any Bag of groceries.
- Print the contents of both bags using a method named displayBagContents that you added to the Bag class, i.e. a public member function.
- Use a 1 argument function (the parameter is a string) you called in main that removes and counts all occurrences of the given string from the Bag object. Your function should return the number of items removed.
- Print the count returned by the function and the contents of the bag after the function executes using the displayBagContents member function that you added to the Bag class.
- write getFrequencyOf function which returns number of times item occur in the bag
Bag.cpp
// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2016 __Pearson Education__. All rights reserved.
/** @file Bag.cpp (same as ArrayBag) */
#include "Bag.h"
#include
template
Bag
{
} // end default constructor
template
int Bag
{
return itemCount;
} // end getCurrentSize
template
bool Bag
{
return itemCount == 0;
} // end isEmpty
template
bool Bag
{
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
{
itemCount = 0;
} // end clear
template
bool Bag
{
return getIndexOf(anEntry) > -1;
} // end contains
/* ALTERNATE 1
template
bool Bag
{
return getFrequencyOf(anEntry) > 0;
} // end contains
*/
/* ALTERNATE 2
template
bool Bag
{
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
*/
template
std::vector
{
std::vector
for (int i = 0; i < itemCount; i++)
bagContents.push_back(items[i]);
return bagContents;
} // end toVector
// private
template
int Bag
{
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
Bag.h
/ Created by Frank M. Carrano and Tim Henry. // Copyright (c) 2016 __Pearson Education__. All rights reserved. /** ADT bag: Array-based implementation. @file Bag.h */ #ifndef BAG_ #define BAG_ #include#include "Bag.cpp" 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; std::vector toVector() const; }; // end Bag #endif
I want C++ code for the above question and output.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started