Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ I need help implementing some methods to dynamically resize an array using templates. Here are the methods: You will need to create and ArrayList

C++

I need help implementing some methods to dynamically resize an array using templates.

Here are the methods:

You will need to create and ArrayList class that implements a dynamically resizable array using templates.

Here are the methods I am trying to implement:

//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

Below you will find my .h file.

NOTE: I would appreciate comments that explain how the pointers (*) and ampersand (&) sign are working in these methods and how they all relate to eachother. I am having trouble understanding/grapsing this concept.

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

#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 */

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine Ricardo

2nd Edition

1449606008, 978-1449606008

More Books

Students also viewed these Databases questions

Question

Show that 1 V/m = 1 N/C.

Answered: 1 week ago

Question

=+ 9. What is inflation and what causes it?

Answered: 1 week ago

Question

=+6. What does the invisible hand of the marketplace do?

Answered: 1 week ago