Implement the set class as specified in the provided Set.h file. You are NOT all
ID: 3819301 • Letter: I
Question
Implement the set class as specified in the provided Set.h file. You are NOT allowed to modify the Set.h file. It includes methods to check how many elements are in the set, add an element to the set, remove an element from the set, remove all elements from the set, check if an element is in the set, and check if one set is a subset of another. It specifies two stand-alone functions using operator overloading to union and take the difference of two sets. It specifies an output operator that must output the items in the set on one line separate by a comma and space. See the sample output below. for example, if a set contained 1, 2, 3, it vould output 1, 2, 3 for example if a set contained just 1, it would output Also write a main.cpp function that tests your set implementation. You may either use the C assert macro (see section 8.14 of the book) that will stop the program and tell you if an assertion fails or you may write code such as: Set a; if (a num Elements 0) cerr empty set does not have zero elements end else cerr You may assume that a set will never contain more than 100 elements since the Set class uses an array of size 100. Note that the operators are friends of the classes so they are allowed to access the private data of any Set instance.Explanation / Answer
Set.h
#include<iostream>
const int MAX_SET_ITEMS = 100;
class Set
{
friend Set operator+(const Set &a, const Set &b);
friend Set operator-(const Set &a, const Set &b);
friend std::ostream& operator<<(std::ostream &out, const Set &a);
public:
Set();
int numElements() const { return _numItems; }
void addElement(int x);
void removeElement(int x);
void removeAllElement();
bool containsElement(int x) const;
bool isSubsetof(const Set &other) const;
private:
int _items[MAX_SET_ITEMS];
int _numItems;
};
---------------------
Set.cpp
#include"Set.h"
Set::Set()
{
for (int i = 0; i < _numItems; i++)
{
_items[i] = -1;
}
_numItems = 0;
}
void Set::addElement(int x)
{
for (int i = 0; i < _numItems; i++)
{
//check if set has this nmber already
if (x == _items[i])
return;
}
_items[_numItems++] = x;
}
void Set::removeElement(int x)
{
int *tmp;
int count = 0;
//count how many elements of x there in set
for (int i = 0; i < _numItems; i++)
{
if (x == _items[i])
count++;
}
tmp = new int[_numItems - count];
for (int i = 0; i < _numItems; i++)
{
if (x != _items[i])
tmp[i] = _items[i];
}
//now copy the tmp elements back to set _Items and update _numItems
for (int i = 0; i < _numItems - count; i++)
{
_items[i] = tmp[i];
}
}
void Set::removeAllElement()
{
for (int i = 0; i < _numItems; i++)
{
_items[i] = -1;
}
_numItems = 0;
}
bool Set::containsElement(int x) const
{
for (int i = 0; i < _numItems; i++)
{
if (_items[i] == x)
return true;
}
return false;
}
bool Set::isSubsetof(const Set &other) const
{
//call diff
int j = 0;
for (int i = 0; i < other._numItems; i++)
{
if (other._items[i++] != _items[i])
{
return false;
}
}
return true;
}
Set operator+(const Set &a, const Set &b)
{
Set add;
for (int i = 0; i < a._numItems; i++)
{
add.addElement(a._items[i]);
}
for (int i = 0; i < b._numItems; i++)
{
if (a._items[i] != b._items[i])
add.addElement(b._items[i]);
}
return add;
}
--------------
//main.cpp
#include"Set.h"
using namespace std;
int main()
{
Set s;
Set t;
s.addElement(1);
s.addElement(2);
t.addElement(3);
t.addElement(2);
cout << "S = " << s << endl;
cout << "T = " << t << endl;
Set U, I, D;
U = s + t;
cout << "Union: " << U << endl;
D = s - t;
cout << "Difference : " << D << endl;
if (U.isSubsetof(s))
cout << "s is subset of U: " << endl;
else
cout << "s is not subset of U " << endl;
return 0;
}
-------------------------------------------
//output
S = 1,2,
T = 3,2,
Union: 1,2,3,
Difference : 1,
s is subset of U: