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;     } };

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

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;
    }
};

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 4 images

Blurred answer
Knowledge Booster
Introduction to Template
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education