#ifndef SET_INTERFACE_H_ #define SET_INTERFACE_H_ #include template class SetInt
ID: 3744367 • Letter: #
Question
#ifndef SET_INTERFACE_H_
#define SET_INTERFACE_H_
#include
template class SetInterface
{
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 bool contains(const ItemType& anEntry) const = 0;
virtual std::vector toVector() const = 0; };
private:
static const int DEFAULT_SET_SIZE = 4; // for testing purposes we will keep the set small
ItemType items_[DEFAULT_SET_SIZE]; // array of set items
int item_count_; // current count of set items
int max_items_; // max capacity of the set
int getIndexOf(const ItemType& target) const;
#endif
Explanation / Answer
Hello, I have completed your code. Added a method to remove an item by index, also added a method to add element at the front (which was needed for the main method), and a constructor to initialize a list with 10 capacity. Thanks.
// BagInterface.h
/* @file BagInterface.h */
#ifndef BAG_INTERFACE_
#define BAG_INTERFACE_
#include <vector>
template<class ItemType>
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<ItemType> toVector() const = 0;
virtual ~BagInterface () { }
};
#endif
//ArrayBag.h
#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();
ArrayBag(const int capacity);//added one more constructor
int getCurrentSize() const;
bool isEmpty() const;
bool add(const ItemType& newEntry);
bool addToFront(const ItemType& newEntry);
bool remove(const ItemType& anEntry);
//prototype for method to remove an item at index
bool removeByIndex(const int index);
void clear();
bool contains(const ItemType& anEntry) const;
int getFrequencyOf(const ItemType& anEntry) const;
std::vector<ItemType> toVector() const;
}; // end ArrayBag
#endif
// ArrayBag.cpp
#include "ArrayBag.h"
#include <cstddef>
template<class ItemType>
ArrayBag<ItemType>::ArrayBag(): itemCount(0), maxItems(DEFAULT_CAPACITY)
{
}
template<class ItemType>
//constructor to initialize bag with a given capacity
ArrayBag<ItemType>::ArrayBag(const int capacity): itemCount(0), maxItems(capacity)
{
}
template<class ItemType>
int ArrayBag<ItemType>::getCurrentSize() const
{
return itemCount;
}
template<class ItemType>
bool ArrayBag<ItemType>::isEmpty() const
{
return itemCount == 0;
}
template<class ItemType>
bool ArrayBag<ItemType>::add(const ItemType& newEntry)
{
bool hasRoomToAdd = (itemCount < maxItems);
if (hasRoomToAdd)
{
items[itemCount] = newEntry;
itemCount++;
}
return hasRoomToAdd;
}
//method to add an item to the front
template<class ItemType>
bool ArrayBag<ItemType>::addToFront(const ItemType& newEntry)
{
bool hasRoomToAdd = (itemCount < maxItems);
if (hasRoomToAdd)
{
//shifting elements to the right
for(int i=itemCount;i>0;i--){
items[i]=items[i-1];
}
//adding new element at the front
items[0] = newEntry;
itemCount++;
}
return hasRoomToAdd;
}
template<class ItemType>
bool ArrayBag<ItemType>::remove(const ItemType& anEntry)
{
int locatedIndex = getIndexOf(anEntry);
bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
if (canRemoveItem)
{
itemCount--;
items[locatedIndex] = items[itemCount];
}
return canRemoveItem;
}
//method to remove an item by index, the name should be different from
//remove() otherwise it will create ambiguity while removing something from
//an integer array bag
template<class ItemType>
bool ArrayBag<ItemType>::removeByIndex(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
if(anIndex>=0 && anIndex<itemCount){
for(int i=anIndex;i<itemCount-1;i++){
items[i]=items[i+1];
}
itemCount--;
}
}
template<class ItemType>
void ArrayBag<ItemType>::clear()
{
itemCount = 0;
}
template<class ItemType>
int ArrayBag<ItemType>::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<class ItemType>
bool ArrayBag<ItemType>::contains(const ItemType& anEntry) const
{
return getIndexOf(anEntry) > -1;
}
template<class ItemType>
std::vector<ItemType> ArrayBag<ItemType>::toVector() const
{
std::vector<ItemType> bagContents;
for (int i = 0; i < itemCount; i++)
bagContents.push_back(items[i]);
return bagContents;
}
// private
template<class ItemType>
int ArrayBag<ItemType>::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;
}
//main.cpp
#include <iostream>
#include <string>
#include "ArrayBag.h"
using namespace std;
int main()
{
//CODE for the Lab 3
// 1. Creating a bag of integers of maximum 10 items
ArrayBag<int> bag(10);
// 2. Adding 3 items from the back one by one, so the array will looks like: {10, 15, 5}
cout<<"Adding 3 items to the back"<<endl;
bag.add(10);
bag.add(15);
bag.add(5);
cout<<"Current bag: ";
for(int i=0;i<bag.toVector().size();i++){
cout<<bag.toVector()[i]<<" ";
}
cout<<endl;
// 3. Adding 2 items from the front, so the array will looks like: {20, 0, 10, 15, 5}
cout<<"Adding 2 items to the front"<<endl;
bag.addToFront(0);
bag.addToFront(20);
cout<<"Current bag: ";
for(int i=0;i<bag.toVector().size();i++){
cout<<bag.toVector()[i]<<" ";
}
cout<<endl;
// 3. Removing item with value 10
cout<<"Removing item with value 10"<<endl;
bag.remove(10);
cout<<"Current bag: ";
for(int i=0;i<bag.toVector().size();i++){
cout<<bag.toVector()[i]<<" ";
}
cout<<endl;
// 4. Removing item at index 1
cout<<"removing item at index 1"<<endl;
bag.removeByIndex(1);
// 4. Displaying the final bag
cout<<"Current bag: ";
for(int i=0;i<bag.toVector().size();i++){
cout<<bag.toVector()[i]<<" ";
}
cout<<endl;
return 0;
} // end main
/*OUTPUT*/
Adding 3 items to the back
Current bag: 10 15 5
Adding 2 items to the front
Current bag: 20 0 10 15 5
Removing item with value 10
Current bag: 20 0 5 15
removing item at index 1
Current bag: 20 5 15
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.