Question: Use the files: ArrayBag.h and ArrayBag.cpp to implement the Set class for integers as described in Chapter 1, Exercises 6 and 8 at page 28.
Use the files: ArrayBag.h
and ArrayBag.cpp
to implement the "Set" class for integers as described in Chapter 1, Exercises 6 and 8 at page 28. ArrayBag, makes an array ADT with many of the functions of a STL vector. Please use this not a vector. To verify your set class, write a main function that takes two sets of integers from an input file setInput.txt, and store them into separate sets, and apply following two operations.
First, combine the two into one set by using + (union) operator, and print out the union set. The + operator must remove all duplicates and store one copy of any item. You also need to implement a subtraction operator (difference) where (A-B means removing all elements of B from A) and print out the result. So the main function takes two sets of integers and print the result of + and - operation.
ArrayBag.h
#ifndef BAG_
#define BAG_
#include
typedef int ItemType;
class ArrayBag
{
private:
static const int DEFAULT_BAG_SIZE = 100;
ItemType items[DEFAULT_BAG_SIZE]; // array of bag items
int itemCount; // current count of bag items
int maxItems; // max capacity of the bag
// Returns either the index of the element in the array items that
// contains the given target or -1, if the array does not contain
// the target.
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 Bag
#endif
ArrayBag.cpp
#include "ArrayBag.h"
ArrayBag::ArrayBag() : itemCount(0), maxItems(DEFAULT_ArrayBag_SIZE)
{
} // end default constructor
int ArrayBag::getCurrentSize() const
{
return itemCount;
} // end getCurrentSize
bool ArrayBag::isEmpty() const
{
return itemCount == 0;
} // end isEmpty
bool ArrayBag::add(const ItemType& newEntry)
{
bool hasRoomToAdd = (itemCount
if (hasRoomToAdd)
{
items[itemCount] = newEntry;
itemCount++;
} // end if
return hasRoomToAdd;
} // end add
bool ArrayBag::remove(const ItemType& anEntry)
{
int locatedIndex = getIndexOf(anEntry);
bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
if (canRemoveItem)
{
itemCount--;
items[locatedIndex] = items[itemCount];
} // end if
return canRemoveItem;
} // end remove
void ArrayBag::clear()
{
itemCount = 0;
} // end clear
int ArrayBag::getFrequencyOf(const ItemType& anEntry) const
{
int frequency = 0;
int searchIndex = 0;
while (searchIndex
{
if (items[searchIndex] == anEntry)
{
frequency++;
} // end if
searchIndex++;
} // end while
return frequency;
} // end getFrequencyOf
bool ArrayBag::contains(const ItemType& anEntry) const
{
return getIndexOf(anEntry) > -1;
} // end contains
std::vector
{
std::vector
for (int i = 0; i
ArrayBagContents.push_back(items[i]);
return ArrayBagContents;
} // end toVector
// private
int ArrayBag::getIndexOf(const ItemType& target) const
{
bool found = false;
int result = -1;
int searchIndex = 0;
// if the ArrayBag is empty, itemCount is zero, so loop is skipped
while (!found && (searchIndex
{
if (items[searchIndex] == target)
{
found = true;
result = searchIndex;
}
else
{
searchIndex++;
} // end if
} // end while
return result;
} // end getIndexOf
You can either add the + and - operators to the ArrayBag, or you can add a Set class as a friend to ArrayBag (friend class Set; Then define a class Set in a .h and associated .cpp)
A sample setInput.txt
may look like this. Note that we assume the input file includes only two sets of integer numbers, each number is separated by white spaces and sets are separated by a newline character. 3 4 5 7 5 16 7 12 11 12 3 9 9 8 1 12 15 4 3 6 1 12 3 12 7 8 19 9 11 12 8 5 -4 -100
After validating, instead of putting them into a vector, you will add them to a "Set" (ArrayBag) to meet this homework's requirement.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
