You are using the Array-based bag that is in the book and that we covered in cla
ID: 675036 • Letter: Y
Question
You are using the Array-based bag that is in the book and that we covered in class. Write a complete C++ main( ) program that does the following the 6 items listed below. Assume that the displayBag( ) method has already been correctly implemented.
You can start with the following statements:
#include <iostream>
#include <string>
#include "ArrayBag.h"
#include "ArrayBag.cpp"
int main( ) {
Creates a bag of integers.
Adds 1, 9, and 24 to the bag.
Prints to stdout the size of the bag.
Prints to stdout the contents of the bag.
Clears the bag.
Confirms that the bag is empty.
Below is the header file for the class. You can assume that everything is correctly implemented.
#ifndef _ARRAY_BAG
#define _ARRAY_BAG
#include "BagInterface.h"
template<class ItemType>
class ArrayBag : public BagInterface<ItemType>
{
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
int getIndexOf(const ItemType& target) const;
public:
ArrayBag();
int getCurrentSize() const;
bool isEmpty() const;
bool add(const ItemType& newEntry);
void clear();
vector<ItemType> toVector() const;
}; // end ArrayBag
Explanation / Answer
Bag Interface code:
#ifndef _BAG_INTERFACE
#define _BAG_INTERFACE
#include <vector>
using namespace std;
template<class ItemType>
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;
@return The number of times anEntry appears in the bag. */
virtual int getFrequencyOf(const ItemType& anEntry) const = 0;
virtual bool contains(const ItemType& anEntry) const = 0;
};
#endif
ArrayBag header file:
#ifndef _ARRAY_BAG
#define _ARRAY_BAG
#include "BagInterface.h"
template<class Integer>
class ArrayBag : public BagInterface<Integer>
{
private:
static const int DEFAULT_CAPACITY = 6; // Small size to test for a full bag
Integer items[DEFAULT_CAPACITY]; // Array of bag items
int itemCount; // Current count of bag items
int maxItems; // Max capacity of the bag
int getIndexOf(const Integer& target) const;
public:
ArrayBag();
int getCurrentSize() const;
bool isEmpty() const;
bool add(const Integer& newEntry);
void clear();
vector<Integer> toVector() const;
}; // end ArrayBag
#endif
Cpp program to implement the arraybag:
#include <iostream>
#include <string>
#include "ArrayBag.h"
#include "ArrayBag.cpp"
int main( ) {
ArrayBag ab1;
ab1.add(6);
ab1.add(9);
ab1.add(24);
cout<<"the current size of the bag is:"<<ab1.getcurrrentSize()<<endl;
ab1.displayBag();
ab1.clear();
ab1.isEmpty();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.