SlideShare a Scribd company logo
1 of 11
Download to read offline
Write a class that implements the BagInterface. BagInterface should have methods to:
getCurrentSize (): integer isEmpty(): boolean add (newEntry: T): boolean remove(): T Rather
than T use int. Also it should have a toString that displays what you have in the bag. Write both
BagInterface and Bag class. Write the main that test the Bag class. Bag +getCurrentSize():
integer +isEmpty(): boolean +add(newEntry: T): boolean +remove(): T +remove(anEntry: T):
Boolean +clear(): void +getFrequencyOf (anEntry: T): integer +contains (anEntry: T): boolean
+toArray(): T[]
Solution
Here is a list of all files (source code is given below one by one save them with given name and
rum):
ArrayBag.cpp-------------------->
#include "ArrayBag.h"
#include
template
ArrayBag::ArrayBag(): itemCount(0), maxItems(DEFAULT_CAPACITY)
{
} // end default constructor
template
int ArrayBag::getCurrentSize() const
{
return itemCount;
} // end getCurrentSize
template
bool ArrayBag::isEmpty() const
{
return itemCount == 0;
} // end isEmpty
template
bool ArrayBag::add(const ItemType& newEntry)
{
bool hasRoomToAdd = (itemCount < maxItems);
if (hasRoomToAdd)
{
items[itemCount] = newEntry;
itemCount++;
} // end if
return hasRoomToAdd;
} // end add
template
bool ArrayBag::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 ArrayBag::clear()
{
itemCount = 0;
} // end clear
template
int ArrayBag::getFrequencyOf(const ItemType& anEntry) const
{
int frequency = 0;
int curIndex = 0; // Current array index
while (curIndex < itemCount)
{
if (items[curIndex] == anEntry)
{
frequency++;
} // end if
curIndex++; // Increment to next entry
} // end while
return frequency;
} // end getFrequencyOf
template
bool ArrayBag::contains(const ItemType& anEntry) const
{
return getIndexOf(anEntry) > -1;
} // end contains
template
std::vector ArrayBag::toVector() const
{
std::vector bagContents;
for (int i = 0; i < itemCount; i++)
bagContents.push_back(items[i]);
return bagContents;
} // end toVector
// private
template
int ArrayBag::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
ArrayBag.h-------------->
#ifndef ARRAY_BAG_
#define ARRAY_BAG_
#include "BagInterface.h"
template
class ArrayBag : public BagInterface
{
private:
static const int DEFAULT_CAPACITY = 6; // Small size to test for a full bag
ItemType items[DEFAULT_CAPACITY]; // 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:
ArrayBag();
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 ArrayBag
#include "ArrayBag.cpp"
#endif
BagDriver.cpp-------->
//-----------------------
// C++ includes
//-----------------------
#include
#include
#include
#include
//-----------------------
// Application includes
//-----------------------
#include "ArrayBag.h"
#include "io_functions.h"
//-----------------------
// Using statements
//-----------------------
using namespace std;
//----------------------
// Functions Prototypes
//----------------------
int getBagNum(); //prompts user for bag number
void processOption(char& c, ArrayBag bags[]); //processes menu input
void displayBag(ArrayBag& bag); //display the bag's entire contents
void restoreBag(ArrayBag& bag); //prompts for filename with items
void saveBag(ArrayBag& bag); //prompts for filename to store items in
int main()
{
int bagNum; //The bag number to use
ArrayBag bag[3];//bags to test, bag 0 used for intersection/union
string restoreBagFlg; // holds response to restore bag from a file
string menuOption; // holds menu option input from user
//Opening Message
cout<< "Bag Driver - Tests the Bag class using two bags" << endl;
//Restore the bag from a file
cout<< "----------------------------------" << endl;
cout<< "Would you like to restore a bag from a file? (Y/N) ";
//Read user input - use a string to accept all inputs
getline(cin, restoreBagFlg);
if(restoreBagFlg[0] == 'Y' || restoreBagFlg[0] == 'y'){
bagNum = getBagNum();
restoreBag(bag[bagNum]); // opens/reads file into aBag
}
//Menu driven part of main program
//Initial menu display and loop to process menu option
printMenu();
do {
cout << " Enter menu selection (0 for menu): ";
getline(cin, menuOption); //Use a string in case user enters chars
processOption(menuOption[0], bag);
}while(menuOption[0] != '9');
return 0; //Exit program
}//main()
//---------------------------
// Local function definitions
//---------------------------
int getBagNum()
{
int bagNum=0;
while(bagNum != 1 && bagNum != 2)
{
cout << "Which bag (1 or 2)? ";
bagNum = getInt();
}
return bagNum;
} //getBagNum()
void displayBag(ArrayBag& bag)
{
string item;
vector bagVector = bag.toVector();
if(bagVector.size() > 0)
{
cout << "The bag contains: " << endl;
for(unsigned int i=0; i bag[])
{
int bagNum;
string item;
string saveBagFlg;
switch (menuChar)
{
case '0': // Display menu options
printMenu();
break;
case '1': // Output size of the bag
cout << " Bag Size is: " << bag[getBagNum()].getCurrentSize() << endl;
break;
case '2': // Add items to the bag
cout << "Add Item:" << endl;
item = getItem();
bagNum = getBagNum();
if(bag[bagNum].add(item))
{
cout << item << " was successfully added to bag " << bagNum << endl;
}
else
{
cout << item << " was not added to the bag." << endl;
}
break;
//NOTE that options 3 - 8 are left for the student to complete
case '8': // Display entire contents of bag
displayBag(bag[getBagNum()]);
break;
case '9': // Exit the program
case 'Q': // handles multiple quit inputs
case 'q':
cout<< "Would you like to save the bag to a file? (Y/N) ";
getline(cin, saveBagFlg);
if(saveBagFlg[0] == 'Y' || saveBagFlg[0] == 'y')
saveBag(bag[getBagNum()]); // save bag to file
cout<< "Goodbye!"<< endl; // Exit message
menuChar = '9';
break;
default: // Invalid menu option entered
cout << " Error! Invalid option. Please try again.  ";
printMenu();
}
}//processOption()
void restoreBag(ArrayBag& bag)
{
bool success = true;
ifstream fin;
string filename;
string item;
//Data validation to get filename and open it
do{
cout<<"Enter the filename that contains the bag: ";
getline(cin, filename);
fin.clear();
fin.open(filename.c_str());
if(fin == 0){
cout<<"ERROR - could not open file " << filename<& bag)
{
//NOTE this is a function stub. You need to implement it.
cout << "Bag will be saved when implemented" << endl;
}//saveBag()
BagInterface.h------>
#ifndef BAG_INTERFACE_
#define BAG_INTERFACE_
#include
template
class BagInterface
{
public:
virtual int getCurrentSize() const = 0;
virtual bool isEmpty() const = 0;
virtual bool add(const ItemType& newEntry) = 0;
virtual bool remove(const ItemType& anEntry) = 0;
virtual void clear() = 0;
virtual int getFrequencyOf(const ItemType& anEntry) const = 0;
virtual bool contains(const ItemType& anEntry) const = 0;
virtual std::vector toVector() const = 0;
virtual ~BagInterface() { }
}; // end BagInterface
#endif
CardGuesser.cpp------->
#include // For cout and cin
#include // For string objects
#include "Bag.h" // For ADT bag
using namespace std;
int main()
{
string clubs[] = { "Joker", "Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King" };
// Create our bag to hold cards.
Bag grabBag;
// Place six cards in the bag.
grabBag.add(clubs[1]);
grabBag.add(clubs[2]);
grabBag.add(clubs[4]);
grabBag.add(clubs[8]);
grabBag.add(clubs[10]);
grabBag.add(clubs[12]);
// Get friends guess and check it.
int guess = 0;
while (!grabBag.isEmpty())
{
cout << "What is your guess?"<< "(1 for Ace to 13 for King):";
cin >> guess;
// Is card in the bag?
if (grabBag.contains(clubs[guess]))
{
// Good guess remove card from the bag.
cout << "You get the card! ";
grabBag.remove(clubs[guess]);
}
else
{
cout << "Sorry, card was not in the bag. ";
} // end if
} // end while
cout << "No more cards in the bag. Game over! ";
return 0;
}; // end main
io_functions.cpp----------->
#include "io_functions.h"
using namespace std;
void printMenu(){
cout << "1) Print the size of the bag" << endl;
cout << "2) Add an item into the bag" << endl;
cout << "3) Remove an item from the bag" << endl;
cout << "4) Check for an item in the bag" << endl;
cout << "8) Display the bag" << endl;
cout << "9) Quit the program" << endl;
}
int getInt(){
int intValue = 0;
string inputStr;
do{
getline(cin, inputStr);
stringstream(inputStr) >> intValue;
if(intValue < 1) cout << "ERROR - please enter a positive integer> ";
} while(intValue < 1);
return intValue;
}
string getItem()
{
string item;
cout << "Enter the bag item> ";
getline(cin, item);
return item;
}
io_functions.h------------>
#ifndef IO_FUNCTIONS_H
#define IO_FUNCTIONS_H
#include
#include
using namespace std;
void printMenu();
int getInt();
string getItem();
#endif

More Related Content

Similar to Write a class that implements the BagInterface. BagInterface should .pdf

#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdfBackPack3
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdfBackPack3
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdfBackPack3
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdfssuseraef9da
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdfBackPack3
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdfssuseraef9da
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdfarwholesalelors
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdfBackPack3
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdfBackPack3
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdfssuseraef9da
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdfssuseraef9da
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdfBackPack3
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdfssuseraef9da
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdfssuseraef9da
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdfBackPack3
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdfBackPack3
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdfBackPack3
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdfBackPack3
 
HERE is a C PROGRAM BELOW EVERYTIME I TRY TO RUN IT ON MATRIX IT GI.pdf
HERE is a C PROGRAM BELOW EVERYTIME I TRY TO RUN IT ON MATRIX IT GI.pdfHERE is a C PROGRAM BELOW EVERYTIME I TRY TO RUN IT ON MATRIX IT GI.pdf
HERE is a C PROGRAM BELOW EVERYTIME I TRY TO RUN IT ON MATRIX IT GI.pdffashiongallery1
 

Similar to Write a class that implements the BagInterface. BagInterface should .pdf (20)

#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf#include algorithm #include vector #include iostream usi.pdf
#include algorithm #include vector #include iostream usi.pdf
 
Kotlin
KotlinKotlin
Kotlin
 
HERE is a C PROGRAM BELOW EVERYTIME I TRY TO RUN IT ON MATRIX IT GI.pdf
HERE is a C PROGRAM BELOW EVERYTIME I TRY TO RUN IT ON MATRIX IT GI.pdfHERE is a C PROGRAM BELOW EVERYTIME I TRY TO RUN IT ON MATRIX IT GI.pdf
HERE is a C PROGRAM BELOW EVERYTIME I TRY TO RUN IT ON MATRIX IT GI.pdf
 

More from fashiongallery1

For this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdfFor this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdffashiongallery1
 
Discuss the character and impact of the economic and legal revouluti.pdf
Discuss the character and impact of the economic and legal revouluti.pdfDiscuss the character and impact of the economic and legal revouluti.pdf
Discuss the character and impact of the economic and legal revouluti.pdffashiongallery1
 
Discrete Math -Use induction to prove. The city of Inductionapolis.pdf
Discrete Math -Use induction to prove. The city of Inductionapolis.pdfDiscrete Math -Use induction to prove. The city of Inductionapolis.pdf
Discrete Math -Use induction to prove. The city of Inductionapolis.pdffashiongallery1
 
Describe polarization and why it is important to WLANs.Solution.pdf
Describe polarization and why it is important to WLANs.Solution.pdfDescribe polarization and why it is important to WLANs.Solution.pdf
Describe polarization and why it is important to WLANs.Solution.pdffashiongallery1
 
Describe the four basic elements of most communication systems.So.pdf
Describe the four basic elements of most communication systems.So.pdfDescribe the four basic elements of most communication systems.So.pdf
Describe the four basic elements of most communication systems.So.pdffashiongallery1
 
Can someone please help me figure out how to do this Required Resou.pdf
Can someone please help me figure out how to do this Required Resou.pdfCan someone please help me figure out how to do this Required Resou.pdf
Can someone please help me figure out how to do this Required Resou.pdffashiongallery1
 
Assume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdf
Assume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdfAssume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdf
Assume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdffashiongallery1
 
All computer are configured for TCPIP connectivity. This exercise i.pdf
All computer are configured for TCPIP connectivity. This exercise i.pdfAll computer are configured for TCPIP connectivity. This exercise i.pdf
All computer are configured for TCPIP connectivity. This exercise i.pdffashiongallery1
 
4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdf
4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdf4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdf
4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdffashiongallery1
 
Briefly Explain all these points belowA. Marketing channels VS cha.pdf
Briefly Explain all these points belowA. Marketing channels VS cha.pdfBriefly Explain all these points belowA. Marketing channels VS cha.pdf
Briefly Explain all these points belowA. Marketing channels VS cha.pdffashiongallery1
 
write an equation for the transformation of the graph of y =f(x) tra.pdf
write an equation for the transformation of the graph of y =f(x) tra.pdfwrite an equation for the transformation of the graph of y =f(x) tra.pdf
write an equation for the transformation of the graph of y =f(x) tra.pdffashiongallery1
 
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdfUsing c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdffashiongallery1
 
Why are helper T-cells called helper cellsThey help pathogens i.pdf
Why are helper T-cells called helper cellsThey help pathogens i.pdfWhy are helper T-cells called helper cellsThey help pathogens i.pdf
Why are helper T-cells called helper cellsThey help pathogens i.pdffashiongallery1
 
Which of the following is NOT true of the Sons of Liberty a. New Yor.pdf
Which of the following is NOT true of the Sons of Liberty a. New Yor.pdfWhich of the following is NOT true of the Sons of Liberty a. New Yor.pdf
Which of the following is NOT true of the Sons of Liberty a. New Yor.pdffashiongallery1
 
What made the Later Roman Economy so unstable (Bennette, Medieval E.pdf
What made the Later Roman Economy so unstable (Bennette, Medieval E.pdfWhat made the Later Roman Economy so unstable (Bennette, Medieval E.pdf
What made the Later Roman Economy so unstable (Bennette, Medieval E.pdffashiongallery1
 
What data would illustrate whether these underlying problems are occ.pdf
What data would illustrate whether these underlying problems are occ.pdfWhat data would illustrate whether these underlying problems are occ.pdf
What data would illustrate whether these underlying problems are occ.pdffashiongallery1
 
1. What are distinct characteristics of baby boomers, generation X, .pdf
1. What are distinct characteristics of baby boomers, generation X, .pdf1. What are distinct characteristics of baby boomers, generation X, .pdf
1. What are distinct characteristics of baby boomers, generation X, .pdffashiongallery1
 
VERSION The cells denoted by the letter Ain the figure to.pdf
VERSION The cells denoted by the letter Ain the figure to.pdfVERSION The cells denoted by the letter Ain the figure to.pdf
VERSION The cells denoted by the letter Ain the figure to.pdffashiongallery1
 
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdf
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdfUsing standard libraries like stdio and sdtlib.h and using stats.h a.pdf
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdffashiongallery1
 
The __________ is the preeminent organization for developing and pub.pdf
The __________ is the preeminent organization for developing and pub.pdfThe __________ is the preeminent organization for developing and pub.pdf
The __________ is the preeminent organization for developing and pub.pdffashiongallery1
 

More from fashiongallery1 (20)

For this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdfFor this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdf
 
Discuss the character and impact of the economic and legal revouluti.pdf
Discuss the character and impact of the economic and legal revouluti.pdfDiscuss the character and impact of the economic and legal revouluti.pdf
Discuss the character and impact of the economic and legal revouluti.pdf
 
Discrete Math -Use induction to prove. The city of Inductionapolis.pdf
Discrete Math -Use induction to prove. The city of Inductionapolis.pdfDiscrete Math -Use induction to prove. The city of Inductionapolis.pdf
Discrete Math -Use induction to prove. The city of Inductionapolis.pdf
 
Describe polarization and why it is important to WLANs.Solution.pdf
Describe polarization and why it is important to WLANs.Solution.pdfDescribe polarization and why it is important to WLANs.Solution.pdf
Describe polarization and why it is important to WLANs.Solution.pdf
 
Describe the four basic elements of most communication systems.So.pdf
Describe the four basic elements of most communication systems.So.pdfDescribe the four basic elements of most communication systems.So.pdf
Describe the four basic elements of most communication systems.So.pdf
 
Can someone please help me figure out how to do this Required Resou.pdf
Can someone please help me figure out how to do this Required Resou.pdfCan someone please help me figure out how to do this Required Resou.pdf
Can someone please help me figure out how to do this Required Resou.pdf
 
Assume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdf
Assume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdfAssume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdf
Assume that the Current Assets for Shine Co. as of Decebmer 31, 2011.pdf
 
All computer are configured for TCPIP connectivity. This exercise i.pdf
All computer are configured for TCPIP connectivity. This exercise i.pdfAll computer are configured for TCPIP connectivity. This exercise i.pdf
All computer are configured for TCPIP connectivity. This exercise i.pdf
 
4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdf
4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdf4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdf
4. At what temperature will a solution of 8.27 g CaCl2 in 45.0 g H2.pdf
 
Briefly Explain all these points belowA. Marketing channels VS cha.pdf
Briefly Explain all these points belowA. Marketing channels VS cha.pdfBriefly Explain all these points belowA. Marketing channels VS cha.pdf
Briefly Explain all these points belowA. Marketing channels VS cha.pdf
 
write an equation for the transformation of the graph of y =f(x) tra.pdf
write an equation for the transformation of the graph of y =f(x) tra.pdfwrite an equation for the transformation of the graph of y =f(x) tra.pdf
write an equation for the transformation of the graph of y =f(x) tra.pdf
 
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdfUsing c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
 
Why are helper T-cells called helper cellsThey help pathogens i.pdf
Why are helper T-cells called helper cellsThey help pathogens i.pdfWhy are helper T-cells called helper cellsThey help pathogens i.pdf
Why are helper T-cells called helper cellsThey help pathogens i.pdf
 
Which of the following is NOT true of the Sons of Liberty a. New Yor.pdf
Which of the following is NOT true of the Sons of Liberty a. New Yor.pdfWhich of the following is NOT true of the Sons of Liberty a. New Yor.pdf
Which of the following is NOT true of the Sons of Liberty a. New Yor.pdf
 
What made the Later Roman Economy so unstable (Bennette, Medieval E.pdf
What made the Later Roman Economy so unstable (Bennette, Medieval E.pdfWhat made the Later Roman Economy so unstable (Bennette, Medieval E.pdf
What made the Later Roman Economy so unstable (Bennette, Medieval E.pdf
 
What data would illustrate whether these underlying problems are occ.pdf
What data would illustrate whether these underlying problems are occ.pdfWhat data would illustrate whether these underlying problems are occ.pdf
What data would illustrate whether these underlying problems are occ.pdf
 
1. What are distinct characteristics of baby boomers, generation X, .pdf
1. What are distinct characteristics of baby boomers, generation X, .pdf1. What are distinct characteristics of baby boomers, generation X, .pdf
1. What are distinct characteristics of baby boomers, generation X, .pdf
 
VERSION The cells denoted by the letter Ain the figure to.pdf
VERSION The cells denoted by the letter Ain the figure to.pdfVERSION The cells denoted by the letter Ain the figure to.pdf
VERSION The cells denoted by the letter Ain the figure to.pdf
 
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdf
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdfUsing standard libraries like stdio and sdtlib.h and using stats.h a.pdf
Using standard libraries like stdio and sdtlib.h and using stats.h a.pdf
 
The __________ is the preeminent organization for developing and pub.pdf
The __________ is the preeminent organization for developing and pub.pdfThe __________ is the preeminent organization for developing and pub.pdf
The __________ is the preeminent organization for developing and pub.pdf
 

Recently uploaded

COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 

Recently uploaded (20)

COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 

Write a class that implements the BagInterface. BagInterface should .pdf

  • 1. Write a class that implements the BagInterface. BagInterface should have methods to: getCurrentSize (): integer isEmpty(): boolean add (newEntry: T): boolean remove(): T Rather than T use int. Also it should have a toString that displays what you have in the bag. Write both BagInterface and Bag class. Write the main that test the Bag class. Bag +getCurrentSize(): integer +isEmpty(): boolean +add(newEntry: T): boolean +remove(): T +remove(anEntry: T): Boolean +clear(): void +getFrequencyOf (anEntry: T): integer +contains (anEntry: T): boolean +toArray(): T[] Solution Here is a list of all files (source code is given below one by one save them with given name and rum): ArrayBag.cpp--------------------> #include "ArrayBag.h" #include template ArrayBag::ArrayBag(): itemCount(0), maxItems(DEFAULT_CAPACITY) { } // end default constructor template int ArrayBag::getCurrentSize() const { return itemCount; } // end getCurrentSize template bool ArrayBag::isEmpty() const { return itemCount == 0; } // end isEmpty template bool ArrayBag::add(const ItemType& newEntry) { bool hasRoomToAdd = (itemCount < maxItems); if (hasRoomToAdd) { items[itemCount] = newEntry;
  • 2. itemCount++; } // end if return hasRoomToAdd; } // end add template bool ArrayBag::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 ArrayBag::clear() { itemCount = 0; } // end clear template int ArrayBag::getFrequencyOf(const ItemType& anEntry) const { int frequency = 0; int curIndex = 0; // Current array index while (curIndex < itemCount) { if (items[curIndex] == anEntry) { frequency++; } // end if curIndex++; // Increment to next entry } // end while return frequency; } // end getFrequencyOf
  • 3. template bool ArrayBag::contains(const ItemType& anEntry) const { return getIndexOf(anEntry) > -1; } // end contains template std::vector ArrayBag::toVector() const { std::vector bagContents; for (int i = 0; i < itemCount; i++) bagContents.push_back(items[i]); return bagContents; } // end toVector // private template int ArrayBag::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 ArrayBag.h-------------->
  • 4. #ifndef ARRAY_BAG_ #define ARRAY_BAG_ #include "BagInterface.h" template class ArrayBag : public BagInterface { private: static const int DEFAULT_CAPACITY = 6; // Small size to test for a full bag ItemType items[DEFAULT_CAPACITY]; // 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: ArrayBag(); 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 ArrayBag #include "ArrayBag.cpp" #endif BagDriver.cpp--------> //----------------------- // C++ includes //----------------------- #include #include #include #include
  • 5. //----------------------- // Application includes //----------------------- #include "ArrayBag.h" #include "io_functions.h" //----------------------- // Using statements //----------------------- using namespace std; //---------------------- // Functions Prototypes //---------------------- int getBagNum(); //prompts user for bag number void processOption(char& c, ArrayBag bags[]); //processes menu input void displayBag(ArrayBag& bag); //display the bag's entire contents void restoreBag(ArrayBag& bag); //prompts for filename with items void saveBag(ArrayBag& bag); //prompts for filename to store items in int main() { int bagNum; //The bag number to use ArrayBag bag[3];//bags to test, bag 0 used for intersection/union string restoreBagFlg; // holds response to restore bag from a file string menuOption; // holds menu option input from user //Opening Message cout<< "Bag Driver - Tests the Bag class using two bags" << endl; //Restore the bag from a file cout<< "----------------------------------" << endl; cout<< "Would you like to restore a bag from a file? (Y/N) "; //Read user input - use a string to accept all inputs getline(cin, restoreBagFlg); if(restoreBagFlg[0] == 'Y' || restoreBagFlg[0] == 'y'){ bagNum = getBagNum(); restoreBag(bag[bagNum]); // opens/reads file into aBag } //Menu driven part of main program //Initial menu display and loop to process menu option
  • 6. printMenu(); do { cout << " Enter menu selection (0 for menu): "; getline(cin, menuOption); //Use a string in case user enters chars processOption(menuOption[0], bag); }while(menuOption[0] != '9'); return 0; //Exit program }//main() //--------------------------- // Local function definitions //--------------------------- int getBagNum() { int bagNum=0; while(bagNum != 1 && bagNum != 2) { cout << "Which bag (1 or 2)? "; bagNum = getInt(); } return bagNum; } //getBagNum() void displayBag(ArrayBag& bag) { string item; vector bagVector = bag.toVector(); if(bagVector.size() > 0) { cout << "The bag contains: " << endl; for(unsigned int i=0; i bag[]) { int bagNum; string item; string saveBagFlg; switch (menuChar) { case '0': // Display menu options
  • 7. printMenu(); break; case '1': // Output size of the bag cout << " Bag Size is: " << bag[getBagNum()].getCurrentSize() << endl; break; case '2': // Add items to the bag cout << "Add Item:" << endl; item = getItem(); bagNum = getBagNum(); if(bag[bagNum].add(item)) { cout << item << " was successfully added to bag " << bagNum << endl; } else { cout << item << " was not added to the bag." << endl; } break; //NOTE that options 3 - 8 are left for the student to complete case '8': // Display entire contents of bag displayBag(bag[getBagNum()]); break; case '9': // Exit the program case 'Q': // handles multiple quit inputs case 'q': cout<< "Would you like to save the bag to a file? (Y/N) "; getline(cin, saveBagFlg); if(saveBagFlg[0] == 'Y' || saveBagFlg[0] == 'y') saveBag(bag[getBagNum()]); // save bag to file cout<< "Goodbye!"<< endl; // Exit message menuChar = '9'; break; default: // Invalid menu option entered cout << " Error! Invalid option. Please try again. "; printMenu(); }
  • 8. }//processOption() void restoreBag(ArrayBag& bag) { bool success = true; ifstream fin; string filename; string item; //Data validation to get filename and open it do{ cout<<"Enter the filename that contains the bag: "; getline(cin, filename); fin.clear(); fin.open(filename.c_str()); if(fin == 0){ cout<<"ERROR - could not open file " << filename<& bag) { //NOTE this is a function stub. You need to implement it. cout << "Bag will be saved when implemented" << endl; }//saveBag() BagInterface.h------> #ifndef BAG_INTERFACE_ #define BAG_INTERFACE_ #include template class BagInterface { public: virtual int getCurrentSize() const = 0; virtual bool isEmpty() const = 0; virtual bool add(const ItemType& newEntry) = 0; virtual bool remove(const ItemType& anEntry) = 0; virtual void clear() = 0; virtual int getFrequencyOf(const ItemType& anEntry) const = 0; virtual bool contains(const ItemType& anEntry) const = 0; virtual std::vector toVector() const = 0; virtual ~BagInterface() { }
  • 9. }; // end BagInterface #endif CardGuesser.cpp-------> #include // For cout and cin #include // For string objects #include "Bag.h" // For ADT bag using namespace std; int main() { string clubs[] = { "Joker", "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; // Create our bag to hold cards. Bag grabBag; // Place six cards in the bag. grabBag.add(clubs[1]); grabBag.add(clubs[2]); grabBag.add(clubs[4]); grabBag.add(clubs[8]); grabBag.add(clubs[10]); grabBag.add(clubs[12]); // Get friends guess and check it. int guess = 0; while (!grabBag.isEmpty()) { cout << "What is your guess?"<< "(1 for Ace to 13 for King):"; cin >> guess; // Is card in the bag? if (grabBag.contains(clubs[guess])) { // Good guess remove card from the bag. cout << "You get the card! "; grabBag.remove(clubs[guess]); } else { cout << "Sorry, card was not in the bag. ";
  • 10. } // end if } // end while cout << "No more cards in the bag. Game over! "; return 0; }; // end main io_functions.cpp-----------> #include "io_functions.h" using namespace std; void printMenu(){ cout << "1) Print the size of the bag" << endl; cout << "2) Add an item into the bag" << endl; cout << "3) Remove an item from the bag" << endl; cout << "4) Check for an item in the bag" << endl; cout << "8) Display the bag" << endl; cout << "9) Quit the program" << endl; } int getInt(){ int intValue = 0; string inputStr; do{ getline(cin, inputStr); stringstream(inputStr) >> intValue; if(intValue < 1) cout << "ERROR - please enter a positive integer> "; } while(intValue < 1); return intValue; } string getItem() { string item; cout << "Enter the bag item> "; getline(cin, item); return item; } io_functions.h------------> #ifndef IO_FUNCTIONS_H #define IO_FUNCTIONS_H
  • 11. #include #include using namespace std; void printMenu(); int getInt(); string getItem(); #endif