-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOOP_cpp.cpp
More file actions
427 lines (336 loc) · 7.39 KB
/
Copy pathOOP_cpp.cpp
File metadata and controls
427 lines (336 loc) · 7.39 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/* Вызов конструктора базового класса
class A {
public:
A() {
msg = "Message";
}
A(string msg) {
this->msg = msg;
}
void PrintMsg() {
cout << msg << endl;
}
private:
string msg;
};
class B : public A {
public:
B():A("New message") {
}
};
int main() {
//setlocale(LC_ALL,"ru");
//B b;
//b.PrintMsg();
//system("pause");
return 0;
}
*/
/*
* Полиморфизм
* Виртуальные функции
* Абстрактные классы
* Чисто виртуальные функции
* virtual
* override
// Абстрактный класс
class Weapon {
public:
virtual void Shoot() = 0;
void Foo(){
cout << "Foo()" << endl;
}
};
class Gun : public Weapon {
public:
void Shoot() override {
cout << "Shoot from gun" << endl;
}
};
class SubMachineGun : public Gun {
void Shoot() override {
cout << "Shoot from machineGun" << endl;
}
};
class Bazooka : public Weapon {
void Shoot() override {
cout << "Shoot from bazooka" << endl;
}
};
class Knife : public Weapon{
public:
void Shoot() override{
cout << "Knife" << endl;
}
};
class Player {
public:
void Shoot(Weapon *weapon){
weapon->Shoot();
}
};
int main() {
Gun gun;
SubMachineGun machineGun;
Bazooka bazooka;
Knife knife;
//Gun *weapon = &gun;
//Gun *weapon = &machineGun;
//weapon->Shoot();
Player player;
player.Shoot(&bazooka);
knife.Foo();
return 0;
}
*/
/* Виртуальный деструктор
class A{
public:
A() {
cout << "New HEAP resources class A" << endl;
}
virtual ~A() {
cout << "Deleted HEAP resources class A" << endl;
}
};
class B : public A{
public:
B() {
cout << "New HEAP resources class B" << endl;
}
~B() override {
cout << "Deleted HEAP resources class B" << endl;
}
};*/
/* Чисто виртуальный деструктор. Используется для вызова деструктора класса родителя
class A{
public:
A() {
}
virtual ~A() = 0;
};
// обязательно объявить вне класса
A::~A() {};
class B : public A{
public:
B() {
}
~B() override {
}
};
int main() {
A *b_ptr = new B;
delete b_ptr;
return 0;
}
*/
/* Делегирующий конструктор
class Human{
public:
Human(string Name){
this->Name = Name + "!";
this->Age =0;
this->Weight = 0;
}
Human(string Name, int Age): Human(Name) {
this->Age = Age;
}
Human(string Name, int Age, int Weight): Human(Name, Age) {
this->Weight = Weight;
}
string Name;
int Age;
int Weight;
};
int main() {
Human human("Aristarh", 30, 70);
return 0;
}
*/
/* Вызов виртуального метода базового класса
class Msg{
public:
Msg(string msg){
this->msg = msg;
}
virtual string GetMsg(){
return msg;
}
private:
string msg;
};
class BraketsMsg : public Msg {
public:
BraketsMsg(string msg): Msg(msg){
}
string GetMsg() override{ // Явное указание метода базового класса
return "[" + ::Msg::GetMsg() + "]";
}
};
class Printer{
public:
void Print(Msg *msg){
cout << msg->GetMsg() << endl;
}
};
int main() {
setlocale(LC_ALL,"ru");
BraketsMsg m("Hello!");
Printer p;
p.Print(&m);
return 0;
}
*/
/* Множественное наследование
* Порядок вызова конструкторов и деструкторов при множественном наследовании
* Проблема одинаковых методов в классах
class Car{
public:
string str = "Field car";
Car(){
cout << "Call creator Car" << endl;
}
void Drive() {cout << "I drive!" << endl;}
void Use() {cout << "I drive!" << endl;}
~Car(){
cout << "Call destroyer Car" << endl;
}
};
class AirPlain{
public:
string str2 = "Field plain";
AirPlain(){
cout << "Call creator AirPlain" << endl;
}
void Fly(){cout << "I fly!" << endl;}
void Use(){cout << "I fly!" << endl;}
~AirPlain(){
cout << "Call destroyer AirPlain" << endl;
}
};
class FlyingCar : public Car, public AirPlain{
public:
FlyingCar(){
cout << "Call creator FlyingCar" << endl;
}
~FlyingCar(){
cout << "Call destroyer FlyingCar" << endl;
}
};
int main() {
setlocale(LC_ALL,"ru");
FlyingCar fc;
//fc.Drive();
//fc.Fly();
//Car *ptrC = &fc;
//AirPlain *ptrA = &fc;
//((Car)fc).Use();
//((AirPlain)fc).Use();
fc.Car::Use();
fc.AirPlain::Use();
return 0;
}
*/
/* Интерфейсы
class IBicycle{
public:
virtual void TwistTheWheel() =0;
virtual void Ride() = 0;
};
class SimpleBicycle : public IBicycle{
public:
void TwistTheWheel() override {
cout << "метод TwistTheWheel() SimpleBicycle" << endl;
}
void Ride() override{
cout << "метод Ride() SimpleBicycle" << endl;
}
};
class SportBicycle : public IBicycle{
public:
void TwistTheWheel() override {
cout << "метод TwistTheWheel() SportBicycle" << endl;
}
void Ride() override{
cout << "метод Ride() SportBicycle" << endl;
}
};
class Human{
public:
void RideOn(IBicycle & bicycle) {
cout << "Крутим руль" << endl;
bicycle.TwistTheWheel();
cout << "Поехали" << endl;
bicycle.Ride();
}
};
int main() {
SetConsoleOutputCP(CP_UTF8);
SimpleBicycle sb;
SportBicycle spb;
Human human;
human.RideOn(sb);
human.RideOn(spb);
return 0;
}
*/
/* Ромбовидное наследование
* Виртуальное наследование
class Component {
public:
Component(string companyName) {
cout << "Конструктор Component" << endl;
this->companyName = companyName;
}
string companyName;
};
class GPU : public Component{
public:
GPU(string companyName) : Component(companyName) {
cout << "Конструктор GPU" << endl;
}
};
class Memory : public Component{
public:
Memory(string companyName) : Component(companyName) {
cout << "Конструктор Memory" << endl;
}
};
class GraphicCard : public GPU, public Memory {
public:
GraphicCard(string GPUCompanyName, string MemoryCompanyName) : Memory(GPUCompanyName),
GPU(MemoryCompanyName) {
cout << "Конструктор GraphicCard" << endl;
}
};
class Character {
public:
Character() {
cout << "Конструктор Character" << endl;
}
int HP;
};
class Elf : public virtual Character{
public:
Elf() {
cout << "Конструктор Elf" << endl;
}
};
class Warrior : public virtual Character{
public:
Warrior() {
cout << "Конструктор Warrior" << endl;
}
};
class ElfWarrior : public Elf, public Warrior {
public:
ElfWarrior() {
cout << "Конструктор ElfWarrior" << endl;
}
};
int main() {
SetConsoleOutputCP(CP_UTF8);
GraphicCard gc("AMD", "Samsung");
ElfWarrior ew;
return 0;
}*/