-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplates.cpp
More file actions
558 lines (333 loc) · 8.66 KB
/
Templates.cpp
File metadata and controls
558 lines (333 loc) · 8.66 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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#include <cstdio>
struct Trazar {
Trazar(const char* nombre): nombre{ nombre } {
printf("%s construida.\n", nombre);
}
~Trazar() {
printf("%s destruida.\n", nombre);
}
private:
const char* const nombre;
};
static Trazar t1{ "Variable estatica" };
thread_local Trazar t2{ "Variable hilo-local" };
int main() {
printf("A\n");
Trazar t3{ "Variable Automatica" };
printf("B\n");
const auto* t4 = new Trazar{ "Variable Dinamica" };
printf("C\n");
}
/*#include <cstdio>
struct PoderNuclear {
static int poder_nuclear;
static void aumentar_poder_nuclear(int isotopos_nucleares) {
poder_nuclear = poder_nuclear + isotopos_nucleares;
const auto calor_perdido = poder_nuclear * 20;
if (calor_perdido > 10000) {
printf("Advertencia: Sobrecalentamiento\n");
}
printf("Poder Nuclear: %d\n", poder_nuclear);
}
};
int PoderNuclear::poder_nuclear = 200;
int main() {
printf("Poder Nuclear: %d\n", PoderNuclear::poder_nuclear);
PoderNuclear::aumentar_poder_nuclear(100);
PoderNuclear::aumentar_poder_nuclear(500);
return 0;
}*/
/*#include <cstdio>
struct PoderNuclear {
static int poder_nuclear;
static void aumentar_poder_nuclear(int isotopos_nucleares) {
poder_nuclear = poder_nuclear + isotopos_nucleares;
const auto calor_perdido = poder_nuclear * 20;
if (calor_perdido > 10000) {
printf("Advertencia: Sobrecalentamiento\n");
}
printf("Poder Nuclear: %d\n", poder_nuclear);
}
};
int PoderNuclear::poder_nuclear = 200;
int main() {
PoderNuclear::aumentar_poder_nuclear(100);
PoderNuclear::aumentar_poder_nuclear(500);
}*/
/*#include <cstdio>
void aumentar_poder_nuclear(int isotopos_nucleares) {
thread_local int poder_nuclear = 200;
poder_nuclear = poder_nuclear + isotopos_nucleares;
const auto calor_perdido = poder_nuclear * 20;
printf("Poder Nuclear: %d\n", poder_nuclear);
if (calor_perdido > 10000) {
printf("Advertencia, Sobrecalentamiento\n");
}
}
int main() {
//printf("Poder Nuclear: %d\n", poder_nuclear);
aumentar_poder_nuclear(100);
aumentar_poder_nuclear(500);
return 0;
}*/
/*void poder_nuclear(int isotopos_nucleares) {
int calor_perdido = 0;
//sigue
}*/
/*#include <cstdio>
//La variable local tiene el mismo nombre que un miembro
class Test
{
private:
int x;
public:
void setX(int x)
{
//El puntero'this' es usado para recuperar el objeto de x
//oculto por la variable local 'x'
this->x = x;
}
void print() { printf("x = %d\n", x); }
};
int main()
{
Test obj;
int x = 20;
obj.setX(30);
obj.print();
printf("x = %d\n", x);
return 0;
}*/
/*#include <cstdio>
class Test
{
private:
int x;
int y;
public:
//Constructor que toma dos parámetros int, en la definición usamos el puntero
//this para tener una referencia al Objeto que llama.
Test(int x = 0, int y = 0) { this->x = x; this->y = y; }
//Creamos dos referencias a métodos que reciben
//un int que asigna a la dirección que apunta this
Test& setX(int a) { x = a; return *this; }
Test& setY(int b) { y = b; return *this; }
void print() { printf("x = %d\n", x); printf("y = %d\n", y); }
};
int main()
{
Test obj1(5, 5);
obj1.print();
// Llamadas a funciones encadenadas. Todas las llamadas modifican el mismo objeto
// ya que el mismo objeto se devuelve por referencia
obj1.setX(10).setY(20);
obj1.print();
return 0;
}*/
/*#include <cstdio>
int main() {
int original = 100;
int& original_ref = original;
printf("Original: %d\n", original);
printf("Referencia: %d\n", original_ref);
int nuevo_valor = 200;
original_ref = nuevo_valor;
printf("Original: %d\n", original);
printf("Nuevo Valor: %d\n", nuevo_valor);
printf("Reference: %d\n", original_ref);
};*/
/*int original = 100;
int& original_ref = original;
printf("Original: %d\n", original);
printf("Referencia: %d\n", original_ref);
int nuevo_valor = 200;
int& original_ref = nuevo_valor;
printf("Original: %d\n", original);
printf("Nuevo Valor: %d\n", nuevo_valor);
printf("Reference: %d\n", original_ref);
nuevo_valor = 300;
printf("Original: %d\n", original);
printf("Nuevo Valor: %d\n", nuevo_valor);
printf("Reference: %d\n", original_ref);*/
/*#include <cstdio>
struct Elemento {
Elemento* next{};
void insert_after(Elemento* new_element) {
new_element->next = next;
next = new_element;
}
char prefix[2];
short operating_number;
};
int main() {
Elemento trooper1, trooper2, trooper3;
trooper1.prefix[0] = 'T';
trooper1.prefix[1] = 'K';
trooper1.operating_number = 421;
trooper1.insert_after(&trooper2);
trooper2.prefix[0] = 'F';
trooper2.prefix[1] = 'N';
trooper2.operating_number = 2187;
trooper2.insert_after(&trooper3);
trooper3.prefix[0] = 'L';
trooper3.prefix[1] = 'S';
trooper3.operating_number = 005;
for (Elemento* cursor = &trooper1; cursor; cursor = cursor->next) {
printf("stormtrooper %c%c-%d\n",
cursor->prefix[0],
cursor->prefix[1],
cursor->operating_number);
}*/
/*#include <cstdio>
struct Elemento {
Elemento* next{};
void insert_after(Elemento* new_element) {
new_element->next = next;
next = new_element;
}
char prefix[2];
short operating_number;
};
int main() {
Elemento trooper1, trooper2, trooper3;
trooper1.prefix[0] = 'T';
trooper1.prefix[1] = 'K';
trooper1.operating_number = 421;
trooper1.insert_after(&trooper2);
trooper2.prefix[0] = 'F';
trooper2.prefix[1] = 'N';
trooper2.operating_number = 2187;
trooper2.insert_after(&trooper3);
trooper3.prefix[0] = 'L';
trooper3.prefix[1] = 'S';
trooper3.operating_number = 005;
for (Elemento* cursor = &trooper1; cursor; cursor = cursor->next) {
printf("stormtrooper %c%c-%d\n",
cursor->prefix[0],
cursor->prefix[1],
cursor->operating_number);
}*/
/*//Ejemplo de uso de Referencias
int main()
{
int x = 10;
// ref es una referencia a x.
int& ref = x;
// Valor de x se cambia a 20
ref = 20;
printf("El valor de x es: %d.\n", x);
// Valor de x es ahora cambiado a 30
x = 30;
printf("El valor de ref es: %d.\n", ref);
}*/
/*#include <cstdio>
int main()
{
int x = 10;
// ref es una referencia a x.
int& ref = x;
// Valor de x se cambia a 20
ref = 20;
printf("El valor de x es: %d.\n", x);
// Valor de x es ahora cambiado a 30
x = 30;
printf("El valor de ref es: %d.\n", ref);
return 0;
}*/
/*#include <cstdio>
struct Element {
Element* next{};
void insert_after(Element* new_element) {
new_element->next = next;
next = new_element;
}
char prefix[2];
short operating_number;
};
int main() {
Element trooper1, trooper2, trooper3;
trooper1.prefix[0] = 'T';
trooper1.prefix[1] = 'K';
trooper1.operating_number = 421;
trooper1.insert_after(&trooper2);
trooper2.prefix[0] = 'F';
trooper2.prefix[1] = 'N';
trooper2.operating_number = 2187;
trooper2.insert_after(&trooper3);
trooper3.prefix[0] = 'L';
trooper3.prefix[1] = 'S';
trooper3.operating_number = 005;
for (Element* cursor = &trooper1; cursor; cursor = cursor->next) {
printf("stormtrooper %c%c-%d\n",
cursor->prefix[0],
cursor->prefix[1],
cursor->operating_number);
}
}*/
/*#include <cstdio>
struct Hucha {
Hucha(int moneda_in) {
if (!set_moneda(moneda_in)) {
moneda = 200;
}
}
Hucha() {
moneda = 200;
}
void add_moneda() {
moneda++;
}
bool set_moneda(int nueva_moneda) {
if (nueva_moneda < 200)
return false;
moneda = nueva_moneda;
return true;
}
int get_moneda() {
return moneda;
}
private:
int moneda;
};
void add_moneda(Hucha& ref_hucha) {
ref_hucha.set_moneda(ref_hucha.get_moneda() + 1); // No necesitamos operador deref
}
int main() {
Hucha hucha;
printf("Las monedas son %d.\n", hucha.get_moneda());
hucha.add_moneda();
add_moneda(hucha); // ¡Hucha es implícitamente pasado por referencia!
printf("Las monedas son %d.\n", hucha.get_moneda());
}*/
/*#include <cstdio>
void intercambiar(int& primero, int& segundo)
{
int temp = primero;
primero = segundo;
segundo = temp;
}
int main()
{
int a = 2, b = 3;
intercambiar(a, b);
printf("El valor de a es: %d.\n", a);
printf("El valor de b es: %d.\n", b);
return 0;
}*/
/*#include <cstdio>
using nombrespace std;
int main() {
// declare simple variables
int i;
double d;
// declare reference variables
int& r = i;
double& s = d;
i = 5;
printf("El valor de i: %d.\n", i);
printf("El valor de la referencia a i: %d.\n", r);
d = 11.7;
printf("El valor de d: %f.\n", d);
printf("El valor de la referencia a d: %f.\n", s);
return 0;
}*/