-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraining4_3.cpp
More file actions
277 lines (236 loc) · 6.37 KB
/
training4_3.cpp
File metadata and controls
277 lines (236 loc) · 6.37 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// /*
// 복사생성자 -깊은 복사와 얕은 복사
// 소멸자
// */
// #include<iostream>
// #include<string.h>
// class Marine{
// int hp;
// int coord_x, coord_y;
// int damage;
// bool is_dead;
// char* name;
// public:
// Marine();
// Marine(int x, int y);
// Marine(int x, int y, const char* marine_name);
// ~Marine(); //소멸자는 인자를 가지지 않는다.
// int attack();
// void be_attacked(int damage_earn);
// void move(int x, int y);
// void show_status();
// };
// Marine::Marine(){
// name =NULL;
// hp=50;
// coord_x=coord_y=0;
// damage = 5;
// is_dead = false;
// }
// Marine::Marine(int x, int y){
// coord_x = x;
// coord_y = y;
// hp =50;
// damage=5;
// is_dead = false;
// name = NULL;
// }
// Marine::Marine(int x, int y, const char* marine_name){
// name = new char[strlen(marine_name)+1]; //동적으로 생성한 char 배열 -> 객체가 삭제돼도 없어지지않는다. ->소멸자필요
// strcpy(name,marine_name);
// coord_x = x;
// coord_y = y;
// hp = 50;
// damage = 5;
// is_dead = false;
// }
// void Marine:: move(int x, int y){
// coord_x = x;
// coord_y = y;
// }
// void Marine::show_status(){
// std::cout <<"*** Marine *** " << std::endl;
// std::cout <<" Location :( " << coord_x << " , " << coord_y << " ) " << std::endl;
// std::cout << " HP : " << hp << std::endl;
// }
// int Marine::attack(){ return damage;}
// void Marine::be_attacked(int damage_earn){
// hp-= damage_earn;
// if(hp <= 0) is_dead =true;
// }
// Marine::~Marine(){
// std::cout << name <<"소멸자의 호출" << std::endl;
// if(name != NULL){
// delete[] name; //배열동적할당엔 delete[]
// }
// }
// int main(){
// // Marine marine1(2, 3);
// // Marine marine2(3, 5);
// // marine1.show_status();
// // marine2.show_status();
// // std::cout << std::endl << "마린 1 이 마린 2 를 공격! " << std::endl;
// // marine2.be_attacked(marine1.attack());
// // marine1.show_status();
// // marine2.show_status();
// // 객체를 배열로 사용
// // Marine* marines[100];
// // marines[0] = new Marine(2,3); // malloc과 달리 new는 생성자도 동시에 호출해 준다.
// // marines[1] = new Marine(3,5);
// // marines[0]->show_status();
// // marines[1]->show_status();
// // std::cout <<std::endl << "마린 1이 마린 2를 공격" <<std::endl;
// // marines[0]->be_attacked(marines[1]->attack());
// // marines[0]->show_status();
// // marines[1]->show_status();
// // delete marines[0];
// // delete marines[1];
// // Marine* marines[100];
// // marines[0] = new Marine(2, 3, "Marine 2");
// // marines[1] = new Marine(1, 5, "Marine 1");
// // marines[0]->show_status();
// // std::cout << std::endl << "마린 1 이 마린 2 를 공격! " << std::endl;
// // marines[0]->be_attacked(marines[1]->attack());
// // marines[0]->show_status();
// // marines[1]->show_status();
// // delete marines[0];
// // delete marines[1];
// }
// 복사 생성자의 중요성
// #include <string.h>
// #include <iostream>
// class Photon_Cannon
// {
// int hp, shield;
// int coord_x, coord_y;
// int damage;
// char *name;
// public:
// Photon_Cannon(int x, int y);
// Photon_Cannon(int x, int y, const char *cannon_name);
// Photon_Cannon(const Photon_Cannon &pc);
// ~Photon_Cannon();
// void show_status();
// };
// Photon_Cannon::Photon_Cannon(int x, int y)
// {
// hp = shield = 100;
// coord_x = x;
// coord_y = y;
// damage = 20;
// name = NULL;
// }
// Photon_Cannon::Photon_Cannon(const Photon_Cannon &pc)
// {
// std::cout << "복사 생성자 호출! " << std::endl;
// hp = pc.hp;
// shield = pc.shield;
// coord_x = pc.coord_x;
// coord_y = pc.coord_y;
// damage = pc.damage;
// name = new char[strlen(pc.name) + 1];
// strcpy(name, pc.name);
// }
// Photon_Cannon::Photon_Cannon(int x, int y, const char *cannon_name)
// {
// hp = shield = 100;
// coord_x = x;
// coord_y = y;
// damage = 20;
// name = new char[strlen(cannon_name) + 1];
// strcpy(name, cannon_name);
// }
// Photon_Cannon::~Photon_Cannon()
// {
// if (name)
// delete[] name;
// }
// void Photon_Cannon::show_status()
// {
// std::cout << "Photon Cannon :: " << name << std::endl;
// std::cout << " Location : ( " << coord_x << " , " << coord_y << " ) "
// << std::endl;
// std::cout << " HP : " << hp << std::endl;
// }
// int main()
// {
// Photon_Cannon pc1(3, 3, "Cannon");
// Photon_Cannon pc2 = pc1;
// pc1.show_status();
// pc2.show_status();
// }
#include <iostream>
#include <string.h>
class string
{
char *str;
int len;
public:
string(char c, int n); // 문자 c 가 n 개 있는 문자열로 정의
string(const char *s);
string(const string &s);
~string();
void add_string(const string &s); // str 뒤에 s 를 붙인다.
void copy_string(const string &s); // str 에 s 를 복사한다.
int strlen(); // 문자열 길이 리턴
void show();
};
string::string(char c, int n){
str = new char[n+1];
for (int i = 0; i < n; i++)
{
str[i] = c;
}
str[n]='\0';
len=n;
}
string::string(const char* s){
int i=0;
while(s[i] != '\0'){
i++;
}
str = new char[i];
strcpy(str,s);
len = i;
}
string::string(const string &s){
len = s.len;
str = new char[len+1];
strcpy(str,s.str);
}
string::~string(){
if(str) delete[] str;
}
void string::add_string(const string &s){
char* tmp = str;
str=new char[len+s.len+1];
strcpy(str,tmp);
strcpy(str+len,s.str);
len+=s.len;
}
void string::copy_string(const string &s){
len = s.len;
str=new char[len+1];
strcpy(str,s.str);
}
int string::strlen(){
return len;
}
void string::show(){
std::cout<<str<<std::endl;
}
int main(){
string s1("hello");
string s2= s1;
string s4('a',10);
s2.add_string(" world");
s2.show();
string s3("good");
s3.copy_string(s2);
s3.show();
s4.show();
std::cout<<s1.strlen()<<std::endl;
std::cout<<s2.strlen()<<std::endl;
std::cout<<s3.strlen()<<std::endl;
std::cout<<s4.strlen()<<std::endl;
}