I'm designing a program that mimics a small banking program. There's two classes I'm trying to design right now, which is the transaction class and the account class. I need the account class to contain an array of transaction objects, but I need it to be sized dynamically as the account performs more transactions. Is there a way to dynamically size an array of objects in a class, and if so, what would be the method to doing that?
EDIT: The requirements state that I CANNOT utilize the STL/vectors.
But here's the caveat, and I should have specified this before. The requirements state that I CANNOT utilize the STL/vectors. Otherwise, I would have banged this out days ago.
To resize, you'll have to create a new (bigger) array, copy/move all of the elements in the old array to the new one, destroy the old array, and set the pointer to the array to point to the new array.
So something like
1 2 3 4 5 6 7 8 9
void Account::resize(int newSize)
{
Transaction* newArray = new Transaction[newSize];
// Copy everything over
for (int i = 0; i < /* old size -- probably need an extra member variable for that*/; ++i)
newArray[i] = transactions[i];
delete[] transactions;
transactions = newArray;
}