include using namespace std const static int DS5 study this

#include using namespace std; const static int DS=5; //study this class carefully. It models a notion of a \"Bag\" of ints. //A Bag can contain duplicate items. class My Bag private: int* b; int cap; int size; public: //The following functions have been implemented My Bag); My-Bag ( int ) ; My_Bag (); bool drop (int); bool pick(int); void display(); bool assiqn (My Bag&)

Solution

Modified code;

Code:

#include<iostream>
using namespace std;
const static int DS=5;
class My_Bag
{
private:
   int *b;
   int cap;
   int size;
public:
   My_Bag();
   My_Bag(int);
   ~My_Bag();
   bool drop(int);
   bool pick(int);
   void display();
   bool assign(My_Bag&);
   My_Bag(int,int);
   bool operator=(const My_Bag&);
   My_Bag operator+(const My_Bag&);
   My_Bag operator-(const My_Bag&);
   bool operator==(const My_Bag&);
   bool operator>(const My_Bag&);

};
bool My_Bag::assign(My_Bag& q)
{
   if(size!=q.size || cap!=q.cap)
       return false;
   for(int i=0;i<size;i++)
       b[i]=q.b[i];
   return true;
}
My_Bag::My_Bag()
{
   cap=DS;
   size=0;
   b=new int[cap];
}
My_Bag::My_Bag(int c)
{
   if(c<=0)
       cap=DS;
   else
       cap=c;
   size=0;
   b=new int[cap];
}
My_Bag::~My_Bag()
{
   cout<<\"Cleaning up....\ \";
   delete[] b;
   cout<<\"Done.\ \";
}
bool My_Bag:: drop(int item)
{
   if(size==cap)
       return false;
   b[size]=item;
   ++size;
   return true;
}
bool My_Bag:: pick(int item)
{
   int index=-1;
   for(int i=0;i<size;i++)
   {
       if(b[i]==item)
       {
           for(int j=1;j<size;j++)
               b[j]=b[j+1];
           --size;
           return true;
       }
   }
   return false;
}
void My_Bag::display()
{
   cout<<\"Bag capacity: \"<<cap<<endl;
   cout<<\"Curremt size: \"<<size<<endl;
   for(int i=0;i<size;i++)
       cout<<b[i]<<\' \';
   cout<<endl;
   cout<<endl;
}
My_Bag::My_Bag(int c,int item)
{
   if(c<=0)
       cap=DS;
   else
       cap=c;
   size=0;
   b=new int[cap];
   for(int i=0;i<cap;i++)
       b[i]=item;
}
bool My_Bag:: operator=(const My_Bag& q)
{
   cap=q.cap;
   size=q.size;
   for(int i=0;i<q.cap;i++)
       b[i]=q.b[i];
   return true;
}
My_Bag My_Bag:: operator+(const My_Bag& q)
{
   My_Bag bb;
   bb.cap=cap+q.cap;
   bb.size=size+q.size;
   int i;
   bb.b=new int[bb.cap];
   for(i=0;i<size;i++)
       bb.b[i]=b[i];
   for(int k=0;k<q.size;k++)
       bb.b[i+k]=q.b[k];
   return bb;
}
My_Bag    My_Bag ::operator-(const My_Bag& q)
{
   int flag=0;
   My_Bag bb;
   int* bbb=new int[q.cap];
   int ind=0;
   for(int i=0;i<size;i++)
   {
       flag=0;
       for(int k=0;k<q.size;k++)
       {
           if(b[i]!=q.b[k])
               flag=1;
       }
       if(flag==1)
           bbb[ind++]=b[i];

   }
   bb.cap=q.cap;
   bb.b=new int[bb.cap];
   for(int k=0;k<ind;k++)
       bb.b[k]=bbb[k];
   return bb;
}
bool My_Bag ::operator==(const My_Bag& q)
{
   int flag=0;
if(size!=q.size || cap!=q.cap)
       return false;
   for(int i=0;i<size;i++)
   {
       flag=0;
       for(int k=0;k<q.size;k++)
       {
           if(b[i]==q.b[k])
           {
               flag=1;
               break;
           }
       }
       if(flag==0)
           return false;;

   }
   return true;

}
bool My_Bag :: operator>(const My_Bag& q)
{
   int flag=0;
   if(size>q.size)
   {
       for(int i=0;i<q.size;i++)
       {
           for(int k=0;k<size;k++)
           {
               if(q.b[i]==b[k])
               {
                   flag=1;
                   break;
               }
           }
           if(flag==0)
           {
               return false;
           }
       }
       return true;
   }
   else
       return false;

}
int main()
{
   My_Bag b1(5);
   b1.drop(10);
   b1.drop(10);
   b1.drop(20);
   b1.pick(10);
   b1.drop(20);
   b1.drop(30);
   b1.drop(10);
   b1.pick(20);


}

 #include using namespace std; const static int DS=5; //study this class carefully. It models a notion of a \
 #include using namespace std; const static int DS=5; //study this class carefully. It models a notion of a \
 #include using namespace std; const static int DS=5; //study this class carefully. It models a notion of a \
 #include using namespace std; const static int DS=5; //study this class carefully. It models a notion of a \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site