-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitem.cpp
More file actions
140 lines (110 loc) · 3.09 KB
/
Copy pathitem.cpp
File metadata and controls
140 lines (110 loc) · 3.09 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// item.cpp
// Michael Bottini
// Created for CS260
// June 22, 2015
// Implementation file for item class.
#include "item.h"
// Set by default to "" and 0, respectively.
item::item(const char* newName, double newWeight) {
_name = makeAndCopy(newName);
_weight = newWeight;
return;
}
// Uses the overloaded = operator right below. I used this method for all
// of the copy constructors.
item::item(const item& originalItem) {
_name = NULL;
*this = originalItem;
return;
}
item& item::operator =(const item& originalItem) {
if(_name) { // Avoids memory leaks if _name is already defined.
delete [] _name;
}
_name = makeAndCopy(originalItem.getName());
_weight = originalItem.getWeight();
return *this;
}
item::~item() {
if(_name) {
delete [] _name;
}
_weight = 0;
return;
}
char* item::getName() const {
return _name;
}
double item::getWeight() const {
return _weight;
}
void item::setName(const char* newName) {
if (_name) {
delete [] _name;
}
_name = makeAndCopy(newName);
return;
}
void item::setWeight(double newWeight) {
_weight = newWeight;
return;
}
void item::printSuccess() const {
std::cout << "You picked up a " << _name << ".\n";
return;
}
void item::printFailure() const {
std::cout << "You're not strong enough to pick up the "
<< _name
<< " with everything else you're carrying.\n";
return;
}
bool item::operator <(const item& other) const {
return (CIstrcmp(_name, other.getName()) < 0);
}
bool item::operator >(const item& other) const {
return (CIstrcmp(_name, other.getName()) > 0);
}
bool item::operator ==(const item& other) const {
return (CIstrcmp(_name, other.getName()) == 0);
}
bool item::operator !=(const item& other) const {
return (!(*this == other));
}
bool item::operator <=(const item& other) const {
return (!(*this > other));
}
bool item::operator >=(const item& other) const {
return (!(*this < other));
}
// Case Insensitive string comparison.
// Copies str1 and str2 to new locations, reduces these new locations
// to only lowercase, and compares them with strcmp.
int CIstrcmp(const char* str1, const char* str2) {
int returnValue = 0;
char *insensitiveStr1 = makeAndCopy(str1);
char *insensitiveStr2 = makeAndCopy(str2);
if(insensitiveStr1 && insensitiveStr2) {
toLower(insensitiveStr1);
toLower(insensitiveStr2);
returnValue = strcmp(insensitiveStr1, insensitiveStr2);
}
delete [] insensitiveStr1;
delete [] insensitiveStr2;
return returnValue;
}
void toLower(char* str) {
for(int i = 0; str[i]; i++) { // Will end when str[i] == \0.
str[i] = tolower(str[i]);
}
return;
}
// Takes a C-string, allocates a new space for it to go to, copies it over,
// and returns the new pointer. Used to deep-copy strings.
char* makeAndCopy(const char* newName) {
char *newPtr = new char[strlen(newName) + 1];
if(newPtr) {
strncpy(newPtr, newName, strlen(newName) + 1);
}
return newPtr; // Returns NULL if memory failure.
}