blob: 699997ec264824958c07bcd262fb402edfc83f49 (
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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;
}
|