Dynamic Array Implementation in C++ I've finished implementing a dynamic array in C++. I'm a beginner in C++ and also in algorithms and data structures, so any constructive feedback about improving the implementation would be greatly appreciated. 

#include &lt;iostream&gt;

using namespace std;
#define INITIAL_CAPACITY 5

template &lt;class T&gt;
class dynamic_array {
    T *array;
    int MIN_CAPACITY = INITIAL_CAPACITY;
    int GROWTH_FACTOR = 2;
    int size;

public:
    // constructor init
    dynamic_array() {
        array = new T[MIN_CAPACITY];
        size = 0;
    }

    // append @ the end
    void append(T element) {
        if(size == MIN_CAPACITY) {
            resize();
        }
        array[size] = element;
        size++;

    }

    void deleteAt(int pos) {
        if((pos &gt; size) || (pos &lt; 0)) {
            cout &lt;&lt; "Invalid index";
            return;
        }

        for(int i = pos; i &lt;= size; i++) {
            array[i] = array[i+1];
        }
        size--;
    }

    void insertAt(int element, int pos) {
        if((pos &gt; size) || (pos &lt; 0)) {
            cout &lt;&lt; "Invalid index";
            return;
        }

        if(size == MIN_CAPACITY) {
            resize();
        }

        size++;

        for(int i = size - 1; i &gt;= pos; i--) {
            if(i == pos) {
                array[i] = element;
            } else {
                array[i] = array[i-1];
            }
        }
    }

    // returns size of array
    int length() {
        return size;
    }

    // doubles capacity if it has to and deletes reference to current array.
    void resize() {
        MIN_CAPACITY *= GROWTH_FACTOR;
        T *temp = new T[MIN_CAPACITY];
        copy(temp);
        delete [] array;
        array = temp;

    }

    // copies original array into temp
    void copy(T temp[]) {
        for(int i = 0; i &lt; size; i++) {
            temp[i] = array[i];
        }
    }

    // returns element in x position.
    T get(int pos) {
        return array[pos];
    }
};



int main() {
    dynamic_array&lt;int&gt; dynArr;
    dynArr.append(3);
    dynArr.append(4);
    dynArr.append(5);
    dynArr.append(4);
    dynArr.append(33);
    dynArr.append(3);

    dynArr.deleteAt(6);

    return 0;
}
The code is pretty good as it is. Here is how I would improve it:

Constant handling

In C++, you should prefer const over #define. So

#define INITIAL_CAPACITY 5


becomes

const int INITIAL_CAPACITY = 5;


Also don't use all uppercase names for non constant variables. It is
confusing and breaks the 50 year old tradition.

Clearer names

I renamed a few of your variables:


MIN_CAPACITY =&gt; capacity Because the instance variable holds the
current capacity of the dynamic array, not the minimum capacity.
length() =&gt; size The words length and size are synonymous but
using them both can cause confusion. A reader might think is the
length the number of elements in the array and size the allocacted
size, or vice versa?


Offby one errors

You have an offby one error in deleteAt. If you have a dynamic array
with 5 elements, then their positions are 0 to 4 so deleteAt(5)
shouldn't work, but it does. I've fixed that for you.

Pretty printing

Adding pretty printing functions is almost always a good idea because
they make debugging much easier. I've added one for you.

Error handling

Your error handling consists of printing to stdout and letting the
program continue to run. That is incorrect because errors might not be
discovered if stdout is redirected or the user is not paying attention
to what is printed.

There are many ways to handle errors. I've implemented basic assert
based error handling for you. But you can certainly be fancier and use
exceptions or status codes.

Functions implemented in terms of each other

For all dynamic arrays dynarr.append(x) is equivalent to
dynarr.insertAt(x, dynarr.size()). So append can just call
insertAt.

Destructor

Your dynamic array allocates memory in the constructor, but there is
no corresponding destructor that frees the memory. I've added one
looking like this:

~dynamic_array() {
    delete[] array;
}


Copying memory

There's a function called std::copy which you can use for copying
memory. That way, you don't have to write your own copy function.

Pointless comments

Writing good comments is hard. As a reviewer I much prefer no comments
over pointless comments. An example of a pointless comment is //
constructor init. I can see that the lines below is the constructor
so the comment doesn't tell me anything I didn't already know. Same
for the comment // returns size of array.

Source code

Here is the improved version of the dynamic array:

#include &lt;assert.h&gt;
#include &lt;cstring&gt;
#include &lt;iostream&gt;

using namespace std;

const int INITIAL_CAPACITY = 2;
const int GROWTH_FACTOR = 2;

template &lt;class T&gt;
class dynamic_array {
    T *array;
    int capacity = INITIAL_CAPACITY;
    int _size;

public:
    dynamic_array() {
        array = new T[capacity];
        _size = 0;
    }

    ~dynamic_array() {
        delete[] array;
    }

    void deleteAt(int pos) {
        assert(0 &lt;= pos &amp;&amp; pos &lt; _size);
        _size--;
        for (int i = pos; i &lt; _size; i++) {
            array[i] = array[i + 1];
        }
    }

    void insertAt(int element, int pos) {
        assert(0 &lt;= pos &amp;&amp; pos &lt;= _size);
        if(_size == capacity) {
            resize();
        }
        for(int i = _size; i &gt; pos; i--) {
            array[i] = array[i-1];
        }
        _size++;
        array[pos] = element;
    }

    void append(T element) {
        insertAt(element, _size);
    }

    int size() {
        return _size;
    }

    // doubles capacity if it has to and deletes reference to current array.
    void resize() {
        capacity *= GROWTH_FACTOR;
        T *temp = new T[capacity];
        copy(array, array + _size, temp);
        delete [] array;
        array = temp;
    }

    T get(int pos) {
        return array[pos];
    }

    void pretty_print() {
        cout &lt;&lt; "[";
        for (int i = 0; i &lt; _size - 1; i++) {
            cout &lt;&lt; array[i] &lt;&lt; " ";
        }
        if (_size) {
            cout &lt;&lt; array[_size - 1];
        }
        cout &lt;&lt; "]\n";
    }
};