aboutsummaryrefslogtreecommitdiff
path: root/week05/Exercise2.cpp
blob: 06489314b9390f0455889d8ee8e3d05e43ed4a9f (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
45
46
struct FloatArray {
private:
	float* arr;
	unsigned size;

	void free() {
		delete[] arr;
	}
	void copyFrom(const FloatArray& other) {
		this->size = other.size;
		this->arr = new float[size];
		for (int i = 0; i < size; i++) {
			this->arr[i] = other.arr[i];
		}
	}

public:
	FloatArray(unsigned size) {
		this->size = size;
		arr = new float[size];
	}
	~FloatArray() {
		free();
	}
	FloatArray(const FloatArray& other) {
		copyFrom(other);
	}
	FloatArray& operator=(const FloatArray& other) {
		if (this != &other) {
			free();
			copyFrom(other);
		}
		return *this;
	}

	float GetElem(unsigned index) {
		if (index >= size) return 0.0;

		return arr[index];
	}
	void SetElem(unsigned index, float value) {
		if (index >= size) return;

		arr[index] = value;
	}
};