This is for an intro computer science course in c++. I need to create a class de
ID: 3827286 • Letter: T
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 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.
Thank you very much!!!
Explanation / Answer
*************************************************bag.h***********************************************************
#include <iostream>
#include <string>
#include <list>
using namespace std;
class Bag
{
public:
Bag();
void add(string strAdd);
bool addReturnBool(string strAdd);
void display();
void remove(string strRemove);
void clear();
bool isEmpty();
bool contains(string strContains);
int getFrequencyOf(string strFrequency);
list<string> strList ;
std::list<string>::iterator it;
};
*****************************************************************************************************************
**************************************************bag.cpp********************************************************
#include <iostream>
#include <string>
#include "bag.h"
using namespace std;
Bag::Bag()
{
}
void Bag::add(string strAdd)
{
strList.push_back(strAdd);
}
bool Bag::addReturnBool(string strAdd)
{
strList.push_back(strAdd);
return true;
}
void Bag::display()
{
for (it=strList.begin(); it!=strList.end(); ++it)
std::cout << ' ' << *it;
std::cout << ' ';
}
void Bag::remove(string strRemove)
{
strList.remove(strRemove);
}
void Bag::clear()
{
strList.clear();
}
bool Bag::isEmpty()
{
if(strList.empty())
return true;
else
return false;
}
bool Bag::contains(string strContains)
{
for (it=strList.begin(); it!=strList.end(); ++it)
{
if(*it == strContains)
return true;
else
return false;
}
}
int Bag::getFrequencyOf(string strFrequency)
{
int nCount = 0;
for (it=strList.begin(); it!=strList.end(); ++it)
{
if(*it == strFrequency)
nCount++;
}
return nCount;
}
*********************************************************************************************************************
************************************************main.cpp*************************************************************
#include <iostream>
#include <string>
#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.addReturnBool(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 ****** ";
}
*********************************************************************************************************************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.