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 <iostream>
using namespace std;
#define INITIAL_CAPACITY 5
template <class T>
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 > size) || (pos < 0)) {
cout << "Invalid index";
return;
}
for(int i = pos; i <= size; i++) {
array[i] = array[i+1];
}
size--;
}
void insertAt(int element, int pos) {
if((pos > size) || (pos < 0)) {
cout << "Invalid index";
return;
}
if(size == MIN_CAPACITY) {
resize();
}
size++;
for(int i = size - 1; i >= 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 < size; i++) {
temp[i] = array[i];
}
}
// returns element in x position.
T get(int pos) {
return array[pos];
}
};
int main() {
dynamic_array<int> dynArr;
dynArr.append(3);
dynArr.append(4);
dynArr.append(5);
dynArr.append(4);
dynArr.append(33);
dynArr.append(3);
dynArr.deleteAt(6);
return 0;
}