add more comment in the code. BagInterface.h:

#ifndef BAGINTERFACE_H
#define BAGINTERFACE_H

template<class ItemType>
class BagInterface {
public:
// Pure virtual functions for basic bag operations
virtual bool insert(const ItemType& item) = 0;
virtual bool contains(const ItemType& item) const = 0;
virtual int getFrequencyOf(const ItemType& item) const = 0;
virtual bool remove(const ItemType& item) = 0;
virtual void clear() = 0;
virtual int getCurrentSize() const = 0;
virtual bool isEmpty() const = 0;
virtual bool isFull() const = 0;
};

#endif

PlainBag.h:

#ifndef PLAINBAG_H
#define PLAINBAG_H

#include “BagInterface.h”

template<class ItemType>
class PlainBag : public BagInterface<ItemType> {
private:
static const int DEFAULT_CAPACITY = 20;
ItemType items[DEFAULT_CAPACITY];
int itemCount;
int maxItems;

public:
PlainBag(); // Constructor
bool insert(const ItemType& item);
bool contains(const ItemType& item) const;
int getFrequencyOf(const ItemType& item) const;
bool remove(const ItemType& item);
void clear();
int getCurrentSize() const;
bool isEmpty() const;
bool isFull() const;
};

#include “PlainBag.cpp”

#endif

PlainBag.cpp:

#include “PlainBag.h”

template<class ItemType>
PlainBag<ItemType>::PlainBag() : itemCount(0), maxItems(DEFAULT_CAPACITY) {}

template<class ItemType>
bool PlainBag<ItemType>::insert(const ItemType& item) {
if (itemCount < maxItems) {
items[itemCount] = item;
itemCount++;
return true;
}
return false;
}

template<class ItemType>
bool PlainBag<ItemType>::contains(const ItemType& item) const {
for (int i = 0; i < itemCount; i++) {
if (items[i] == item) {
return true;
}
}
return false;
}

template<class ItemType>
int PlainBag<ItemType>::getFrequencyOf(const ItemType& item) const {
int frequency = 0;
for (int i = 0; i < itemCount; i++) {
if (items[i] == item) {
frequency++;
}
}
return frequency;
}

template<class ItemType>
bool PlainBag<ItemType>::remove(const ItemType& item) {
for (int i = 0; i < itemCount; i++) {
if (items[i] == item) {
items[i] = items[itemCount - 1];
itemCount–;
return true;
}
}
return false;
}

template<class ItemType>
void PlainBag<ItemType>::clear() {
itemCount = 0;
}

template<class ItemType>
int PlainBag<ItemType>::getCurrentSize() const {
return itemCount;
}

template<class ItemType>
bool PlainBag<ItemType>::isEmpty() const {
return itemCount == 0;
}

template<class ItemType>
bool PlainBag<ItemType>::isFull() const {
return itemCount == maxItems;
}

MagicChangeBag.h:

#ifndef MAGICCHANGEBAG_H
#define MAGICCHANGEBAG_H

#include “BagInterface.h”

template<class ItemType>
class MagicChangeBag : public BagInterface<ItemType> {
private:
static const int DEFAULT_CAPACITY = 20;
ItemType items[DEFAULT_CAPACITY];
int itemCount;
int maxItems;

public:
MagicChangeBag(); // Constructor
bool insert(const ItemType& item);
bool contains(const ItemType& item) const;
int getFrequencyOf(const ItemType& item) const;
bool remove(const ItemType& item);
void clear();
int getCurrentSize() const;
bool isEmpty() const;
bool isFull() const;
};

#include “MagicChangeBag.cpp”

#endif

MagicChangeBag.cpp:

#include “MagicChangeBag.h”

template<class ItemType>
MagicChangeBag<ItemType>::MagicChangeBag() : itemCount(0), maxItems(DEFAULT_CAPACITY) {}

template<class ItemType>
bool MagicChangeBag<ItemType>::insert(const ItemType& item) {
itemCount = 0; // Item disappears when inserted
return true;
}

template<class ItemType>
bool MagicChangeBag<ItemType>::contains(const ItemType& item) const {
for (int i = 0; i < itemCount; i++) {
if (items[i] == item) {
return true;
}
}
return false;
}

template<class ItemType>
int MagicChangeBag<ItemType>::getFrequencyOf(const ItemType& item) const {
if (itemCount == 0) {
return 0; // No items in the bag
}
return itemCount - 1; // One less than the current item count to account for the disappearing item
}

template<class ItemType>
bool MagicChangeBag<ItemType>::remove(const ItemType& item) {
for (int i = 0; i < itemCount; i++) {
if (items[i] == item) {
itemCount = 0; // All items except the removed item disappear
return true;
}
}
return false;
}

template<class ItemType>
void MagicChangeBag<ItemType>::clear() {
itemCount = 0; // All items disappear
}

template<class ItemType>
int MagicChangeBag<ItemType>::getCurrentSize() const {
return itemCount;
}

template<class ItemType>
bool MagicChangeBag<ItemType>::isEmpty() const {
return itemCount == 0;
}

template<class ItemType>
bool MagicChangeBag<ItemType>::isFull() const {
return itemCount == maxItems;
}