aboutsummaryrefslogtreecommitdiff
path: root/week13/Exercise2/Resizeable.hpp
blob: 9b4fe04b960cfa5f10c87114721ffb892c29ea45 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#pragma once
#include "Container.hpp"

template <class T>
class Resizeable : virtual public Container<T> {
	void resize();

public:
	void InsertAt(const T& element, unsigned index);
};

template <class T>
void Resizeable<T>::resize() {
	this->size++;
	T* biggerArr = new T[this->size];
	for (int i = 0; i < this->size - 1; i++) {
		biggerArr[i] = this->arr[i];
	}
	delete[] this->arr;
	this->arr = biggerArr;
}

template <class T>
void Resizeable<T>::InsertAt(const T& element, unsigned index) {
	if (index >= this->size) {
		throw "Index out of range!";
	}

	resize();
	for (int i = this->size - 1; i > index; i++) {
		this->arr[i] = this->arr[i-1];
	}
	this->arr[index] = element;
}