-
Notifications
You must be signed in to change notification settings - Fork 1
/
tomasi-lezione-05.html
527 lines (475 loc) · 36.5 KB
/
tomasi-lezione-05.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<meta name="author" content="Maurizio Tomasi">
<title>Laboratorio di TNDS – Lezione 5</title>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="https://unpkg.com/reveal.js@^4//dist/reset.css">
<link rel="stylesheet" href="https://unpkg.com/reveal.js@^4//dist/reveal.css">
<style>
.reveal .sourceCode { /* see #7635 */
/* overflow: visible; */
}
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
/* The extra [class] is a hack that increases specificity enough to
override a similar rule in reveal.js */
ul.task-list[class]{list-style: none;}
ul.task-list li input[type="checkbox"] {
font-size: inherit;
width: 0.8em;
margin: 0 0.8em 0.2em -1.6em;
vertical-align: middle;
}
/* CSS for syntax highlighting */
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { display: inline-block; text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<link rel="stylesheet" href="https://unpkg.com/reveal.js@^4//dist/theme/white.css" id="theme">
<link rel="stylesheet" href="./css/custom.css"/>
<link rel="stylesheet" href="./css/asciinema-player.css"/>
</head>
<body>
<div class="reveal">
<div class="slides">
<section id="title-slide">
<h1 class="title">Laboratorio di TNDS – Lezione 5</h1>
<p class="author">Maurizio Tomasi</p>
<p class="date">Martedì 22 Ottobre 2024</p>
</section>
<section id="esercizi-per-oggi" class="slide level1">
<h1>Esercizi per oggi</h1>
</section>
<section id="link-alle-risorse-online" class="slide level1">
<h1>Link alle risorse online</h1>
<ul>
<li><p>La spiegazione dettagliata degli esercizi si trova qui: <a
href="carminati-esercizi-05.html">carminati-esercizi-05.html</a>.</p></li>
<li><p>Come al solito, queste slides, che forniscono suggerimenti
addizionali rispetto alla lezione di teoria, sono disponibili
all’indirizzo <a
href="https://ziotom78.github.io/tnds-tomasi-notebooks/">ziotom78.github.io/tnds-tomasi-notebooks</a>.</p></li>
</ul>
</section>
<section id="esercizi-per-oggi-1" class="slide level1">
<h1>Esercizi per oggi</h1>
<ul>
<li><a href="carminati-esercizi-05.html#5.0">Esercizio 5.0</a>:
Creazione della classe <code>Posizione</code></li>
<li><a href="carminati-esercizi-05.html#5.1">Esercizio 5.1</a>:
Creazione della classe <code>Particella</code> ed
<code>Elettrone</code></li>
<li><a href="carminati-esercizi-05.html#5.2">Esercizio 5.2</a>:
Creazione delle classi <code>CampoVettoriale</code> e
<code>PuntoMateriale</code></li>
<li><a href="carminati-esercizi-05.html#5.3">Esercizio 5.3</a>: Calcolo
del campo elettrico generato da un dipolo (<strong>da
consegnare</strong>)</li>
<li><a href="carminati-esercizi-05.html#5.4">Esercizio 5.4</a>: Campo di
multipolo (approfondimento)</li>
<li><a href="carminati-esercizi-05.html#5.5">Esercizio 5.5</a>: Gravità
dallo spazio (approfondimento)</li>
</ul>
</section>
<section id="suggerimenti-per-gli-esercizi" class="slide level1">
<h1>Suggerimenti per gli esercizi</h1>
</section>
<section id="esercizio-5.0" class="slide level1">
<h1>Esercizio 5.0</h1>
<ul>
<li><p>Alcune funzioni utili disponibili in
<code><cmath></code>:</p>
<div class="sourceCode" id="cb1"><pre
class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="dt">double</span> <span class="bu">std::</span>sin<span class="op">(</span><span class="dt">double</span> x<span class="op">);</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="dt">double</span> <span class="bu">std::</span>cos<span class="op">(</span><span class="dt">double</span> x<span class="op">);</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="dt">double</span> <span class="bu">std::</span>tan<span class="op">(</span><span class="dt">double</span> x<span class="op">);</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="dt">double</span> <span class="bu">std::</span>atan<span class="op">(</span><span class="dt">double</span> x<span class="op">);</span></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="dt">double</span> <span class="bu">std::</span>atan2<span class="op">(</span><span class="dt">double</span> y<span class="op">,</span> <span class="dt">double</span> x<span class="op">);</span></span></code></pre></div></li>
<li><p>Per maggiori informazioni, eseguire <code>man</code> da
terminale</p>
<div class="sourceCode" id="cb2"><pre
class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> man atan2</span></code></pre></div></li>
<li><p>Consultate Wikipedia per comprendere la logica di <a
href="https://en.wikipedia.org/wiki/Atan2"><code>atan2</code></a>.</p></li>
</ul>
</section>
<section id="esercizio-5.0-1" class="slide level1">
<h1>Esercizio 5.0</h1>
<div class="sourceCode" id="cb3"><pre
class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="dt">bool</span> are_close<span class="op">(</span><span class="dt">double</span> calculated<span class="op">,</span> <span class="dt">double</span> expected<span class="op">,</span> <span class="dt">double</span> epsilon <span class="op">=</span> <span class="fl">1e-7</span><span class="op">)</span> <span class="op">{</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> fabs<span class="op">(</span>calculated <span class="op">-</span> expected<span class="op">)</span> <span class="op"><</span> epsilon<span class="op">;</span></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a><span class="dt">void</span> test_coordinates<span class="op">(</span><span class="dt">void</span><span class="op">)</span> <span class="op">{</span></span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a> Posizione p<span class="op">{</span><span class="dv">1</span><span class="op">,</span> <span class="dv">2</span><span class="op">,</span> <span class="dv">3</span><span class="op">};</span></span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a> <span class="ot">assert</span><span class="op">(</span>are_close<span class="op">(</span>p<span class="op">.</span>getX<span class="op">(),</span> <span class="fl">1.0</span><span class="op">));</span></span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a> <span class="ot">assert</span><span class="op">(</span>are_close<span class="op">(</span>p<span class="op">.</span>getY<span class="op">(),</span> <span class="fl">2.0</span><span class="op">);</span></span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a> <span class="ot">assert</span><span class="op">(</span>are_close<span class="op">(</span>p<span class="op">.</span>getZ<span class="op">(),</span> <span class="fl">3.0</span><span class="op">);</span></span>
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-12"><a href="#cb3-12" aria-hidden="true" tabindex="-1"></a> <span class="ot">assert</span><span class="op">(</span>are_close<span class="op">(</span>p<span class="op">.</span>getR<span class="op">(),</span> <span class="fl">3.7416573867739</span><span class="op">));</span></span>
<span id="cb3-13"><a href="#cb3-13" aria-hidden="true" tabindex="-1"></a> <span class="ot">assert</span><span class="op">(</span>are_close<span class="op">(</span>p<span class="op">.</span>getPhi<span class="op">(),</span> <span class="fl">1.1071487177941</span><span class="op">);</span></span>
<span id="cb3-14"><a href="#cb3-14" aria-hidden="true" tabindex="-1"></a> <span class="ot">assert</span><span class="op">(</span>are_close<span class="op">(</span>p<span class="op">.</span>getTheta<span class="op">(),</span> <span class="fl">0.64052231267943</span><span class="op">);</span></span>
<span id="cb3-15"><a href="#cb3-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-16"><a href="#cb3-16" aria-hidden="true" tabindex="-1"></a> <span class="ot">assert</span><span class="op">(</span>are_close<span class="op">(</span>p<span class="op">.</span>getRho<span class="op">(),</span> <span class="fl">2.2360679774998</span><span class="op">);</span></span>
<span id="cb3-17"><a href="#cb3-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-18"><a href="#cb3-18" aria-hidden="true" tabindex="-1"></a> cerr <span class="op"><<</span> <span class="st">"The coordinates work correctly! 🥳</span><span class="sc">\n</span><span class="st">"</span><span class="op">;</span></span>
<span id="cb3-19"><a href="#cb3-19" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
</section>
<section id="esercizio-5.2" class="slide level1">
<h1>Esercizio 5.2</h1>
<ul>
<li><p>L’esempio usa <code>new</code> per creare puntatori:</p>
<div class="sourceCode" id="cb4"><pre
class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>Particella a<span class="op">{</span><span class="fl">1.</span><span class="op">,</span> <span class="fl">1.6e-19</span><span class="op">};</span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a>Elettrone <span class="op">*</span>e<span class="op">{</span><span class="kw">new</span> Elettrone<span class="op">{}};</span></span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a>CorpoCeleste <span class="op">*</span>c<span class="op">{</span><span class="kw">new</span> CorpoCeleste<span class="op">{</span><span class="st">"Terra"</span><span class="op">,</span> <span class="fl">6.0e24</span><span class="op">,</span> <span class="fl">6.4e6</span><span class="op">}};</span></span></code></pre></div>
<p>Questo è utile per lo scopo dell’esercizio (comprendere come funziona
l’ereditarietà).</p></li>
<li><p>In un vero programma l’uso di <code>new</code> e
<code>delete</code> espliciti andrebbe però limitato il più possibile (e
in questi pochissimi casi, andrebbe comunque usato solamente in
costruttori/distruttori di classi, mai nel <code>main</code>).</p></li>
</ul>
</section>
<section class="slide level1">
<center>
<img data-src="images/c++-advice-no-pointers.png" style="width:40.0%" />
</center>
<p>Estratto dall’articolo <a
href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2657r0.html">C++
is the next C++</a>, che propone una nuova «modalità» di compilazione
del C++ in cui disabilitare (quasi) del tutto i puntatori.</p>
</section>
<section id="esercizio-5.3" class="slide level1">
<h1>Esercizio 5.3</h1>
<div class="sourceCode" id="cb5"><pre
class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="dt">void</span> test_coulomb_law<span class="op">(</span><span class="dt">void</span><span class="op">)</span> <span class="op">{</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> <span class="co">// 0.5 µC charge with no mass (irrelevant for the electric field)</span></span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a> PuntoMateriale particella1<span class="op">{</span><span class="fl">0.0</span><span class="op">,</span> <span class="fl">5e-7</span><span class="op">,</span> <span class="dv">5</span><span class="op">,</span> <span class="dv">3</span><span class="op">,</span> <span class="op">-</span><span class="dv">2</span><span class="op">};</span></span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> Posizione p<span class="op">{-</span><span class="dv">2</span><span class="op">,</span> <span class="dv">4</span><span class="op">,</span> <span class="dv">1</span><span class="op">};</span></span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a> CampoVettoriale V<span class="op">{</span>particella<span class="op">.</span>CampoElettrico<span class="op">(</span>p<span class="op">)};</span></span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a> <span class="ot">assert</span><span class="op">(</span>are_close<span class="op">(</span>V<span class="op">.</span>getFx<span class="op">(),</span> <span class="op">-</span><span class="fl">69.41150052142065</span><span class="op">));</span></span>
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a> <span class="ot">assert</span><span class="op">(</span>are_close<span class="op">(</span>V<span class="op">.</span>getFy<span class="op">(),</span> <span class="fl">9.915928645917235</span><span class="op">));</span></span>
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a> <span class="ot">assert</span><span class="op">(</span>are_close<span class="op">(</span>V<span class="op">.</span>getFz<span class="op">(),</span> <span class="fl">29.747785937751708</span><span class="op">));</span></span>
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-12"><a href="#cb5-12" aria-hidden="true" tabindex="-1"></a> cerr <span class="op"><<</span> <span class="st">"Coulomb's law works correctly! 🥳</span><span class="sc">\n</span><span class="st">"</span><span class="op">;</span></span>
<span id="cb5-13"><a href="#cb5-13" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span>
<span id="cb5-14"><a href="#cb5-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-15"><a href="#cb5-15" aria-hidden="true" tabindex="-1"></a><span class="dt">void</span> test_newton_law<span class="op">(</span><span class="dt">void</span><span class="op">)</span> <span class="op">{</span></span>
<span id="cb5-16"><a href="#cb5-16" aria-hidden="true" tabindex="-1"></a> <span class="co">// 10⁹ tonnes, without charge (irrelevant for the gravitational field)</span></span>
<span id="cb5-17"><a href="#cb5-17" aria-hidden="true" tabindex="-1"></a> PuntoMateriale particella1<span class="op">{</span><span class="fl">1e12</span><span class="op">,</span> <span class="dv">0</span><span class="op">,</span> <span class="dv">5</span><span class="op">,</span> <span class="dv">3</span><span class="op">,</span> <span class="op">-</span><span class="dv">2</span><span class="op">};</span></span>
<span id="cb5-18"><a href="#cb5-18" aria-hidden="true" tabindex="-1"></a> Posizione p<span class="op">{-</span><span class="dv">2</span><span class="op">,</span> <span class="dv">4</span><span class="op">,</span> <span class="dv">1</span><span class="op">};</span></span>
<span id="cb5-19"><a href="#cb5-19" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-20"><a href="#cb5-20" aria-hidden="true" tabindex="-1"></a> CampoVettoriale V<span class="op">{</span>particella<span class="op">.</span>CampoElettrico<span class="op">(</span>p<span class="op">)};</span></span>
<span id="cb5-21"><a href="#cb5-21" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-22"><a href="#cb5-22" aria-hidden="true" tabindex="-1"></a> <span class="ot">assert</span><span class="op">(</span>are_close<span class="op">(</span>V<span class="op">.</span>getFx<span class="op">(),</span> <span class="op">-</span><span class="fl">1.0302576701177</span><span class="op">));</span></span>
<span id="cb5-23"><a href="#cb5-23" aria-hidden="true" tabindex="-1"></a> <span class="ot">assert</span><span class="op">(</span>are_close<span class="op">(</span>V<span class="op">.</span>getFy<span class="op">(),</span> <span class="fl">0.14717966715968</span><span class="op">));</span></span>
<span id="cb5-24"><a href="#cb5-24" aria-hidden="true" tabindex="-1"></a> <span class="ot">assert</span><span class="op">(</span>are_close<span class="op">(</span>V<span class="op">.</span>getFz<span class="op">(),</span> <span class="fl">0.44153900147903</span><span class="op">));</span></span>
<span id="cb5-25"><a href="#cb5-25" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-26"><a href="#cb5-26" aria-hidden="true" tabindex="-1"></a> cerr <span class="op"><<</span> <span class="st">"Newton's law works correctly! 🥳</span><span class="sc">\n</span><span class="st">"</span><span class="op">;</span></span>
<span id="cb5-27"><a href="#cb5-27" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
</section>
<section id="inizializzazione-di-variabili-membro" class="slide level1">
<h1>Inizializzazione di variabili membro</h1>
</section>
<section id="inizializzazione-e-assegnamento" class="slide level1">
<h1>Inizializzazione e assegnamento</h1>
<p>Per inizializzare i membri di una classe in C++ esistono due
possibilità:</p>
<div class="sourceCode" id="cb6"><pre
class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="kw">class</span> Prova <span class="op">{</span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a><span class="kw">public</span><span class="op">:</span></span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a> Prova<span class="op">();</span></span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a><span class="kw">private</span><span class="op">:</span></span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a> <span class="dt">int</span> a<span class="op">;</span></span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a> <span class="dt">double</span> b<span class="op">;</span></span>
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a><span class="op">};</span></span>
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-9"><a href="#cb6-9" aria-hidden="true" tabindex="-1"></a>Prova<span class="op">::</span>Prova<span class="op">()</span> <span class="op">:</span> a<span class="op">{</span><span class="dv">1</span><span class="op">}</span> <span class="op">{</span> <span class="co">// Initializer: use ":" *before* the {</span></span>
<span id="cb6-10"><a href="#cb6-10" aria-hidden="true" tabindex="-1"></a> b <span class="op">=</span> <span class="fl">5.0</span><span class="op">;</span> <span class="co">// Old boring assignment</span></span>
<span id="cb6-11"><a href="#cb6-11" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
</section>
<section id="inizializzazione-e-assegnamento-1" class="slide level1">
<h1>Inizializzazione e assegnamento</h1>
<ul>
<li><p>I due metodi non sono equivalenti!</p></li>
<li><p>Costruiamo una classe <code>DaylightPeriod</code> che contiene al
suo interno due variabili dello stesso tipo <code>Time</code>, ma
inizializzate in modo diverso:</p>
<div class="sourceCode" id="cb7"><pre
class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="kw">class</span> DaylightPeriod <span class="op">{</span></span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a><span class="kw">public</span><span class="op">:</span></span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a> <span class="co">// Pass two strings as names to distinguish the two objects</span></span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a> DaylightPeriod<span class="op">()</span> <span class="op">:</span> dawn<span class="op">{</span><span class="st">"7:00"</span><span class="op">}</span> <span class="op">{</span> sunset <span class="op">=</span> Time<span class="op">(</span><span class="st">"21:00"</span><span class="op">);</span> <span class="op">}</span></span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a><span class="kw">private</span><span class="op">:</span></span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a> Time dawn<span class="op">,</span> sunset<span class="op">;</span></span>
<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a><span class="op">};</span></span></code></pre></div></li>
</ul>
</section>
<section id="inizializzazione-e-assegnamento-2" class="slide level1">
<h1>Inizializzazione e assegnamento</h1>
<p>Per capire cosa succede, definiamo <code>Time</code> in modo che
stampi a video un messaggio ogni volta che viene invocato un suo
metodo.</p>
<div class="sourceCode" id="cb8"><pre
class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="kw">class</span> Time <span class="op">{</span></span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a><span class="kw">public</span><span class="op">:</span></span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a> Time<span class="op">()</span> <span class="op">{</span> <span class="bu">std::</span>cout <span class="op"><<</span> <span class="st">"Time() called with no arguments</span><span class="sc">\n</span><span class="st">"</span><span class="op">;</span> <span class="op">}</span></span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a> Time<span class="op">(</span><span class="at">const</span> <span class="dt">char</span> <span class="op">*</span>time<span class="op">)</span> <span class="op">{</span></span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a> <span class="bu">std::</span>cout <span class="op"><<</span> <span class="bu">std::</span>format<span class="op">(</span><span class="st">"Call to Time constructor with time </span><span class="sc">\"{}\"\n</span><span class="st">"</span><span class="op">,</span> time<span class="op">);</span></span>
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span>
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a> <span class="dt">void</span> <span class="kw">operator</span><span class="op">=(</span><span class="at">const</span> Time <span class="op">&)</span> <span class="op">{</span></span>
<span id="cb8-9"><a href="#cb8-9" aria-hidden="true" tabindex="-1"></a> <span class="bu">std::</span>cout <span class="op"><<</span> <span class="st">"Call to Time::operator=</span><span class="sc">\n</span><span class="st">"</span><span class="op">;</span></span>
<span id="cb8-10"><a href="#cb8-10" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span></span>
<span id="cb8-11"><a href="#cb8-11" aria-hidden="true" tabindex="-1"></a><span class="op">};</span></span></code></pre></div>
</section>
<section id="inizializzazione-e-assegnamento-3" class="slide level1">
<h1>Inizializzazione e assegnamento</h1>
<p>Se ora creiamo nel <code>main</code> una variabile
<code>DaylightPeriod</code>, vedremo cosa accade nel costruttore:</p>
<div class="sourceCode" id="cb9"><pre
class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="dt">int</span> main<span class="op">(</span><span class="dt">void</span><span class="op">)</span> <span class="op">{</span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a> DaylightPeriod c<span class="op">{};</span></span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> <span class="dv">0</span><span class="op">;</span></span>
<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
</section>
<section id="risultato" class="slide level1">
<h1>Risultato</h1>
<ul>
<li><p>L’output del programma è il seguente:</p>
<pre><code>Call to Time constructor with time "7:00"
Time() called with no arguments
Call to Time constructor with time "21:00"
Call to Time::operator=</code></pre></li>
<li><p>Ricordiamo come abbiamo definito la classe
<code>DaylightPeriod</code>, e in particolare il costruttore:</p>
<div class="sourceCode" id="cb11"><pre
class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="kw">class</span> DaylightPeriod <span class="op">{</span></span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a><span class="kw">public</span><span class="op">:</span></span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a> DaylightPeriod<span class="op">()</span> <span class="op">:</span> dawn<span class="op">{</span><span class="st">"7:00"</span><span class="op">}</span> <span class="op">{</span> sunset <span class="op">=</span> Time<span class="op">(</span><span class="st">"21:00"</span><span class="op">);</span> <span class="op">}</span></span>
<span id="cb11-4"><a href="#cb11-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb11-5"><a href="#cb11-5" aria-hidden="true" tabindex="-1"></a><span class="kw">private</span><span class="op">:</span></span>
<span id="cb11-6"><a href="#cb11-6" aria-hidden="true" tabindex="-1"></a> Time dawn<span class="op">,</span> sunset<span class="op">;</span></span>
<span id="cb11-7"><a href="#cb11-7" aria-hidden="true" tabindex="-1"></a><span class="op">};</span></span></code></pre></div></li>
</ul>
</section>
<section id="regola-generale" class="slide level1">
<h1>Regola generale</h1>
<ul>
<li><p>Si dovrebbe evitare di inizializzare gli oggetti nel corpo del
costruttore, perché il C++ richiede che tutte le variabili membro siano
inizializzate <strong>prima</strong> che il costruttore di una classe
sia eseguito.</p></li>
<li><p>Di conseguenza, se si usano i «vecchi» assegnamenti gli oggetti
con costruttore vengono inizializzati due volte.</p></li>
<li><p>Se avete scelta, l’inizializzazione con i due punti
(<code>:</code>) è <strong>sempre</strong> da preferire:</p>
<div class="sourceCode" id="cb12"><pre
class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="kw">class</span> DaylightPeriod <span class="op">{</span></span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a><span class="kw">public</span><span class="op">:</span></span>
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a> DaylightPeriod<span class="op">()</span> <span class="op">:</span> dawn<span class="op">{</span><span class="st">"7:00"</span><span class="op">},</span> sunset<span class="op">{</span><span class="st">"21:00"</span><span class="op">}</span> <span class="op">{</span> <span class="op">}</span> <span class="co">// That's the way!</span></span>
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-5"><a href="#cb12-5" aria-hidden="true" tabindex="-1"></a> <span class="co">// ...</span></span>
<span id="cb12-6"><a href="#cb12-6" aria-hidden="true" tabindex="-1"></a><span class="op">};</span></span></code></pre></div></li>
</ul>
</section>
</div>
</div>
<script src="https://unpkg.com/reveal.js@^4//dist/reveal.js"></script>
<!-- reveal.js plugins -->
<script src="https://unpkg.com/reveal.js@^4//plugin/notes/notes.js"></script>
<script src="https://unpkg.com/reveal.js@^4//plugin/search/search.js"></script>
<script src="https://unpkg.com/reveal.js@^4//plugin/zoom/zoom.js"></script>
<script>
// Full list of configuration options available at:
// https://revealjs.com/config/
Reveal.initialize({
// Display controls in the bottom right corner
controls: true,
// Help the user learn the controls by providing hints, for example by
// bouncing the down arrow when they first encounter a vertical slide
controlsTutorial: true,
// Determines where controls appear, "edges" or "bottom-right"
controlsLayout: 'bottom-right',
// Visibility rule for backwards navigation arrows; "faded", "hidden"
// or "visible"
controlsBackArrows: 'faded',
// Display a presentation progress bar
progress: true,
// Display the page number of the current slide
slideNumber: true,
// 'all', 'print', or 'speaker'
showSlideNumber: 'all',
// Add the current slide number to the URL hash so that reloading the
// page/copying the URL will return you to the same slide
hash: true,
// Start with 1 for the hash rather than 0
hashOneBasedIndex: false,
// Flags if we should monitor the hash and change slides accordingly
respondToHashChanges: true,
// Push each slide change to the browser history
history: true,
// Enable keyboard shortcuts for navigation
keyboard: true,
// Enable the slide overview mode
overview: true,
// Disables the default reveal.js slide layout (scaling and centering)
// so that you can use custom CSS layout
disableLayout: false,
// Vertical centering of slides
center: true,
// Enables touch navigation on devices with touch input
touch: true,
// Loop the presentation
loop: false,
// Change the presentation direction to be RTL
rtl: false,
// see https://revealjs.com/vertical-slides/#navigation-mode
navigationMode: 'default',
// Randomizes the order of slides each time the presentation loads
shuffle: false,
// Turns fragments on and off globally
fragments: true,
// Flags whether to include the current fragment in the URL,
// so that reloading brings you to the same fragment position
fragmentInURL: true,
// Flags if the presentation is running in an embedded mode,
// i.e. contained within a limited portion of the screen
embedded: false,
// Flags if we should show a help overlay when the questionmark
// key is pressed
help: true,
// Flags if it should be possible to pause the presentation (blackout)
pause: true,
// Flags if speaker notes should be visible to all viewers
showNotes: false,
// Global override for autoplaying embedded media (null/true/false)
autoPlayMedia: null,
// Global override for preloading lazy-loaded iframes (null/true/false)
preloadIframes: null,
// Number of milliseconds between automatically proceeding to the
// next slide, disabled when set to 0, this value can be overwritten
// by using a data-autoslide attribute on your slides
autoSlide: 0,
// Stop auto-sliding after user input
autoSlideStoppable: true,
// Use this method for navigation when auto-sliding
autoSlideMethod: null,
// Specify the average time in seconds that you think you will spend
// presenting each slide. This is used to show a pacing timer in the
// speaker view
defaultTiming: null,
// Enable slide navigation via mouse wheel
mouseWheel: false,
// The display mode that will be used to show slides
display: 'block',
// Hide cursor if inactive
hideInactiveCursor: true,
// Time before the cursor is hidden (in ms)
hideCursorTime: 5000,
// Opens links in an iframe preview overlay
previewLinks: false,
// Transition style (none/fade/slide/convex/concave/zoom)
transition: 'slide',
// Transition speed (default/fast/slow)
transitionSpeed: 'default',
// Transition style for full page slide backgrounds
// (none/fade/slide/convex/concave/zoom)
backgroundTransition: 'fade',
// Number of slides away from the current that are visible
viewDistance: 3,
// Number of slides away from the current that are visible on mobile
// devices. It is advisable to set this to a lower number than
// viewDistance in order to save resources.
mobileViewDistance: 2,
// Parallax background image
parallaxBackgroundImage: './media/background.png', // e.g. "'https://s3.amazonaws.com/hakim-static/reveal-js/reveal-parallax-1.jpg'"
// The "normal" size of the presentation, aspect ratio will be preserved
// when the presentation is scaled to fit different resolutions. Can be
// specified using percentage units.
width: 1440,
height: 810,
// reveal.js plugins
plugins: [
RevealNotes,
RevealSearch,
RevealZoom
]
});
</script>
<script src="js/asciinema-player.js"></script>
</body>
</html>