Website Summary Files Commits Branches @commit @diff @stats
path: root / Week10 / Exercise03 / Inventory.cpp
diff options
context:
space:
mode:
Commit7546f0d0da487e16f0a8671c09a46e1cee2bc03a (patch)
Author
 Syndamia <kamen@syndamia.com>
2026-05-02 14:57:36 +0300
Committer
 Syndamia <kamen@syndamia.com>
2026-05-02 14:57:36 +0300
Files6462868cebe114a87ca38252af9a49bce6603286 /Week10/Exercise03/Inventory.cpp
oop-2025-solutions-7546f0d0da487e16f0a8671c09a46e1cee2bc03a.tar
oop-2025-solutions-7546f0d0da487e16f0a8671c09a46e1cee2bc03a.tar.gz
oop-2025-solutions-7546f0d0da487e16f0a8671c09a46e1cee2bc03a.zip
Parentc98c633b7087e3c52f4b05236f3509a9ec5f2bba (diff)
feat(Week10): Add solutions
1 files changed, 73 insertions, 0 deletions
-rw-r--r--Week10/Exercise03/Inventory.cpp73
diff --git a/Week10/Exercise03/Inventory.cpp b/Week10/Exercise03/Inventory.cpp
new file mode 100644
index 0000000..699997e
--- /dev/null
+++ b/Week10/Exercise03/Inventory.cpp
@@ -0,0 +1,73 @@
+#include "Inventory.h"
+
+void Inventory::free() {
+ delete[] products;
+}
+
+void Inventory::copyFrom(const Inventory& other) {
+ productsCount = other.productsCount;
+ productsAlloc = other.productsAlloc;
+
+ products = new Product[productsAlloc];
+ for (unsigned i = 0; i < productsCount; ++i)
+ products[i] = other.products[i];
+}
+
+void Inventory::resize() {
+ productsAlloc = productsAlloc == 0 ? 8 : productsAlloc * 2;
+ Product* productsLarger = new Product[productsAlloc];
+
+ for (unsigned i = 0; i < productsCount; ++i)
+ productsLarger[i] = products[i];
+
+ delete[] products;
+ products = productsLarger;
+}
+
+Inventory::Inventory() : products(), productsCount(), productsAlloc()
+{ }
+
+Inventory::~Inventory() {
+ free();
+}
+
+Inventory::Inventory(const Inventory& other) {
+ copyFrom(other);
+}
+
+Inventory& Inventory::operator=(const Inventory& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+Inventory::Inventory(Inventory&& other) {
+ products = other.products;
+ productsCount = other.productsCount;
+ productsAlloc = other.productsAlloc;
+
+ other.products = nullptr;
+ other.productsAlloc = other.productsCount = 0;
+}
+
+Inventory& Inventory::operator=(Inventory&& other) {
+ if (this != &other) {
+ free();
+
+ products = other.products;
+ productsCount = other.productsCount;
+ productsAlloc = other.productsAlloc;
+
+ other.products = nullptr;
+ other.productsAlloc = other.productsCount = 0;
+ }
+ return *this;
+}
+
+void Inventory::AddProduct(const Product& product) {
+ if (productsCount >= productsAlloc)
+ resize();
+ products[productsCount++] = product;
+}