C++ Idk how to refactor the code below to use smartpointers and implement the class as a Template class. Add a function to test your DynamicArray changes. Note: Apply the template type to the declaration int* base as T* base: template class DynamicArray { T* base; ... Other 'T' changes will also be needed in the original code. If there are any errors in the code, please fix it to let it become a good program. class DynamicArray { int* base; int size = 0; int capacity = 0; public: DynamicArray(int c = 10) : capacity(c), size(0) { allocate(capacity); } void set(int v, int offset) { if (offset >= capacity) resize(capacity * 2); base[offset] = v; size++; } int get(int offset) { return base[offset]; } int length() { return size; } int extent() { return capacity; } int begin() { return base[0]; } int end() { return base[size - 1]; } void push(int t) { set(t, size); } void allocate(int c) { capacity = c; base = new int[capacity]; } void resize(int nusize) { int* temp = new int[nusize]; for (int i = 0; i < capacity; i++) temp[i] = base[i]; delete[] base; base = temp; capacity = nusize; } void pop() { for (int i = 1; i < size; i++) base[i - 1] = base[i]; size--; } void clear() { delete[] base; size = 0; capacity = 0; } int& operator[](int index) { if (index > -1 && index < capacity) return base[index]; else { return base[0]; } } void Output(ostream& color(ostream&)) { cout << endl; for (int i = 0; i < size; i++) cout << color << base[i] << "\t"; cout << endl; } };
C++ Idk how to refactor the code below to use smartpointers and implement the class as a Template class. Add a function to test your DynamicArray changes. Note: Apply the template type to the declaration int* base as T* base:
template <typename T>
class DynamicArray
{
T* base;
...
Other 'T' changes will also be needed in the original code.
If there are any errors in the code, please fix it to let it become a good program.
class DynamicArray
{
int* base;
int size = 0;
int capacity = 0;
public:
DynamicArray(int c = 10) : capacity(c), size(0) { allocate(capacity); }
void set(int v, int offset)
{
if (offset >= capacity)
resize(capacity * 2);
base[offset] = v;
size++;
}
int get(int offset) { return base[offset]; }
int length() { return size; }
int extent() { return capacity; }
int begin() { return base[0]; }
int end() { return base[size - 1]; }
void push(int t) { set(t, size); }
void allocate(int c)
{
capacity = c;
base = new int[capacity];
}
void resize(int nusize)
{
int* temp = new int[nusize];
for (int i = 0; i < capacity; i++)
temp[i] = base[i];
delete[] base;
base = temp;
capacity = nusize;
}
void pop()
{
for (int i = 1; i < size; i++)
base[i - 1] = base[i];
size--;
}
void clear()
{
delete[] base;
size = 0;
capacity = 0;
}
int& operator[](int index)
{
if (index > -1 && index < capacity)
return base[index];
else
{
return base[0];
}
}
void Output(ostream& color(ostream&))
{
cout << endl;
for (int i = 0; i < size; i++)
cout << color << base[i] << "\t";
cout << endl;
}
};
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 4 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)