The purpose of this lab is to give you more practice with pointers and to illustrate how to create a dynamic array with an overloaded [] operator.
One limitation of arrays is the fixed size. It would be nice if we could add or remove elements from an array. In previous programs we created a large array and only used part of it, but the unused elements waste memory. One way to avoid this problem is to make a class that creates a dynamic array of exactly the size we need.
A first version of such a class that can store integers is below. pNums
is a pointer to an array of ints that is allocated
to hold exactly numElements
integers. The constructor sets numElements
to 0 and the destructor deletes the dynamic array.
The add function adds a new integer to the array. Remove should remove the last element from the array.
There is also an overloaded [] operator that retrieves the integer at a specified index.
class DynamicIntArray { private: int numElements; // How many numbers in array int *pNums; // Pointer to array of numbers of size numElements public: DynamicIntArray(); ~DynamicIntArray(); void add(int num); // Add number to end void remove(); // Remove number from end int operator[](int i); // Overloaded [] operator to access number at index i int size(); // Returns how many numbers stored };
In the implementation file, most of the work is in the add
function. A new array is created that is one integer larger
than the current array. The current array is copied into the new one, the element to add is put at the end, and then the
old array is deleted. Finally, the newly created array is stored in pNums
.
DynamicIntArray::DynamicIntArray() : numElements(0), pNums(nullptr) { } DynamicIntArray::~DynamicIntArray() { // Free up memory when we are done with this dynamic array if (pNums != nullptr) delete[] pNums; } void DynamicIntArray::add(int num) { // Make a new array of size numElements+1 int *pNew = new int[numElements + 1]; // Copy items from old to new array if (pNums != nullptr) { for (int i = 0; i < numElements; i++) pNew[i] = pNums[i]; } // Add new element to the end pNew[numElements] = num; // Increment number of elements stored numElements++; // Delete old array delete[] pNums; // Point to new array pNums = pNew; } /**** * TO DO: Complete this method so it removes the last element * from the array, and shrinks the array so it is exactly large * enough to hold the data. * * If there are no items in the array then nothing should happen. * (Program should not crash). *****/ void DynamicIntArray::remove() { } /******* * TO DO: Print an error message if the index i we are trying to access is * out of range (i.e. it is negative or >= numElements). Return -1 if this occurs. *******/ int DynamicIntArray::operator[](int i) { if (pNums != nullptr) { return pNums[i]; } // Better to throw an exception in this case but we didn't cover that yet cout << "Error: null array" << endl; return -1; } int DynamicIntArray::size() { return numElements; }
The code will compile and allow you to add and access elements using []. However, the remove function is only a stub.
TO DO:
remove()
function so the last element in the array is removed. Use a similar process that is done in add()
except
shrink the size of the array by one. If there are no items in the array then nothing should happen.
DynamicIntArray a; cout << "Adding numbers to the array." << endl; a.add(5); a.add(7); a.add(9); a.add(99); // Outputs 5, 7, 9, 99 for (int i = 0; i < a.size(); i++) cout << a[i] << endl; // Should output an error message, out of range cout << endl; cout << "Accessing illegal indices" << endl; cout << a[-1] << endl; cout << a[5] << endl; cout << endl; cout << "Removing two items" << endl; a.remove(); // Removes 99 a.remove(); // Removes 9 // Outputs 5, 7 for (int i = 0; i < a.size(); i++) cout << a[i] << endl; cout << endl; cout << "Removing three items" << endl; a.remove(); // Removes 7 a.remove(); // Removes 5 a.remove(); // Shouldn't crash although array is empty // Outputs nothing for (int i = 0; i < a.size(); i++) cout << a[i] << endl;
Show your code/program to the instructor or lab assistant for credit, or email to kjmock@alaska.edu.