Website Summary Files Commits Branches @commit @diff @stats
path: root / Week04 / Exercise02 / IntArray.h
blob: 0ed99c929c095d2bdb7c9d7e487584b401a5f0d8 (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
35
36
37
38
39
40
41
42
43
44
#ifndef _INTARRAY
#define _INTARRAY

class IntArray {
	int* elems;
	unsigned capacity;

	/* Как трябва да изглежда голяма четворка: */

	void free() {
		delete[] elems;
	}
	void copyFrom(const IntArray& other) {
		capacity = other.capacity;

		elems = new int[capacity];
		for (unsigned i = 0; i < capacity; ++i)
			elems[i] = other.elems[i];
	}
public:
	IntArray() {
		elems = nullptr;
		capacity = 0;
	}
	~IntArray() {
		free();
	}
	IntArray(const IntArray& other) {
		copyFrom(other);
	}
	IntArray& operator=(const IntArray& other) {
		if (this != &other) {
			free();
			copyFrom(other);
		}
		return *this;
	}

	int At(unsigned index);
	void Resize(unsigned capacity);
	void Concat(const IntArray& endElements);
};

#endif /* _INTARRAY */