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

Set Class First, combine the two into one set by overloading the + (union) opera

ID: 3785223 • Letter: S

Question

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.

Explanation / Answer

//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);
   }
}
}

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