// file thing.C // implementation file for thing // #include #include "thing2.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; } //copy constructor does deep copy thing::thing (const thing & T) { _inorder = T._inorder; _count = T._count; _bucketsize = T._bucketsize; _bucket = new int[_bucketsize]; // I should make sure that allocation succeeded, but I won't here for (int x = 0; x < _bucketsize; ++x) _bucket[x] = T._bucket[x]; cout << "copy constructor called - object created at " << this; } 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 = thing& thing::operator = (const thing & T) { // check for X = X (same object assign if (this == &T) return *this; // different objects -- we have work to do // first = no mem leaks! delete [] _bucket; _inorder = T._inorder; _bucketsize = T._bucketsize; _count = T._count; _bucket = new int[_bucketsize]; for (int x = 0; x < _count ; ++x) _bucket[x] = T._bucket[x]; return *this; } // overloaded operator [] int& thing::operator[] (int x) { if (x < 0 || x >_count) { cerr << "out of range index\n"; exit(1); } return (_bucket[x]); }