Question: You will need to create and ArrayList class that implements a dynamically resizable array using templates. You should implement the following methods: //Default Constructor --

You will need to create and ArrayList class that implements a dynamically resizable array using templates. You should implement the following methods:

//Default Constructor -- create a starting array with 10 slots/elements //Constructor (int size) -- create a starting array with given size //Copy constructor -- deep copy //Assignment operator -- deep copy //Destructor -- frees dynamically allocated memory

//set(int position, T value) sets the value at position //get(int position) gets the value at position //append(T value) appends the value to the end of the array -- extend the array if necessary //insert(int position, T value) inserts value at position -- moves everything else down one -- extend the array if necessary

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#ifndef ArrayList_hpp #define ArrayList_hpp #include

#define DEFAULT_SIZE 10

template class ArrayList{

public: ArrayList(); //array list ArrayList(const ArrayList& other); ArrayList& operator=(const ArrayList& other); ~ArrayList(); void append(T value); //"append" adds value to the end of the array //grows the array as needed. void print(std::ostream& out) private: void copyList(T* old, int size); T* arr; int size; int capacity;

};

template ArrayList::ArrayList() { capacity = [DEFAULT_SIZE]; size = 0; }

template ArrayList::ArrayList(const ArrayList& other) { for (j = 0; j < length; j++) arr[j] = other.arr[j]; }

template ArrayList& ArrayList::operator=(const ArrayList& other) { if(this != &other) { T* oldCopy = arr; copyList(other.arr, other.size); if (oldCopy) { delete[] oldCopy; } } } template ArrayList::~ArrayList() { if (arr) { delete[] arr; } }

template void ArrayList::copyList(T* copyArr, int copySize) { // create a new array T* newArray = new T[copySize]; // copy all the elements from copyArr to newArr for (int i = 0; i < copySize; i++) { newArray[i] = copyArr[i]; } arr = newArray; }

template void ArrayList::print(std::ostream& out) { for (int i = 0; i < size; i++) //Print to ostream out << arr[i] << " " << endl; }

void ArrayList::append(T value) { //Is there room? if (size == capacity) { copyList(arr, capacity * 2); //No -> make a new array //Copy over old values T* oldArray = arr; copyList(arr, capacity * 2); capacity = capacit y * 2; //Delete the old array delete[] oldArr; } //Store the value in the newly available space arr[size] = value; size++; }

#endif /* ArrayList_hpp */

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Here is what I have so far. My int main() is in my .cpp file, but I could use help writing out a couple of test cases.

As far as the code I have now, I am not confident in my code, please correct me and explain if something is wrong.

Also, I am still trying to undersatnd the concept of pointers and how they work. I have not fully grasped this concept, I would really appreciate comments in regards to the pointers and how they are working with this piece of code especially when it comes to knowing where and when to use the astrix(*) symbol and the (&) symbol.

I would appreciate the extra explanation/comments in the code to understand the funtionality of how the pointers are working.

I will leave a thumbs and comment, thank you for your time!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!