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 toVector() const = 0;

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 toVector() const;

}; // end ArrayBag

//#include "ArrayBag.cpp"

#endif

====================================================

/* @file ArrayBag.cpp */

#include "ArrayBag.h"

#include

template

ArrayBag::ArrayBag(): itemCount(0), maxItems(DEFAULT_CAPACITY)

{

}

template

int ArrayBag::getCurrentSize() const

{

return itemCount;

}

template

bool ArrayBag::isEmpty() const

{

return itemCount == 0;

}

template

bool ArrayBag::add(const ItemType& newEntry)

{

bool hasRoomToAdd = (itemCount < maxItems);

if (hasRoomToAdd)

{

items[itemCount] = newEntry;

itemCount++;

}

return hasRoomToAdd;

}

template

bool ArrayBag::remove(const ItemType& anEntry)

{

int locatedIndex = getIndexOf(anEntry);

bool canRemoveItem = !isEmpty() && (locatedIndex > -1);

if (canRemoveItem)

{

itemCount--;

items[locatedIndex] = items[itemCount];

}

return canRemoveItem;

}

template

bool ArrayBag::remove(const int anIndex)

{

// 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::clear()

{

itemCount = 0;

}

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++;

}

curIndex++; // Increment to next entry

}

return frequency;

} // end getFrequencyOf

template

bool ArrayBag::contains(const ItemType& anEntry) const

{

return getIndexOf(anEntry) > -1;

}

template

std::vector ArrayBag::toVector() const

{

std::vector bagContents;

for (int i = 0; i < itemCount; i++)

bagContents.push_back(items[i]);

return bagContents;

}

// 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++;

}

}

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!