-
Notifications
You must be signed in to change notification settings - Fork 1
/
quiz01.tex
1023 lines (823 loc) · 18.2 KB
/
quiz01.tex
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass[landscape]{slides}
\usepackage[margin=.5in]{geometry}
\usepackage{tabularx}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{listings}
\usepackage{color}
\usepackage{graphics}
\usepackage[english]{babel}
\definecolor{hellgelb}{rgb}{1,1,0.8}
\lstset{basicstyle={\tiny\ttfamily},backgroundcolor=\color{hellgelb},frame=single,numbers=left,numberstyle={\tiny\sffamily}}
%\onlyslides{0-999}
\begin{document}
%%% ch1: hello world
\begin{slide}
\begin{lstlisting}
#include<iostream>
int main()
{
std::cout << "Hello Studygroup\n";
}
\end{lstlisting}
What will happen if we try to compile, link and run this program?
Do you have any comments to the code?
\begin{note}
\begin{tiny}
\begin{verbatim}
The code will compile, link, run and print out "Hello Studygroup".
Example from ch 1, page 5.
This is actually the first piece of code in the TC++PL book. Some will claim
that the missing space after include is unusual. Others will claim that a
a missing return statement is an error, but it is perfectly legal C++ and
surprisingly common.
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%% ch2: for loop, post and pre increment
\begin{slide}
\begin{lstlisting}
#include <iostream>
int main() {
for (int i=0; i<3; i++)
std::cout << i;
for (int j=1; j<=3; ++j)
std::cout << j;
return 0;
}
\end{lstlisting}
What will this code print out?
\begin{note}
\begin{tiny}
\begin{verbatim}
It will print out 012123
ch2p26
What is the difference between postincrement and preincrement?
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%% for and while equivalence
\begin{slide}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lr}
\begin{minipage}[t]{.45\linewidth}
\tiny{foo.cpp}
\begin{lstlisting}
int main() {
int r = 42;
for (int i=1; i<8; ++i)
r += i;
return r;
}
\end{lstlisting}
\end{minipage}
&
\begin{minipage}[t]{.45\linewidth}
\tiny{bar.cpp}
\begin{lstlisting}
int main() {
int r = 42;
int i=1;
while (i<8) {
r += i;
++i;
}
return r;
}
\end{lstlisting}
\end{minipage}
\end{tabular*}
Both these programs will return 70. But do you expect the resulting binary executable to be exactly the same? Eg,
\begin{verbatim}
g++ -S foo.cpp
g++ -S bar.cpp
diff foo.s bar.s
\end{verbatim}
\begin{note}
\begin{tiny}
\begin{verbatim}
There is a simple mapping from a for-loop to a while-loop. Eg,
for ( for-init-statement condition_opt ; expression_opt ) statement
for ( int i=1; i<4; ++i ) { std::cout << i; }
to
{ while-init-statement; while (condition) statement }
{ int i=1; while (i<4) { std::cout << i; ++i; } }
So if we write the while-loop inside a block in bar.cpp, then
foo.cpp and bar.cpp should produce exactly the same code.
Note that the scoping rules for variables have changed from
classic C++ to modern C++. Eg,
int a;
for(int i=0; i<4; i++)
;
a = i;
does not compile with C++98 because the scope of i is only
inside the for-loop, just as if:
int a;
{
for(int i=0; i<4; i++)
;
}
a = i; // error
and i is not declared outside the for-loop.
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%% ch2: modular programming
\begin{slide}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lr}
\begin{minipage}[t]{.32\linewidth}
\tiny{stack.h}
\begin{lstlisting}
namespace Stack {
void push(char);
char pop();
class Overflow {};
class Underflow {};
}
\end{lstlisting}
\tiny{main.cpp}
\begin{lstlisting}
#include "stack.h"
int main() {
Stack::push('H');
Stack::push('e');
Stack::push('i');
Stack::pop();
Stack::pop();
Stack::push('e');
Stack::push('l');
Stack::push('l');
Stack::push('o');
return 0;
}
\end{lstlisting}
\end{minipage}
&
\begin{minipage}[t]{.6\linewidth}
\tiny{stack.cpp}
\begin{lstlisting}
#include <iostream>
#include "stack.h"
namespace Stack {
const int max_size = 4;
char v[max_size];
int top = 0;
}
void Stack::push(char c) {
if ( top == max_size )
throw Overflow();
std::cerr << "push: " << c << std::endl;
v[top++] = c;
}
char Stack::pop() {
if ( top == 0 )
throw Underflow();
char c = v[--top];
std::cerr << "pop: " << c << std::endl;
return c;
}
\end{lstlisting}
\end{minipage}
\end{tabular*}
\
\begin{lstlisting}[numbers=none]
g++ -Wall main.cpp stack.cpp && ./a.out
\end{lstlisting}
What will happen if you try to compile, link and run this code?
Please comment this code.
\begin{note}
\begin{tiny}
\begin{verbatim}
g++ -Wall main.cpp stack.cpp && ./a.out
push: H
push: e
push: i
pop: i
pop: e
push: e
push: l
push: l
terminate called after throwing an instance of 'Stack::Overflow'
Naming conventions. Stroustrup and others seems to prefer
names starting with a capital letter for classes and namespaces.
Functions and variables starts with lower letter. .h for
header files, and .cpp for C++ files are quite common.
Notice the order of include statements in stack.cpp and
main.cpp. At least in the stack.cpp it is important that
we include "stack.h" first, in order to avoid undetected
dependencies to iostream.
Notice the similarities between namespaces and classes.
According to Bjarne (p32): Where there is no need for more
than one object of a type, the data-hiding programming
style using modules suffices.
ch2p26-29
Creating singletons this way is not necessarily a good
idea. Even if you might have just one object of a type
in your runtime system, it might be useful to be able
to create several objects during development and testing.
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%% ch2: concrete stack class
\begin{slide}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lr}
\begin{minipage}[t]{.32\linewidth}
\tiny{stack.h}
\begin{lstlisting}
class Stack {
char * v;
int top;
int max_size;
public:
class Underflow {};
class Overflow {};
class Bad_size {};
Stack(int s);
~Stack();
void push(char c);
char pop();
};
\end{lstlisting}
\tiny{main.cpp}
\begin{lstlisting}
#include "stack.h"
int main() {
Stack s(4);
s.push('H');
s.push('e');
s.push('i');
s.pop();
s.pop();
s.push('e');
s.push('l');
s.push('l');
s.push('o');
return 0;
}
\end{lstlisting}
\end{minipage}
&
\begin{minipage}[t]{.6\linewidth}
\tiny{stack.cpp}
\begin{lstlisting}
#include "stack.h"
#include <iostream>
Stack::Stack(int s) {
top = 0;
if ( s < 0 || s > 16 )
throw Bad_size();
max_size = s;
v = new char[s];
}
Stack::~Stack() {
delete v;
}
void Stack::push(char c) {
if ( top == max_size )
throw Overflow();
std::cerr << "push: " << c << std::endl;
v[top++] = c;
}
char Stack::pop() {
if ( top == 0 )
throw Underflow();
char c = v[--top];
std::cerr << "pop: " << c << std::endl;
return c;
}
\end{lstlisting}
What will happen if you try to compile, link and run this code?
Please comment this code.
\end{minipage}
\end{tabular*}
\begin{note}
\begin{tiny}
\begin{verbatim}
g++ -Wall main.cpp stack.cpp && ./a.out
push: H
push: e
push: i
pop: i
pop: e
push: e
push: l
push: l
terminate called after throwing an instance of 'Stack::Overflow'
There is a lot of issues to discuss in this code. Eg,
- if you new an array, you must delete an array
- if your class needs a destructor, you probably also need
implement or hide the copy constructor and assignment operator.
- initializing members should be done in an initializer list
- the size of the stack should probably be given as size_t
ch2p26-29
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%% ch2*, class size
\begin{slide}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lr}
\begin{minipage}[c]{.6\linewidth}
\begin{lstlisting}
#include <iostream>
class U {
int a;
};
class V {
int a;
int b;
};
class W {
int a;
int b;
void foo() { a = 3; }
};
class X {
int a;
int b;
void foo() { a = 3; }
void bar() { b = 4; }
};
class Y { };
class Z {
void foo() {}
};
int main() {
std::cout << sizeof(U) << std::endl;
std::cout << sizeof(V) << std::endl;
std::cout << sizeof(W) << std::endl;
std::cout << sizeof(X) << std::endl;
std::cout << sizeof(Y) << std::endl;
std::cout << sizeof(Z) << std::endl;
}
\end{lstlisting}
\end{minipage}
&
\begin{minipage}{.3\linewidth}
What will this code print out when executed?
\end{minipage}
\end{tabular*}
\begin{note}
\begin{tiny}
\begin{verbatim}
This is of course compiler and processor dependant.
On my machine (MacBook Pro, Intel Core Duo, OS X 10.4, gcc 4.0.1) I get:
g++ -Wall scratch.cpp && ./a.out
4
8
8
8
1
1
Why do you think that the size of an empty class is 1? To avoid
having two objects with the same address, eg:
Y y1;
Y y2;
assert( &y1 != &y2 );
as described in:
http://www.research.att.com/~bs/bs_faq2.html#sizeof-empty
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%% ch2, size of derived objects
\begin{slide}
\begin{lstlisting}
#include <iostream>
class U {
int a;
int b;
};
class V : public U {
int c;
};
class W : public V {
};
class X : public W {
int d;
};
class Y {};
class Z : public Y {
int e;
};
int main() {
std::cout << sizeof(U) << std::endl;
std::cout << sizeof(V) << std::endl;
std::cout << sizeof(W) << std::endl;
std::cout << sizeof(X) << std::endl;
std::cout << sizeof(Y) << std::endl;
std::cout << sizeof(Z) << std::endl;
}
\end{lstlisting}
What will this code print out when executed?
\begin{note}
\begin{tiny}
\begin{verbatim}
This is of course compiler and processor dependant.
On my machine (MacBook Pro, Intel Core Duo, OS X 10.4, gcc 4.0.1) I get:
g++ -Wall scratch.cpp && ./a.out
8
12
12
16
1
4
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%% ch2,
\begin{slide}
\begin{lstlisting}
#include <iostream>
class A {
public:
void foo() { std::cout << "A"; }
};
class B : public A {
public:
void foo() { std::cout << "B"; }
};
class C : public A {
public:
void foo() { std::cout << "C"; }
};
class D : public A {
};
void bar(A & a) {
a.foo();
}
int main() {
B b;
C c;
bar(b);
bar(c);
}
\end{lstlisting}
What will happen when you compile, link and execute this code?
\begin{note}
\begin{tiny}
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
AA
How can you get this code to print out BC? Add virtual to line 5.
If foo in A is virtual, how can you get this code to print out
AA by just removing one character? rewrite to void(bar A a)
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%% ch2*, class size and vtables
\begin{slide}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lr}
\begin{minipage}[c]{.6\linewidth}
\begin{lstlisting}
#include <iostream>
class U {
int a;
int b;
};
class V {
int a;
int b;
void foo() { a = 2; }
void bar() { b = 3; }
};
class W {
int a;
int b;
virtual void foo() { a = 2; }
virtual void bar() { b = 3; }
};
class X {
int a;
int b;
virtual void foo() { a = 2; }
};
int main() {
std::cout << sizeof(U) << std::endl;
std::cout << sizeof(V) << std::endl;
std::cout << sizeof(W) << std::endl;
std::cout << sizeof(X) << std::endl;
}
\end{lstlisting}
\end{minipage}
&
\begin{minipage}{.3\linewidth}
What will this code print out when executed?
\end{minipage}
\end{tabular*}
\begin{note}
\begin{tiny}
\begin{verbatim}
This is of course compiler and processor dependant.
On my machine (MacBook Pro, Intel Core Duo, OS X 10.4, gcc 4.0.1) I get:
g++ -Wall scratch.cpp && ./a.out
scratch.cpp:15: warning: 'class W' has virtual functions but non-virtual destructor
scratch.cpp:22: warning: 'class X' has virtual functions but non-virtual destructor
8
8
12
12
without -Wall, no warning is produced.
Ch2p36-37. "A common implementation technique is for the compiler to convert the name
of a virtual function into an index into a table of pointers to functions. That is
usually called ``a virtual function table'' or simply, a vtbl. Each class with
virtual functions has its own vtbl identifying its virtual functions. ... Its space
overhead is one pointer on each object of a class with virtual functions plus one vtbl
for each such class."
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%% ch3, switch statement
\begin{slide}
\begin{lstlisting}
#include <iostream>
int main()
{
char ch = 'c';
std::cout << '3';
switch(ch) {
case 'a':
std::cout << 'a';
case 'b':
std::cout << 'a';
case 'c':
std::cout << 'c';
case 'd':
std::cout << 'd';
case 'e':
std::cout << 'e';
default:
std::cout << 'x';
}
std::cout << '4';
return 0;
}
\end{lstlisting}
What will this code print out?
\begin{note}
\begin{tiny}
\begin{verbatim}
g++ -Wall foo.cpp && ./a.out
3cdex4
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%% ch3, placement of default
\begin{slide}
\begin{lstlisting}
#include <iostream>
int main()
{
char ch = 'r';
std::cout << '3';
switch(ch) {
case 'a':
std::cout << 'a';
default:
std::cout << 'x';
case 'b':
std::cout << 'a';
case 'c':
std::cout << 'c';
}
std::cout << '4';
return 0;
}
\end{lstlisting}
What will happen when we try to compile, link and run this code?
\begin{note}
\begin{tiny}
\begin{verbatim}
g++ -Wall foo.cpp && ./a.out
3xac4
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%% ch3, defualt
\begin{slide}
\begin{lstlisting}
#include <iostream>
int main()
{
char ch = 'r';
std::cout << '3';
switch(ch) {
case 'a':
std::cout << 'a';
break;
case 'b':
std::cout << 'a';
break;
case 'c':
std::cout << 'c';
break;
defualt:
std::cout << 'x';
break;
}
std::cout << '4';
return 0;
}
\end{lstlisting}
What will this code print out when executed?
\begin{note}
\begin{tiny}
\begin{verbatim}
g++ -Wall foo.cpp && ./a.out
foo.cpp: In function 'int main()':
foo.cpp:17: warning: label 'defualt' defined but not used
34
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%% ch3, list
\begin{slide}
\begin{lstlisting}
#include <iostream>
#include <list>
int main() {
std::list<int> mylist;
mylist.push_front(1);
mylist.push_front(2);
mylist.push_back(3);
mylist.push_back(4);
for (std::list<int>::iterator i = mylist.begin();
i != mylist.end(); ++i) {
std::cout << *i;
}
return 0;
}
\end{lstlisting}
What will this code print out when executed?
\begin{note}
\begin{tiny}
\begin{verbatim}
g++ -Wall foo.cpp && ./a.out
2134
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%% ch3, vector, size_type, range checking
\begin{slide}
\begin{lstlisting}
#include <iostream>
#include <vector>
int main() {
std::vector<char> v;
v.push_back('a');
v.push_back('b');
v.push_back('c');
v.push_back('d');
for (int i=0; i<v.size(); ++i) {
std::cout << v[i];
}
std::cout << std::endl;
for (std::vector<char>::size_type i=0; i<v.size(); ++i) {
std::cout << v.at(i);
}
std::cout << std::endl;
return 0;
}
\end{lstlisting}
Please comment this code.
\begin{note}
\begin{tiny}
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
scratch.cpp: In function 'int main()':
scratch.cpp:11: warning: comparison between signed and unsigned integer expressions
abcd
abcd
The first for-loop is bad because it uses the wrong type in the loop.
The size of a vector, or any STL container, is not int, it is the
size_type of the container, eg std::vector<char>::size_type.
The second for-loop is "bad" because it does it calls v.size()
several times, and also by using at() you get double range-checking.
TC++PLp53
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%% map, sorted, const argument, algorithms
\begin{slide}
\begin{lstlisting}
#include <iostream>
#include <map>
#include <string>
void print(std::pair<const std::string, int> & r) {
std::cout << r.first << '/' << r.second << std::endl;
}
int main() {
std::map<std::string, int> m;
m["A"] = 5;
m["E"] = 8;
m["B"] = 9;
m["C"] = 3;
for_each(m.begin(), m.end(), print);
return 0;
}
\end{lstlisting}
Please comment this code.
\begin{note}
\begin{tiny}
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
A/5
B/9
C/3
E/8
A map keeps it element ordered so an iteration traverses the
map in (increasing) order. [p62]
for_each is defined in <algorithm> so it should really
be in the include list, even if it is often imported by
other STL headers. Actually, MS C++ compiler (version 14)
complains about missing <algorithm>
Notice that print() does not change anything i r, so the
argument is better written as const, eg
void print(const std::pair<const std::string, int> & r) {
std::cout << r.first << '/' << r.second << std::endl;
}
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%%
\begin{slide}
\begin{lstlisting}
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
bool gt_5(const std::pair<const std::string, int> & r) {
return r.second > 5;
}
int main() {
std::map<std::string, int> m;
m["A"] = 5;
m["E"] = 8;
m["B"] = 9;
m["C"] = 3;
int c = count_if(m.begin(), m.end(), gt_5);
std::cout << c << std::endl;
return 0;
}
\end{lstlisting}
What will the following print out?
The third argument to \verb!count_if! is called a predicate. Can you think of other ways of writing such predicates?
\begin{note}
\begin{tiny}
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
2
[p63]
gt_5() is a predicate. There are other ways you can write such
predicates. You might write a functor. Eg,
struct gt {
int value_;
gt(int value) : value_(value) {}
bool operator()(const std::pair<const std::string, int> & r) {
return r.second > value_;
}
};
and then use
int c = count_if(m.begin(), m.end(), gt(5));
or you can make a template version. Eg,
template <int T> bool gt(const std::pair<const std::string, int> & r) {
return r.second > T;
}
and then write:
int c = count_if(m.begin(), m.end(), gt<5>);
Using for example Boost, you can probably also make a lambda version.
\end{verbatim}
\end{tiny}
\end{note}