In: Computer Science
C++
Create an ArrayBag template class from scratch. This will require you to create two files: ArrayBag.hpp for the interface and ArrayBag.cpp for the implementation.
The ArrayBag class must contain the following protected members:
static const int DEFAULT_CAPACITY = 200; // max size of items_ ItemType items_[DEFAULT_CAPACITY]; // items in the array bag int item_count_; // Current count of items in bag /** @param target to be found in items_ @return either the index target in the array items_ or -1, if the array does not containthe target. **/ int getIndexOf(const ItemType &target) const;
Your ArrayBag class must contain the following public members:
/** Default Constructor item_count_ <- 0 **/ ArrayBag(); /** @return item_count_ : the current size of the bag **/ int getCurrentSize() const; /** @return true if item_count_ == 0, false otherwise **/ bool isEmpty() const; /** @return true if new_entry was successfully added to items_, false otherwise **/ bool add(const ItemType &new_entry); /** @return true if an_entry was successfully removed from items_, false otherwise **/ bool remove(const ItemType &an_entry); /** @post item_count_ == 0 **/ void clear(); /** @return true if an_entry is found in items_, false otherwise -> THIS METHOD MUST CALL getIndexOf() <- **/ bool contains(const ItemType &an_entry) const; /** @return the number of times an_entry appears in items_ **/ int getFrequencyOf(const ItemType &an_entry) const;
// ArrayBag.hpp
#ifndef ARRAYBAG_HPP
#define ARRAYBAG_HPP
template<class ItemType>
class ArrayBag {
protected:
static const int DEFAULT_CAPACITY = 200; // max size
of items
ItemType items_[DEFAULT_CAPACITY]; // items in the
array bag
int item_count_; // Current count of items in
bag
/**
@param target to be found in items_
@return either the index target in the array items_ or
-1,
if the array does not containthe target.
**/
int getIndexOf(const ItemType& target) const;
public:
/**
Default Constructor
item_count_ <- 0
**/
ArrayBag();
/**
@return item_count_ : the current size of the
bag
**/
int getCurrentSize() const;
/**
@return true if item_count_ == 0, false
otherwise
**/
bool isEmpty() const;
/**
@return true if new_entry was successfully added to
items_,
false
otherwise
**/
bool add(const ItemType& new_entry);
/**
@return true if an_entry was successfully removed from
items_,
false
otherwise
**/
bool remove(const ItemType& an_entry);
/**
@post item_count_ == 0
**/
void clear();
/**
@return true if an_entry is found in items_, false
otherwise
-> THIS METHOD MUST CALL getIndexOf() <-
**/
bool contains(const ItemType& an_entry)
const;
/**
@return the number of times an_entry appears in
items_
**/
int getFrequencyOf(const ItemType& an_entry)
const;
}; // end ArrayBag
#include "ArrayBag.cpp"
#endif
// end of ArrayBag.hpp
// ArrayBag.cpp
#ifndef ARRAYBAG_CPP
#define ARRAYBAG_CPP
#include "ArrayBag.hpp"
// default constructor to create an empty bag of items
template<class ItemType>
ArrayBag<ItemType>::ArrayBag(): item_count_(0) {
} // end default constructor
// function to return the number of items currently in the
Bag
template<class ItemType>
int ArrayBag<ItemType>::getCurrentSize() const {
return item_count_;
} // end getCurrentSize
// function that returns true if Bag is empty else return
false
template<class ItemType>
bool ArrayBag<ItemType>::isEmpty() const {
return item_count_ == 0;
} // end isEmpty
// function to add new_entry into the Bag and return true if
addition is successful else return false
template<class ItemType>
bool ArrayBag<ItemType>::add(const ItemType&
new_entry){
// check if array has space
bool hasRoomToAdd = (item_count_ <
DEFAULT_CAPACITY);
if (hasRoomToAdd) // array has space, add new_entry at
the end
{
items_[item_count_] =
new_entry;
item_count_++;
} // end if
return hasRoomToAdd;
} // end add
// function to return the index of first occurrence of target in
the Bag, return -1 is target is not found in Bag
template<class ItemType>
int ArrayBag<ItemType>::getIndexOf(const ItemType&
target) const {
bool isFound = false;
int result = -1;
int searchIndex = 0;
// If the bag is empty, item_count_ is 0, so loop is skipped
while (!isFound && (searchIndex < item_count_))
{
isFound = (items_[searchIndex] ==
target);
if (isFound) {
result =
searchIndex;
}
else {
searchIndex++;
} // end if
} // end while
return result;
} // end getIndexOf
// function to remove first occurrence of an_entry from Bag and
returns true if removal was successful else return false
template<class ItemType>
bool ArrayBag<ItemType>::remove(const ItemType& an_entry)
{
int locatedIndex = getIndexOf(an_entry); // get the
index of first occurrence of an_entry in the bag
// check if an_entry exist in the bag
bool canRemoveItem = (locatedIndex > -1);
// an_entry is present, replace the item with the last
item of the bag
if (canRemoveItem)
{
item_count_--;
items_[locatedIndex] = items_[item_count_];
} // end if
return canRemoveItem;
} // end remove
// function to return true is an_entry is found in bag else return
false
template <class ItemType>
bool ArrayBag<ItemType>::contains(const ItemType&
an_entry) const {
int locatedIndex = getIndexOf(an_entry); // get the
index of first occurrence of an_entry in the bag
// if an_entry exist in bag then its index >
-1
return(locatedIndex != -1);
} // end contains
// function to remove all the items from bag i.e set item_count_ to
0
template<class ItemType>
void ArrayBag<ItemType>::clear() {
item_count_ = 0;
} // end clear
// function to return number of times an_entry is found in the
Bag
template<class ItemType>
int ArrayBag<ItemType>::getFrequencyOf(const ItemType&
an_entry) const {
int frequency = 0; // initialize frequency to 0
int curIndex = 0; // Current array index
// loop over the Bag
while (curIndex < item_count_) {
if (items_[curIndex] == an_entry)
{
frequency++;
} // end if
curIndex++; // Increment to next
entry
} // end while
return frequency;
} // end getFrequencyOf
#endif
// end of ArrayBag.cpp