#include <iostream>
#include <stdlib.h>

template <class C>
class bag{
public:
  bag(int s = 10){ //constructor
    size = s;
    A = new C [size];
    currentSize=0;
  }

  bag (const bag & rhs){//copy constructor
    size = rhs.size;
    A = new C [size];
    currentSize = rhs.currentSize;
    for (int i=0; i<currentSize; i++) A[i] = rhs.A[i];
  }

  ~bag(){delete [] A;} //destructor

  const bag & operator = (const bag & rhs){//operator =
    if (this != &rhs){
      delete [] A;
      size = rhs.size;
      A = new C [size];
      currentSize = rhs.currentSize;
      for (int i=0; i<currentSize; i++) A[i] = rhs.A[i];
    }
    return *this;
  }

  friend ostream & operator << <C> (ostream &, const bag<C> &);
  //NOT a member of the class -- just a friend!

  int maxSize(){return size;} //accessor

  void place(C f){ //a mutator
    if (currentSize < size) A[currentSize++] = f;
  }
  C grab(){ // another mutator
    int x = rand() % currentSize;
    C retVal = A[x];
    currentSize -=1;
    for (int i=x; i<currentSize; i++) A[i] = A[i+1];
    return retVal;
  }
private:
  int size;
  int currentSize;
  C * A;
};
//end of the class bag

template <class C>
ostream & operator <<(ostream & os, const bag<C> & b){
  os<<"[";
  for (int i=0; i<b.currentSize; i++) os<< b.A[i] << "\t";
  os << "]\n";
  return os;
}

template <class C>
void makeZeroCopy(bag<C> b){
  for (int i=0; i<b.maxSize(); i++) b.grab();
  for (int i=0; i<b.maxSize(); i++) b.place(0);
  cout << "b copy= " <<b;  
}
int main(){
  int s;
  cout <<"Seed the random-number generator with an integer: "; cin >> s;
  srand(s);

  cout <<"Input bag-size: "; cin >> s;
  bag<float> B(s), D(s);

  for (int i=0; i<B.maxSize(); i++) B.place((float) i);//filling bag B
  D=B; // copying B onto D
  cout << "\tB= " <<B;
  cout << "\tD= " <<D;

  cout<<"Grabbing from bag B:";
 for (int i=0; i<B.maxSize(); i++) cout << "\t" << B.grab();  cout << endl;

  cout << "\tB= " <<B;
  cout << "\tD= " <<D;
  makeZeroCopy(D);
  cout << "D= " <<D;
}
