-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCat.h
227 lines (175 loc) · 3.73 KB
/
Cat.h
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#pragma once
#include <iostream>
#include <ostream>
using namespace std;
/**
* \brief this is my class cat for animal description
*/
class cat
{
public:
cat(const cat& other)
: nick_(other.nick_),
lapki_(other.lapki_),
weight_(other.weight_)
{
}
cat(cat&& other) noexcept
: nick_(other.nick_),
lapki_(other.lapki_),
weight_(other.weight_)
{
}
cat& operator=(const cat& other)
{
if (this == &other)
return *this;
nick_ = other.nick_;
lapki_ = other.lapki_;
weight_ = other.weight_;
return *this;
}
cat& operator=(cat&& other) noexcept
{
if (this == &other)
return *this;
nick_ = other.nick_;
lapki_ = other.lapki_;
weight_ = other.weight_;
return *this;
}
private:
char* nick_;
friend bool operator<(const cat& lhs, const cat& rhs);
friend bool operator<=(const cat& lhs, const cat& rhs);
friend bool operator>(const cat& lhs, const cat& rhs);
friend bool operator>=(const cat& lhs, const cat& rhs);
friend std::ostream& operator<<(std::ostream& os, const cat& obj);
int lapki_;
double weight_;
friend bool operator==(const cat& lhs, const cat& rhs);
friend bool operator!=(const cat& lhs, const cat& rhs);
public:
cat(char* nick, int lapki, double weight);
char* get_nick() const;
void set_nick(char* const nick);
int get_lapki() const;
void set_lapki(const int lapki);
double get_weight() const;
void set_weight(const double weight);
::cat& operator=(int i);
cat(char* nick, int lapki);
/**
* \brief default ctor
*/
cat();
// copy c-tor
cat(cat& another);
~cat();
static void CopyTest();
};
inline bool operator<(const cat& lhs, const cat& rhs)
{
if (lhs.nick_ < rhs.nick_)
return true;
if (rhs.nick_ < lhs.nick_)
return false;
if (lhs.lapki_ < rhs.lapki_)
return true;
if (rhs.lapki_ < lhs.lapki_)
return false;
return lhs.weight_ < rhs.weight_;
}
inline bool operator<=(const cat& lhs, const cat& rhs)
{
return !(rhs < lhs);
}
inline bool operator>(const cat& lhs, const cat& rhs)
{
return rhs < lhs;
}
inline bool operator>=(const cat& lhs, const cat& rhs)
{
return !(lhs < rhs);
}
inline std::ostream& operator<<(std::ostream& os, const cat& obj)
{
os << "nick_: " << obj.nick_
<< " lapki_: " << obj.lapki_
<< " weight_: " << obj.weight_;
return os;
}
inline bool operator==(const cat& lhs, const cat& rhs)
{
return lhs.nick_ == rhs.nick_
&& lhs.lapki_ == rhs.lapki_
&& lhs.weight_ == rhs.weight_;
}
inline bool operator!=(const cat& lhs, const cat& rhs)
{
return !(lhs == rhs);
}
inline cat::cat(char* nick, int lapki, double weight): nick_(nick),
lapki_(lapki),
weight_(weight)
{
}
inline char* cat::get_nick() const
{
return nick_;
}
inline void cat::set_nick(char* const nick)
{
nick_ = nick;
}
inline int cat::get_lapki() const
{
return lapki_;
}
inline void cat::set_lapki(const int lapki)
{
lapki_ = lapki;
}
inline double cat::get_weight() const
{
return weight_;
}
inline void cat::set_weight(const double weight)
{
weight_ = weight;
}
inline ::cat& cat::operator=(int i)
{
this->lapki_ = i;
return *this;
}
inline cat::cat(char* nick, int lapki): nick_(nick),
lapki_(lapki)
{
}
inline cat::cat()
{
cout << "Default Cat C-tor!\n";
nick_ = new char[200];
strcpy_s(nick_, 200, "Barsik");
}
inline cat::cat(cat& another)
{
cout << "Copy Cat C-tor!\n";
// default code:
// nick = another.nick; // bad! two pointers on the same memory
nick_ = new char[200];
strcpy_s(nick_, 200, another.nick_);
}
inline cat::~cat()
{
cout << "Cat Destructor!\n";
delete[] nick_;
}
inline void cat::CopyTest()
{
// add to main:
// Cat::CopyTest();
cat original;
cat copy = original;
}