Question: /* @file BagInterface.h */ #ifndef BAG_INTERFACE_ #define BAG_INTERFACE_ #include template class BagInterface { public: virtual bool isEmpty() const = 0; virtual bool add(const ItemType& newEntry)
/* @file BagInterface.h */
#ifndef BAG_INTERFACE_
#define BAG_INTERFACE_
#include
template
class BagInterface
{
public:
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
virtual ~BagInterface () { }
};
#endif
====================================================
/* @file 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
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
}; // end ArrayBag
//#include "ArrayBag.cpp"
#endif
====================================================
/* @file ArrayBag.cpp */
#include "ArrayBag.h"
#include
template
ArrayBag
{
}
template
int ArrayBag
{
return itemCount;
}
template
bool ArrayBag
{
return itemCount == 0;
}
template
bool ArrayBag
{
bool hasRoomToAdd = (itemCount < maxItems);
if (hasRoomToAdd)
{
items[itemCount] = newEntry;
itemCount++;
}
return hasRoomToAdd;
}
template
bool ArrayBag
{
int locatedIndex = getIndexOf(anEntry);
bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
if (canRemoveItem)
{
itemCount--;
items[locatedIndex] = items[itemCount];
}
return canRemoveItem;
}
template
bool ArrayBag
{
// Add code here to remove an item at anIndex, and shift all the items behind anIndex one position ahead to fill the gap by the removed item
}
template
void ArrayBag
{
itemCount = 0;
}
template
int ArrayBag
{
int frequency = 0;
int curIndex = 0; // Current array index
while (curIndex < itemCount)
{
if (items[curIndex] == anEntry)
{
frequency++;
}
curIndex++; // Increment to next entry
}
return frequency;
} // end getFrequencyOf
template
bool ArrayBag
{
return getIndexOf(anEntry) > -1;
}
template
std::vector
{
std::vector
for (int i = 0; i < itemCount; i++)
bagContents.push_back(items[i]);
return bagContents;
}
// private
template
int ArrayBag
{
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++;
}
}
return result;
}
==========================================
/* @file main.cpp */
#include
#include
#include "ArrayBag.h"
int main()
{
// ADD CODE HERE for the Lab 3
// 1. Create a bag of integers of maximum 10 items
// 2. Add 3 items from the back one by one, so the array will looks like: {10, 15, 5}
// 3. Add 2 items from the front, so the array will looks like: {20, 0, 10, 15, 5}
// 3. Remove item with value 10
// 4. Remove item at index 1
// 4. Display the bag
return 0;
} // end main
Q) add code in MAIN.CPP add and remove items and also add code in ARRAYBAG.cpp to remove an item at index
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
