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

C++ Implement operators for “-“ and “-=” for the bag class. For two bags X and Y

ID: 3688644 • Letter: C

Question

C++

Implement operators for “-“ and “-=” for the bag class. For two bags X and Y, the X-Y contains all the items of X, with all items from Y removed. For example, suppose that X has seven copies of number 3, and Y has two copies of the number 3. Then X-Y will have five copies of the number 3. In the case when Y has more copies of an item then X does, the bag X-Y will have no copies of that item. For example, suppose that X has nine copies of number 8, and Y has 10 copies of the number 8. Then X-Y will have no 8s. The statement X -= Y should have the same effect as the assignment statement X=X-Y.

Explanation / Answer


#include <iostream>

using namespace std;

class bag {
   private:
       int cap;
       int size;
       float *p;
   public:
       bag(int = 10);
       bag(const bag&);
       ~bag();
       bag &operator =(const bag&);
       friend ostream& operator<<(ostream&, const bag&);
      
       float max();
       void increase();
       void add(float);
       bool remove_one(float);
       void remove_all(float);
       bool contains(float);
       int getSize();
       int size_one(float);

       bag operator+(const bag&);
       bag operator-(const bag&);
       bag operator-=(const bag&);


};

//constructor
bag::bag(int c){
   cap = c;
   size = 0;
   p = new float[c];
}

//destructor
bag::~bag(){
   if(p != NULL)
       delete[] p;
}

//Assignment Operator
bag& bag::operator=(const bag& other){
   //self-check
   if(this == &other)
       return *this;
   //clear
   if(this->p != NULL){
       delete[] this->p;
   }

   //copy
   this->p = new float[other.cap];
   this->cap = other.cap;
   this->size = other.size;
   std::copy(other.p, other.p + other.size, this->p);

   return *this;
}

//Copy Constructor
bag::bag(const bag& other){
   this->p = NULL;
   *this = other;
}

//Output
ostream& operator<<(ostream& out, const bag& b){
   for(int i = 0; i < b.size; i++)
       out << b.p[i] << ", ";
   return out;
}

//Add function
void bag::add(float x){
   //Check size
   if(this->size >= cap){
       increase();
   }
   p[size++] = x;
}

//increase
void bag::increase(){
   float* temp = new float[cap*2];
   std::copy(p, p+size, temp);
   delete[] p;
   p = temp;
   cap = cap * 2;
}

bool bag::remove_one(float x){
   for(int i = 0; i<size; i++){
       if(p[i] == x){
           p[i] = p[size-1];
           size--;
           return true;
       }
   }
   return false;
}

void bag::remove_all(float x){
   while(this->remove_one(x));
}

int bag::getSize(){
   return size;
}

float bag::max(){
   float max;
   max = this->p[0];
   for(int i = 1; i < size; i++)
   {
       if(p[i] > max)
           max = p[i];
   }

   return max;
}

int bag::size_one(float x){
   int count = 0;
   for(int i = 0; i< size; i++)
       if(p[i] == x)
           count++;
   return count;
}

bag bag::operator+(const bag& other){
   if(this->size == 0)
       return other;
   if(other.size == 0)
       return *this;

   bag new_bag;
   delete[] new_bag.p;
   new_bag.p = new float[cap + other.cap];
   new_bag.cap = cap + other.cap;
   new_bag.size = size + other.size;
  
   std::copy(this->p, this->p + this->size, new_bag.p);
   std::copy(other.p, other.p + other.size, new_bag.p + this->size);

   return new_bag;
}

bool bag::contains(float f){
   if(size == 0)
       return false;
   else{
       for(int i = 0; i < size; i++){
           if(p[i] == f)
               return true;
       }
   }
   return false;
}

bag bag::operator-(const bag& other){
   bag n;
  
   if(this->size == 0 || other.size == 0)
       return *this;
   if(this == &other)
       return n;

   bag a = *this;
   bag b = other;

   for(int i = 0; i<b.size; ){
       if(a.contains(b.p[i])){
           a.remove_one(b.p[i]);
           b.remove_one(b.p[i]);
       }
       else
           i++;
   }

   return a;
}

bag bag::operator-=(const bag& other){
   *this = *this - other;
   return *this;
}


int main() {
  
  
   //initiate a quadratic
   bag a;

   //put values 0-10 in bag (forces use of increase function)
   for(int i=0; i<11; i++){
       a.add(i);
   }
   a.add(1);

   //output size and max of A
   cout << "A Size: " << a.getSize() << endl;
   cout << "A Max: " << a.max() << endl;
   cout << a << endl;

   //remove max
   a.remove_one(10);

   //check that size and max adjust accordingly
   cout << "A Size: " << a.getSize() << endl;
   cout << "A Max: " << a.max() << endl;
   cout << a << endl;

   //remove two occurences of 1 and make sure size adjusts
   a.remove_all(1);
   cout << "A Size: " << a.getSize() << endl;
   cout << a << endl;
   //initiate new bag and add numbers 0-4 to bag
   bag b;
   for(int i=0; i<5; i++){
       b.add(i);
   }

   //make sure new bag is initiated successfully & correctly
   cout << "B Size: " << b.getSize() << endl;
   cout << "B Max: " << b.max() << endl;
   cout << b << endl;
   //initiate new bag
   bag c;

   //test assignment operator and addition operator
   c = a + b;

      
   cout << "C Size: " << c.getSize() << endl;
   cout << "C Max: " << c.max() << endl;
   cout << c << endl;
   cout << b << endl;
   //test - and -= operators simultaneously
   bag d;  
    d = c - a;

   cout << "D Size: " << d.getSize() << endl;
   cout << "D Max: " << d.max() << endl;
   cout << d << endl;
   //done tests
   return 0;


}


sample output

A Size: 12                                                                                                                                                  
A Max: 10                                                                                                                                                   
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1,                                                                                                                        
A Size: 11                                                                                                                                                  
A Max: 9                                                                                                                                                    
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1,                                                                                                                            
A Size: 9                                                                                                                                                   
0, 9, 2, 3, 4, 5, 6, 7, 8,                                                                                                                                  
B Size: 5                                                                                                                                                   
B Max: 4                                                                                                                                                    
0, 1, 2, 3, 4,                                                                                                                                              
C Size: 14                                                                                                                                                  
C Max: 9                                                                                                                                                    
0, 9, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4,                                                                                                                   
0, 1, 2, 3, 4,                                                                                                                                              
D Size: 5                                                                                                                                                   
D Size: 5                                                                                                                                                   
D Max: 4                                                                                                                                                    
1, 0, 2, 3, 4,