Website Summary Files Commits Branches @commit @diff @stats
path: root / Week09 / Exercise02 / Vector.h
diff options
context:
space:
mode:
Commitb38346eebca9a007971af02fb3bb9472c5e16d0c (patch)
Author
 Syndamia <kamen@syndamia.com>
2026-04-26 21:43:31 +0300
Committer
 Syndamia <kamen@syndamia.com>
2026-04-26 21:43:31 +0300
Files575a64089a17be271aec84683f426252d8f5d1f9 /Week09/Exercise02/Vector.h
oop-2025-solutions-b38346eebca9a007971af02fb3bb9472c5e16d0c.tar
oop-2025-solutions-b38346eebca9a007971af02fb3bb9472c5e16d0c.tar.gz
oop-2025-solutions-b38346eebca9a007971af02fb3bb9472c5e16d0c.zip
Parent7d98b664c9e459a08f2e6e6fa5b636686dfed554 (diff)
feat(Week09): Add solutions
1 files changed, 59 insertions, 0 deletions
-rw-r--r--Week09/Exercise02/Vector.h59
diff --git a/Week09/Exercise02/Vector.h b/Week09/Exercise02/Vector.h
new file mode 100644
index 0000000..bf7b0c3
--- /dev/null
+++ b/Week09/Exercise02/Vector.h
@@ -0,0 +1,59 @@
+#ifndef _VECTOR
+#define _VECTOR
+
+class Vector {
+ double* numbers;
+ unsigned numbersCount;
+ unsigned numbersAlloc;
+
+ void free();
+ void copyFrom(const Vector& other);
+
+public:
+ Vector();
+ ~Vector();
+ Vector(const Vector& other);
+ Vector& operator=(const Vector& other);
+
+ Vector(Vector&& other);
+ Vector& operator=(Vector&& other);
+
+ unsigned GetCount() const;
+ const double* GetNumbers() const;
+
+ // Добра конвенция при бинарни (двуаргументни) оператори е да
+ // дефинираме като член-функции съответните _= оператори (+=, -=, ...)
+ // и след това да дефинираме _ опеарторите (+, -, ...) като нормални
+ // функции, които вътрешно използват _=
+
+ Vector& operator+=(const Vector& other);
+ Vector& operator-=(const Vector& other);
+ Vector& operator*=(double scalar);
+ double operator*=(const Vector& other);
+ Vector& operator^=(const Vector& other);
+
+ double operator&() const;
+
+ // Понеже предефинираме &, не можем да направим &vector, за да
+ // вземем указател от референция към обект. За нашите цели е нужно,
+ // затова си правим помощни методи които "реимплементират" същата логика.
+ Vector* ptr();
+ const Vector* ptr() const;
+};
+
+Vector operator+(const Vector& lhs, const Vector& rhs);
+Vector operator-(const Vector& lhs, const Vector& rhs);
+
+// Понеже лявия и десния аргумент се различават по тип,
+// така поддържаме комутативност
+Vector operator*(const Vector& lhs, double scalar);
+Vector operator*(double scalar, const Vector& rhs);
+
+double operator*(const Vector& lhs, const Vector& rhs);
+Vector operator^(const Vector& lhs, const Vector& rhs);
+
+bool operator||(const Vector& lhs, const Vector& rhs);
+bool operator==(const Vector& lhs, const Vector& rhs);
+bool operator!=(const Vector& lhs, const Vector& rhs);
+
+#endif /* _VECTOR */