Question: This is for an intro computer science course in c++. I need to create a class definiton for a Bag ADT. This class has two
This is for an intro computer science course in c++.
I need to create a class definiton for a Bag ADT. This class has two important data members. The class has the following attributes:
n, an integer, (the number of items in the bag)
an array of strings (the contents of the bag)
and the following member functions
add() - to add an item to the bag
show() - to list all of the items in the bag
remove() - to remove an item from the bag
isEmpty() - to see whether the bag is empty.
clear() - to empty the bag
Also implement another class (the Item ADT). Instead of the data implementation being an array of strings, make it an array of Item objects.
This means that your public member functions of the Bag ADT will need to be revised to handle Item objects, not just strings.
Please completely code the class definitions and the member functions fully and show that they work.
I need the class definitions and their member functions to be able to be tested by this test program.
#include#include #include "bag.h" using namespace std; int main() { // Declarations Bag groceries; // default ctor string item; // test add items cout << "*** TEST 1 add() "; groceries.add("milk"); cout << "added milk "; groceries.add("meat"); cout << "added meat "; groceries.add("carrots"); cout << "added carrots "; groceries.add("fish"); cout << "added fish "; // test display() cout << "*** TEST 2 display() "; groceries.display(); // test remove() cout << "*** TEST 3 remove() "; groceries.remove("carrots"); groceries.display(); // test clear() cout << "*** TEST 4 clear() "; groceries.clear(); groceries.display(); // test isEmpty() cout << "*** TEST 5 isEmpty() "; if (groceries.isEmpty()) cout << "Bag is empty "; else cout << "Bag is not empty "; // TEST add() boolean return cout << "*** TEST 6 add() boolean return "; cout << "Enter an item or 'quit': "; cin >> item; while (item != "quit") { if (!(groceries.add(item))) cout << "ERROR - bag is full! "; cout << "Enter an item or 'quit': "; cin >> item; } groceries.display(); // TEST contains() cout << "*** TEST 7 contains() "; cout << "Enter an item to search for: "; cin >> item; if (groceries.contains(item)) cout << item << " found "; else cout << item << "not found "; cout << " Adding two more of " << item << " to the list "; groceries.add(item); groceries.add(item); groceries.display(); // test of getfrequency Of() cout << "*** TEST 8 getFrequencyOf() "; cout << item << " is in the list " << groceries.getFrequencyOf(item) << " times "; cout << " ****** TEST COMPLETE ****** "; }
Thank you very much!!!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
