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

Specification Design and implement a class Set . At a minimum your class Set API

ID: 3760998 • Letter: S

Question

Specification

Design and implement a class Set.

At a minimum your class Set API must include the following.

Definitions

In the following definitions, assume set A is this set and set B is the given set (i.e. the set received as a parameter).

The union of two sets A and B is a set that contains all members of set A and all members of set B.

The intersection of two sets A and B is a set that contains members that are found in both sets A and B.

The difference of two sets A and B is a set that contains members of set A not found in set B, and members of set B that are not found in set A.

Examples

class TestSet

This class contains the main() method for your application and it used to test class Set.

Explanation / Answer

#include <iostream>
#include <vector>
using namespace std;
class set
{
private :
vector <int> a;
int cap;
public:
set(){
cap=32;
}
set( int capacity)
{
cap = capacity;
}
bool isMember(int member)
{
for(int i=0;i<a.size();i++)
{
if(a[i]==member)
return true;
}
return false;
}
int size()
{
return a.size();
}
bool isEmpty()
{
if(a.size()==0)
return true;
else
return false;
}
int capacity()
{
return cap;
}
int getElement(int pos)
{
return a[pos];
}
bool isSubset(set other)
{
int i;
for(i=0;i<other.size();i++)
{
if(!this->isMember(other.getElement(i)))
return false;
}
return true;
}
set findUnion(set other)
{
set res((this->capacity()) +other.capacity());
res = other;
for(int i=0;i<a.size();i++)
res.addMember(a[i]);
/*
for(i=0;i<other.capacity();i++)
res.add(other.getElement(i));
*/
return res;
}
set findIntersection(set other)
{
set res(this->capacity());
for(int i=0;i<a.size();i++)
{
if(other.isMember(a[i]))
res.addMember(a[i]);
}
return res;
  
}
bool removeMember(int member)
{
for(int i=0;i<a.size();i++)
{
if(a[i]==member)
{
a.erase(a.begin()+i);
return true;
}
}
return false;
}
bool addMember(int member)
{
for(int i=0;i<a.size();i++)
{
if(a[i]==member)
return false;
}
a.push_back(member);
return true;
}
void print()
{
for(int i=0;i<a.size();i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
void clear()
{
a.erase(a.begin(),a.end());
}

};
int main()
{
cout << "Hello World!" << endl;
set a(10);
set b(11);
a.addMember(1);
a.addMember(1);
a.addMember(12);
a.addMember(15);
a.addMember(18);
a.print();
b.addMember(1);
b.addMember(1);
b.addMember(13);
b.addMember(15);
b.addMember(18);
b.print();
set c = a.findUnion(b);
c.print();
set d= a.findIntersection(b);
d.print();
a.clear();
c= a.findUnion(b);
a.print();
c.print();
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote