Question: For this lab you will implement a dynamic array by completing the class called MyDynamicArray. The MyDynamicArray class should manage the storage of an array
For this lab you will implement a dynamic array by completing the class called MyDynamicArray. The MyDynamicArray class should manage the storage of an array that can grow and shrink.
The public methods of your class should be the following:
MyDynamicArray(); Default Constructor. The array should be of size 2.
MyDynamicArray(int s); For this constructor the array should be of size s.
~MyDynamicArray(); Destructor for the class.
int& operator[](int i); Traditional [] operator. Should print a message if i is out of bounds and return a reference to a zero value.
void add(int v); increases the size of the array by 1 and stores v there. Should double the capacity when the new element doesn't fit.
void del(); reduces the size of the array by 1. Should shrink the capacity when only 25% of the array is in use after the delete.
int length(); returns the length of the array.
int clear(); Frees any space currently used and starts over with an array of size 2.
you should also add a copy constructor and a copy assignment operator for the class.
You should complete the class by writing the add and del methods. When you need to grow the array size, the add method should print out the message Doubling to : followed by the new size of the array. When the del method shrinks the size of the array, print out the message Reducing to : followed by the new size of the array.
The output below should result from running the main.cpp file with your MyDynamicArray class:
Doubling to : 4 Doubling to : 8 Doubling to : 16 Doubling to : 32 Doubling to : 64 Doubling to : 128 Doubling to : 256 The sum is : 8385 Reducing to : 128 Reducing to : 64 Reducing to : 32 Reducing to : 16 Out of bounds reference : 10 Doubling to : 200 Doubling to : 400 The sum is : 171600 The sum is : 168133 10 Out of bounds reference : 2 2
main.cpp:
#include
void foo(MyDynamicArray param) { for(int i=0; i int main() { MyDynamicArray x; for (int i=0; i<130; i++){ x.add(i); } int sum = 0; for (int i=0; i MyDynamicArray.cpp: #include
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
