Set Class First combine the two into one set by overloading
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 should submit a single lastnameHW2.zip containing: ArrayBag.h, ArrayBag.cpp, main.cpp, SetFunctions.h, SetFunctions.cpp
Need main.cpp and setfunctions.cpp and setfunctions.h to be built.
Solution
//Following is the code for given problem statement
//SetFunction.h
class SetFunction
{
public:
ArrayBag operator+(ArrayBag a,ArrayBag b);
ArrayBag operator-(ArrayBag a,ArrayBag b);
};
//SetFunction.cpp
#include \"SetFunctio.h\"
ArrayBag SetFnctions::operator+(ArrayBag a,ArrayBag b) //Function for union operation
{
ArrayBag ans=a;
Vector bb=b.toVector(();
int len=b.getCurrentSize();
for(int i=0;i<len;i++)
{
if(!ans.contains(bb[i]))
ans.add(bb[i]);
}
return ans;
}
ArrayBag SetFunctions::operator-(ArrayBag a,ArrayBag b) //Function for A-B operation
{
ArrayBag ans=new ArrayBag();
ans=a;
int len=b.getCurrentSize();
for(int i=0;i<len;i++)
{
if(ans.contains(bb[i]))
ans.remove(bb[i]);
}
return ans;
}
//Main file
#include <stdio.h>
#include <iostream>
using namespace std;
#include \"arraybag.h\"
int main()
{
ArrayBag a=new ArrayBag();
ArrayBag b=new ArrayBag();
ifstream infile;
infile.open(\"setinput.txt\");
string line;
if(getline(infile,line))
{ istringstream iss(line);
while(!iss.eof())
{
int x;
iss>>x;
cout<<x<<\" \";
a.add(x);
}
}
if(getline(infile,line))
{ istringstream iss(line);
while(!iss.eof())
{
int x;
iss>>x;
cout<<x<<\" \";
b.add(x);
}
}
}

