#include #include "StockRecordArray.h" StockRecordArray::StockRecordArray( int sz ) { size = sz; data = new (StockRecord *)[sz]; } StockRecordArray::~StockRecordArray() { delete [] data; } void StockRecordArray::set_value(int index, StockRecord * value) { checkBounds(index); data[index] = value; } // set the value at location index StockRecord * StockRecordArray::get_value(int index) { checkBounds(index); return data[index]; } // make sure that the index is 1) non-negative and 2) not beyond the // array's current length. If the index is beyond the array's current // length, keep doubling the array's size until the array's length // exceeds the index. Then copy the elements in the old array to the new // array. void StockRecordArray::checkBounds(int index) { if (index < 0) { cout << "Index " << index << " is negative" << endl; exit(1); } if (index >= size) { int old_size = size; StockRecord **old_data = data; int i; while (index >= size) size *= 2; data = new (StockRecord *)[size]; for (i = 0; i < old_size; i++) data[i] = old_data[i]; delete [] old_data; } }