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.

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. Handle the possibility that the given bag is either empty or does not contain any occurrences of the given string. Print the count returned and the contents of the bag after the function executes displayBagContents member function that you added to the Bag class.

Respuesta :

Answer:

Explanation:

#include <iostream>

using namespace std;

#include <iostream> // For cout and cin

#include <string>   // For string objects

#include "Bag.h"    // For ADT bag

using namespace std;

int main()

{

int y=0;

string clubs[] = { "eggs", "eggs", "eggs", "orange", "bread", "bread" };

Bag<string> grabBag;

Bag<string> fragileBag;

for (int x = 0; x < 6; x++) {

grabBag.add(clubs[x]);

}

int count = 0;

for (int q = 0; q < 6 ; q++)

{

if (clubs[y] == "eggs" || clubs[y] == "bread") {

fragileBag.add(clubs[y]);

if(grabBag.remove("eggs")){

   ++count;

}

}

y++;

}

cout << "fragile bag contains:" << endl;

fragileBag.displayBagContents();

cout << "grocery bag contains:" << endl;

grabBag.displayBagContents();

cout << "Number of items removed from grocery bag : " << count<<endl;

return 0;

};