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

Sets are widely used in many computer and real life applications. In this Lab Te

ID: 3768452 • Letter: S

Question

Sets are widely used in many computer and real life applications. In this Lab Test you will develop a class in C++ to represent a set of letters [a-z]. Set can be represented as a dynamic array. Each set of letters may form many words. Example if L={a,h,s} then words will be formed : ash and has. Your class should satisfy the following specifications:

A. Name your class “classSet” with three member variable: set(as dynamic array), length and maxSize.

B. Provide a constructor to do both initialize an empty set with a maximum of 50 and the actual size is zero.

C. Provide insert item function (InsertItem) to insert an item to the list.

D. Provide a destructor, copy constructor and an overloaded assignment operator.

E. Overload the stream insertion (<<) and (>>)operator to output a set.


F. Overload the (==) operator to check if two sets are equal.


G. Overload the minus (-) operator to return the intersection between two sets. For example if A and B are sets then A – B should return the intersection set between A and B (i.e. common elements in both sets).


H. Overload the plus (+) operator to return the combination between two sets. For example if A and B are sets then A + B should return a set that contains all the elements in either or both of the sets.

Explanation / Answer

#include <iostream>

using namespace std;

class classSet
{
public:

   char *set;
   int length,maxSize;

   classSet();
   classSet(const classSet &obj);
   ~classSet();
   void insertItem(char item);
   bool operator==(const classSet& obj);
   classSet operator+(const classSet& obj);
   classSet operator-(const classSet& obj);

   friend ostream &operator<<(ostream &output, const classSet &obj)
   {
       int i;
       output<<"{";
       for(i=0;i<obj.length-1;i++)
           output<<obj.set[i]<<", ";

       output<<obj.set[i]<<"} ";
   }

   friend istream &operator>>(istream &input, classSet &obj)
   {
       char item;
       input>>item;
       obj.insertItem(item);
   }


};

classSet::classSet()
{
   set = new char[50];
   length = 0;
   maxSize = 50;
}

classSet::classSet(const classSet &obj)
{
   set = new char[50];
   length = obj.length;
   maxSize = 50;
   int i;
   for(i=0;i<obj.length;i++)
       set[i] = obj.set[i];
}

classSet::~classSet()
{  
   delete(set);
}

void classSet::insertItem(char item)
{
   if(length!=50)
       set[length++] = item;
   else
       cout<<"The set is full can't insert anymore items ";
}

bool classSet::operator==(const classSet& obj)
{
   int i,j;
   bool found;

   if(obj.length!=length)
       return false;
   else
   {
   for(i=0;i<length;i++)
       {
           found = false;
           for(j=0;j<length;j++)
               if(set[i] == obj.set[j])
                   found = true;

           if(!found)
               return false;
       }
   return true;
   }
}

classSet classSet::operator+(const classSet& obj)
{
int i,j;
bool found;
classSet temp_obj = obj;

for(i=0;i<length;i++)
   {
       found = false;
       for(j=0;j<obj.length;j++)
           if(set[i] == obj.set[j])
               found = true;

       if(!found)
           temp_obj.insertItem(set[i]);
   }

return temp_obj;
  
}

classSet classSet::operator-(const classSet& obj)
{
int i,j;
bool found;
classSet temp_obj;

for(i=0;i<length;i++)
   {
       found = false;
       for(j=0;j<obj.length;j++)
           if(set[i] == obj.set[j])
               found = true;

       if(found)
           temp_obj.insertItem(set[i]);
   }

return temp_obj;
}

int main()
{
   classSet obj1,obj2;
   obj1.insertItem('a');
   obj1.insertItem('h');
   obj1.insertItem('s');

   cout<<"obj1::"<<obj1;
   classSet obj3 = obj1; //copy constructor
   cout<<"obj3::"<<obj3;

   //to show overloaded ==
   if(obj1==obj3)
       cout<<"Equal ";
   else
       cout<<"Unequal ";

   obj2.insertItem('s');
   obj2.insertItem('b');
   //to show overloaded - +
   cout<<obj1-obj2;
   cout<<obj1+obj2;

   //to show overloaded input
   classSet obj4;
   for(int i=0;i<5;i++)
       cin>>obj4;

   cout<<obj4;

   return 1;
}

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