Website Summary Files Commits Branches @commit @diff @stats
path: root / pract KN / week8 / Vector.cpp
diff options
context:
space:
mode:
Commit65f6397bb288ab3a8313e26259d02df43f7e1454 (patch)
Author
 Stoycho Kyosev <68247567+stoychoX@users.noreply.github.com>
2022-04-12 20:13:46 +0300
Committer
 GitHub <noreply@github.com>
2022-04-12 20:13:46 +0300
Files45d7579b3e1d36249196697f64ba693da542046f /pract KN/week8/Vector.cpp
Object-oriented_programming_FMI-65f6397bb288ab3a8313e26259d02df43f7e1454.tar
Object-oriented_programming_FMI-65f6397bb288ab3a8313e26259d02df43f7e1454.tar.gz
Object-oriented_programming_FMI-65f6397bb288ab3a8313e26259d02df43f7e1454.zip
Parent8f89ceead9f267194d0ca2b66bbda00730654d45 (diff)
Add files via upload
1 files changed, 90 insertions, 0 deletions
-rw-r--r--pract KN/week8/Vector.cpp90
diff --git a/pract KN/week8/Vector.cpp b/pract KN/week8/Vector.cpp
new file mode 100644
index 0000000..b288e12
--- /dev/null
+++ b/pract KN/week8/Vector.cpp
@@ -0,0 +1,90 @@
+#include "Vector.h"
+
+void Vector::resize()
+{
+ capacity *= 2;
+ int* newArr = new int[capacity];
+ for (size_t i = 0; i < size; i++)
+ {
+ newArr[i] = arr[i];
+ }
+ free();
+ arr = newArr;
+}
+
+void Vector::free()
+{
+ delete[] arr;
+}
+
+void Vector::copyFrom(const Vector& other)
+{
+ size = other.size;
+ capacity = other.capacity;
+ arr = new int[capacity];
+ for (int i = 0; i < size; i++)
+ {
+ arr[i] = other.arr[i];
+ }
+}
+
+Vector::Vector()
+{
+ arr = new int[2];
+ capacity = 2;
+ size = 0;
+}
+
+Vector::Vector(const Vector& other)
+{
+ copyFrom(other);
+}
+
+Vector& Vector::operator=(const Vector& other)
+{
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+Vector::~Vector()
+{
+ free();
+}
+
+void Vector::push_back(const int element)
+{
+ if (size < capacity) {
+ arr[size] = element;
+ size++;
+ }
+ else
+ {
+ resize();
+ push_back(element);
+ }
+}
+
+int Vector::getSize() const
+{
+ return size;
+}
+
+int& Vector::operator[](const int index)
+{
+ if (index >= 0 && index < size) {
+ return arr[index];
+ }
+ throw std::exception("Out of range exception");
+
+}
+
+const int& Vector::operator[](int index) const
+{
+ if (index >= 0 && index < size) {
+ return arr[index];
+ }
+ throw std::exception("Out of range exception");
+}