Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a C++ code. Add any header or cpp files as needed. Include a makefile. Se

ID: 3874993 • Letter: C

Question

Create a C++ code. Add any header or cpp files as needed. Include a makefile.

Set Class

First, combine the two into one set by overloading the + (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.

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)

After validating, instead of putting them into a vector, you will add them to a "Set" (ArrayBag) to meet this homework's requirement.

You should submit a single lastnameHW2.zip containing: ArrayBag.h, ArrayBag.cpp, main.cpp, SetFunctions.h, SetFunctions.cpp, makefile, and Readme.txt and setInput.txt.

// Created by Frank M. Carrano and Tim Henry.

// Copyright (c) 2016 __Pearson Education__. All rights reserved.

//Modified by TAA to remove Template and Inheritance

/** @file 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 < maxItems);

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 < itemCount)

{

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<ItemType> ArrayBag::toVector() const

{

std::vector<ItemType> ArrayBagContents;

for (int i = 0; i < itemCount; 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 < itemCount))

{

if (items[searchIndex] == target)

{

found = true;

result = searchIndex;

}

else

{

searchIndex++;

} // end if

} // end while

return result;

} // end getIndexOf

Explanation / Answer

#ifndef _ARRAY_BAG

#define _ARRAY_BAG

#include "BagInterface.h"

#include <vector>

template<class ItemType>

class ArrayBag : public BagInterface<ItemType>

{

private:

static const int DEFAULT_CAPACITY = 24;

ItemType items[DEFAULT_CAPACITY];

int itemCount;

int maxItems;

int getIndexOf(const ItemType& target) const;

public:

    ArrayBag();

    int getCurrentSize() const;

    bool isEmpty() const;

    bool add(const ItemType& newEntry);

    void clear();

    bool remove(const ItemType& anEntry);

    bool contains(const ItemType& target) const;

    int getFrequencyOf(const ItemType& anEntry) const;

    std::vector<ItemType> toVector() const;

};

#endif

#include "ArrayBag.h"

#include <cstddef>

template<class ItemType>

ArrayBag<ItemType>::ArrayBag(): itemCount(0), maxItems(DEFAULT_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;

}template<class ItemType>

void ArrayBag<ItemType>::clear()

{

itemCount = 0;

}

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;

}

template<class ItemType>

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

{

bool found = false;

int curIndex = 0;

while(!found && (curIndex < itemCount))

{

if(anEntry == items[curIndex])

{

found = true;

}

curIndex++;

}

return found;

}

template<class ItemType>

int ArrayBag<ItemType>::getFrequencyOf(const ItemType& anEntry) const

{

int frequency = 0;

int curIndex = 0; //current index array

while(curIndex < itemCount)

{

if (items[curIndex] == anEntry)

{

frequency++;

}       

curIndex++;

}

return frequency;

}

template<class ItemType>

vector<ItemType> ArrayBag<ItemType>::toVector() const

{

vector<ItemType> bagContents;

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

bagContents.push_back(items[i]);

return bagContents;

}

ArrayBag<string> merge(const ArrayBag<string>& oneBag,

const ArrayBag<string>& anotherBag)

{

int sizeOnebag, sizeAnotherbag, newBagsize;

sizeOnebag = oneBag.getCurrentSize();

sizeAnotherbag = anotherBag.getCurrentSize();

newBagsize = sizeAnotherbag + sizeOnebag;