// file thing.C // implementation file for thing // #include #include "thing1.h" #include // constructor thing::thing (int size /*=10*/, inOrder O) { //note - must not use default value here _inorder = O; _count = 0; _bucketsize = size; _bucket = new int[size]; cout << "default constructor called - object located at" << this << endl; } thing::~thing() { cout << "destructor of object at location " << this << endl; delete [] _bucket; } void thing::removelast(){ if (_count > 0) { _bucket[--_count] = -1; } } // append an int to the end of the array void thing::append (int addendum) { int * newbucket; if (_count >= _bucketsize) // we need some more space { newbucket = new int[2 * _bucketsize]; // copy old array into new one for (int x = 0; x < _bucketsize; ++x) newbucket[x] = _bucket[x]; // no memory leak delete [] _bucket; // set host's size and bucket to what we just did _bucket = newbucket; _bucketsize *=2; } // we are guaranteed to have space for the next item now _bucket[ _count++] = addendum; } void thing::display () { cout << "thing location =" << this << " size = " << _bucketsize << "; count = " << _count << "; in order flag = " << _inorder << endl; for (int x = 0; x < _count ; ++x ) { cout << _bucket[x] << " "; if ((x+1)%8 == 0) cout << endl; } cout << endl; } // overloaded operator = // overloaded operator [] int& thing::operator[] (int x) { if (x < 0 || x >_count) { cerr << "out of range index\n"; exit(1); } return (_bucket[x]); }