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;
}
};
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 4 images