-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventory.cpp
More file actions
86 lines (67 loc) · 1.59 KB
/
Inventory.cpp
File metadata and controls
86 lines (67 loc) · 1.59 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "Inventory.h"
Inventory::Inventory()
{
this->capacity = 12;
this->numOfItems = 0;
this->itemArr = new Item*[capacity];
this->initialize();
}
Inventory::~Inventory() //virtual deconstructor
{
for (size_t i = 0; i < this->numOfItems; i++)
{
delete this->itemArr[i];
}
delete[] this->itemArr;
}
Inventory::Inventory(const Inventory& obj) //copy constructor
{
this->capacity = obj.capacity;
this->numOfItems = obj.numOfItems;
this->itemArr = new Item * [this->capacity];
for (size_t i = 0; i < this->numOfItems; i++)
{
this->itemArr[i] = obj.itemArr[i]->clone();
}
initialize(this->numOfItems);
}
Item& Inventory::operator[](const int index)
{
if (index < 0 || index >= this->numOfItems)
throw("Bad Index.");
return *this->itemArr[index];
}
void Inventory::addItem(const Item& item)
{
if (this->numOfItems >= this->capacity)
{
expand();
}
this->itemArr[this->numOfItems++] = item.clone();
}
void Inventory::removeItem(int index)
{
}
void Inventory::initialize(const int from)
{
for (size_t i = from; i < this->capacity; i++)
{
this->itemArr[i] = nullptr;
}
}
void Inventory::expand()
{
//if inventory reaches maximum capacity,
//and an item is suddenly added
//the inventory will expand and double in size
this->capacity *= 2;
Item** tempArr = new Item* [this->capacity];
for (size_t i = 0; i < numOfItems; i++)
{
tempArr[i] = this->itemArr[i];
}
delete[] this->itemArr;
this->itemArr = tempArr;
//returns back to number of items
this->initialize(this->numOfItems);
}