From 70c2c3eab85bee3100ce1c749af03937a6e11e17 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 28 Mar 2024 09:43:24 +0200 Subject: [w5] Added solutions to exercises 1-7 --- week05/Exercise2.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 week05/Exercise2.cpp (limited to 'week05/Exercise2.cpp') diff --git a/week05/Exercise2.cpp b/week05/Exercise2.cpp new file mode 100644 index 0000000..0648931 --- /dev/null +++ b/week05/Exercise2.cpp @@ -0,0 +1,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; + } +}; -- cgit v1.2.3