Implement the set class as specified in the provided Set.h file. You are NOT all
ID: 3819302 • 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
#ifndef __CPP_SET__SET__
#define __CPP_SET__SET__
#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 &os, const Set &a);
public:
Set();
int numElements() const
{
return _numItems;
}
void addElement(int x);
void removeElement(int x);
void removeAllElements();
bool containsElement(int x) const;
bool isSubsetOf(const Set &other) const;
private:
int _items[MAX_SET_ITEMS];
int _numItems;
};
Set operator+(const Set &a,const Set &b);
Set operator-(const Set &a,const Set &b);
std::ostream& operator<<(std::ostream &os, const Set &a);
#endif
Set.cpp
#include<iostream>
#include "Set.h"
using namespace std;
Set::Set()
{
this->_numItems=0;
}
bool Set::containsElement(int x) const
{
int i=0;
if(this->_numItems==0)
return false;
else
{
for(i=0;i<this->_numItems;i++)
{
if(this->_items[i]==x)
{
return true;
}
}
return false;
}
}
void Set::addElement(int x)
{
bool failure;
failure=this->containsElement(x);
if(!failure) // if there is no duplicacy then insert
{
if(this->_numItems!=100)
{
this->_items[this->_numItems]=x;
this->_numItems++;
}
else
cout<<" Set is already full! ";
}
else
cout<<" Element already present in the set! ";
}
void Set::removeElement(int x)
{
int i;
if(this->containsElement(x))
{
for(i=0;i<this->_numItems;i++)
{
if(this->_items[i]!=x)
continue;
else break;
}
for(;i<this->_numItems-1;i++)
{
this->_items[i]=this->_items[i+1];
}
this->_numItems--;
}
else
cout<<" Element not found in the set! ";
}
void Set::removeAllElements()
{
this->_numItems=0;
}
bool Set::isSubsetOf(const Set &other) const
{
int i,j,flag=0;
for(i=0;i<other.numElements();i++)
{
if(this->containsElement(other._items[0]))
continue;
else
{
flag=1;
break;
}
}
if(flag==1)
return false;
else
return true;
}
Set operator+(const Set &a,const Set &b)
{
Set s=a;
for(int i=0;i<b.numElements();i++)
{
if(s.numElements()<=100)
s.addElement(b._items[i]);
}
return s;
}
Set operator-(const Set &a,const Set &b)
{
Set s=a;
for(int i=0;i<b.numElements();i++)
{
if(s.containsElement(b._items[i]))
s.removeElement(b._items[i]);
}
return s;
}
std::ostream& operator<<(std::ostream &os, const Set &a)
{
os<<"Total elements in the set are: "<<a._numItems<<endl;
for(int i=0;i<a.numElements();i++)
{
os<<a._items[i]<<" ";
}
return os;
}