forked from StevensDeptECE/CPE553-CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlanguagesummaryquiz.cc
649 lines (569 loc) · 15.6 KB
/
languagesummaryquiz.cc
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
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstring>
#include <string>
#include <regex>
#include <random>
using namespace std;
/*
This language checklist serves to let you know what you do and do
not know about C++. If you can predict what each of these functions
does, that is a significant measure of your understanding of what
C++ does, though it does not guarantee that you are able to create
sequences of operations to do what you want, which is the other part
of programming.
Any function that covers a topic NOT required by the course is
labelled as such. It may be a good idea to extend your knowledge
before going for an interview at a high-powered C++ job, but it is
not required for the final.
*/
void subject(const char name[]) {
cout << "\n\n========================\n" << name << '\n';
}
void integeroperations() {
subject("Integer Operations");
int a = 2 / 3; // integer division
int b = 5 % 4 + 4 % 5 + 3 % 4 + 4 + 3;
int c = 9 / 4;
int d = a > 0 ? 6 : 4; // ternary operator
double e = 1 / 2;
cout << a << '\t'
<< b << '\t'
<< c << '\t'
<< d << '\t'
<< e << '\n';
}
void operatorprecedence() {
subject("Operator Precedence");
int a = 2 + 3 * 4;
int b = 2 / 3 * 3;
int c = (int)1.5 * 3;
int d = 1 << 2 & 1 << 3;
cout << a << '\t'
<< b << '\t'
<< c << '\t'
<< d << '\n';
}
void equaloperatorprecedence() {
subject("Equal Operator Precedence");
int a = 3;
int b = 4;
int c = 5;
a += b *= c -= 1;
cout << a << '\t'
<< b << '\t'
<< c << '\n';
a = b = c = 1;
a *= 3 + 1;
cout << a << '\t'
<< b << '\t'
<< c << '\n';
}
void overflow() {
subject("Overflow");
short a = 32767;
a++;
short b = -32767;
b -= 3;
int c = 1;
for (int i = 0; i < 35; i++)
c <<= 1;
cout << a << '\t' << b << '\t' << c << '\n';
}
void typepromotion() {
subject("Type Promotion");
float a = 1.5f * 3;
double b = 1 * 3;
int c = 1.5f * 3;
cout << a << '\t' << b << '\t' << c << '\n';
}
void roundoff() {
subject("Roundoff");
double x = sqrt(2);
bool a = x*x == 2;
bool b = x == x;
cout << a << '\t' << b << '\n';
}
void nan() {
subject("Inf and NaN");
double a = 1.0 / 0.0;
double b = -1.0 / 0.0;
double c = 2.0 * a;
double d = a + b;
double e = 0.0 / 0.0;
double f = sqrt(a);
double g = sin(a);
cout << a << '\t'
<< b << '\t'
<< c << '\t'
<< d << '\t'
<< e << '\t'
<< f << '\t'
<< g << '\n';
cout << (e == e) << '\t' << (e != e) << '\t' << '\n';
}
void ifstatements() {
subject("if Statements");
int a = 1, b = 2;
if (a < 2)
cout << "A is less than 2\n";
if (a < 1)
cout << "A is less than 1\n";
else
cout << "A is NOT less than 1\n";
if (a < b) {
cout << "A is less than B\n";
} else {
cout << "A is NOT less than B\n";
}
if (a < b)
if (a < 1)
cout << "a < b and a < 1\n";
else
cout << "a < b and a is NOT < 1\n";
else
if (a < 1)
cout << "a NOT < b and a < 1\n";
else
cout << "a NOT < b and a is NOT < 1\n";
if (a < b) {
if (a < 1)
cout << "a < b and a < 1\n";
else
cout << "a < b and a is NOT < 1\n";
cout << "this prints if a < b\n";
}
}
void statements() {
subject("Statements");
int i = 10;
while (i < 10) {
cout << 'x';
}
cout << '\n';
do {
cout << 'y';
} while (i < 10);
cout << '\n';
for (int i = 2; i < 12; i <<= 1)
cout << i;
cout << '\n';
for (int i = 1; i < 16; i += 3) {
if (i % 8 == 0)
break;
cout << i;
}
cout << '\n';
for (int i = 0; i < 20; i += 4) {
if (i % 16 == 12)
break;
if (i % 2 == 0)
i++;
cout << i;
}
cout << '\n';
for (int i = 0; i < 10; i++) {
if (i % 3 == 0)
continue;
cout << i;
}
for (int i = 0; i < 5; i++)
switch(i) {
case 0: cout << "zero";
case 1: cout << "one";
case 2: cout << "two";
case 3: cout << "three";
case 4: cout << "four";
default: cout << "default";
}
cout << '\n';
for (int i = 0; i < 5; i++)
switch(i) {
case 0:
cout << "zero";
break;
case 1:
cout << "one";
break;
case 2:
cout << "two";
break;
case 3:
cout << "three";
break;
case 4:
cout << "four";
break;
default:
cout << "default";
break;
}
cout << '\n';
// you should know the difference when not declaring the variable in the for loop
int x;
for (x = 0; x < 10; x++)
cout << x << ' ';
cout << '\n';
cout << x << '\n'; // this would not be possible if x were declared in the loop
}
void thedreadedgoto(int n) {
subject("The goto statement");
// obviously, goto can be misused to create spaghetti code
// There is only one decent use of goto in C++, shown below
int countPrimes = 1; // special case for 2
for (int i = 3; i <= n; i += 2) { // only odd numbers
int lim = sqrt(i);
for (int j = 3; j <= lim; j += 2)
if (i % j == 0)
goto notPrime; // goto in C++ is useful to jump out of loops
countPrimes++; // if all divisors tested, must be prime!
notPrime: // jump here to skip the prime count and keep going
;
}
cout << "There are " << countPrimes << " primes up to " << n << '\n';
}
int x = 1;
namespace stevens {
int x = 2;
}
void scopeandlifetime() {
subject("Scope and Lifetime");
int x = 3;
cout << x++ << stevens::x++ << ::x++ << '\n';
for (int i = 0; i < 2; i++) {
int x = 0;
static int y = 0;
cout << x++ << y++ << stevens::x++ << ::x++ << '\n';
}
}
void openingfiles() {
subject("File I/O");
{
ofstream f("test.dat");
f << "write to this file\n";
f << 2 + 3 << '\n';
}
// at the end of the block, the destructor will fire
// the file is automatically closed.
ifstream f("test.dat");
string s;
f >> s; // reads in one word "write";
getline(f, s); // read in the entire line "to this file"
cout << s << '\n';
int v;
f >> v;
cout << v + 1 << '\n';
}
void oldcstrings() {
subject("Old (C) Strings");
const char* s = "this is a constant string";
// char* s2 = "this would not work, do you know why?";
char s2[80] = "abc";
strcat(s2, "def");
cout << s2 << " length = " << strlen(s2) << '\n';
// length of c strings is 1 longer then the length because they end with '\0'
strcpy(s2, "xyz\n");
/*
you should know old fashioned printf. In many ways better than new C++ stuff
but not compile-time safe, uses runtime checking of the format string
*/
printf("%d %5.2lf, %.20lf %s\n", 52, 123.3456, 123.1234, "testing testing");
//most important thing is to remember the extra '\0', huge source of trouble
int v = atoi("283");
cout << v << '\n';
double d = strtod("1.234", nullptr);
cout << d << '\n';
}
void cppstrings() {
subject("New (C++) Strings");
string a = "abc";
a += "def"; // concatenation of strings
cout << a << '\n';
cout << "substring:" << a.substr(1) << '\n'; // starting with position 1
cout << "substring:" << a.substr(1,2) << '\n'; // starting with 1, for length 2
cout << "length:" << a.length() << '\n';
}
void identifiers() {
subject("Rules for Identifiers (names of entities)");
int thisIsALongVariableName = 1; // identifiers are names
int abc123 = 2;
int _donotusenames_startingwith_underscore = 3; // used by the compiler
int __DONOT_USE = 4; // do not use names starting with two underscores, reserved to the standard
const int CONSTANTS = 5; // C and C++ convention: constants are all caps
// const variables have storage
constexpr int BLACK = 0, WHITE = 1; // new C++11 way of specifying constants that have no storage, so better potential for optimization
class A {}; // class names are identifiers
void f(); // so are function prototypes
// void g() { cout << "hello"; } so are functions but I can't write one here
// functions may not be declared within functions
}
namespace A {
namespace B {
class C {};
}
};
// unlike anything else, namespaces may be declared multiple times.
// each one adds to the definition of the namespace.
// in this case, class D is added to namespace A::B
namespace A {
namespace B {
class D {};
}
};
void namespaces() {
subject("namespaces");
A::B::C c1; // namespaces allow partitioning namespace to allow the same name to be used for multiple classes
A::B::D d1;
}
void sizeofclasses() {
subject("Size of classes (with bit fields)");
class A{}; // minimum size of an object is 1, cannot be zero or two objects
// could have the same address.
// many c++ compilers would make this align with word width, for 64-bit computer=8
cout << "Sizeof an empty class = " << sizeof(A) << '\n';
class B {
int a;
};
/*
for an object containing 4-byte quantities, for speed C++ compilers
will usually align to 4-byte boundaries (at least)
(if this object started at location 7, reading it would require two
memory reads)
*/
cout << sizeof(B) << '\n';
class C {
double a;
int b;
};
/*
for an object containing an 8-byte entity like a double, it is faster to align
to 8 bytes. So even though this object only requires 12 bytes
the compiler will take an extra 4 so that the result is a multiple of 8
In this way, an array of these objects will have each element align to an 8-byte
boundary for speed
*/
cout << sizeof(C) << '\n';
class D1 {
int a;
double b;
char c;
double d;
short e;
};
cout << "sizeof(D1)=" << sizeof(D1) << '\n';
class D2 {
double b;
double d;
char c;
int a;
short e;
};
// grouping fields to pack better is a big win
cout << "sizeof(D2)=" << sizeof(D2) << '\n';
class Bits {
int a:2; // bit field: 2 bits (1 for sign, -2 .. +1)
int b:3; // bit field: 3 bits (1 for sign, -4 .. +3)
unsigned int c:5; // bit field: 5 bits (0 .. 31
public:
Bits(int a, int b, int c) : a(a), b(b), c(c) {}
};
Bits bits(-1, -3, 28);
Bits bits2 = {-1,-3,28}; // this only works in C++11 or better
cout << "sizeof(Bits)=" << sizeof(Bits) << '\n';
}
void passArrayParameters(int a[], int n) {
cout << "Sizeof array parameters is always = sizeof a pointer: " << sizeof(a) << '\n';
cout << "The size of this array = " << n * sizeof(int) << '\n';
a[2] = 9;
for (int i = 0; i < n; i++)
cout << a[i] << ' ';
cout << '\n';
}
void arrays() {
subject("Arrays");
int a[3] = {1, 2};
passArrayParameters(a, 3);
// size of an array parameter is the size of the pointer (8 bytes)
cout << "sizeof array parameter is useless! = " << sizeof(a) << '\n';
// always pass the size of the array manually with the array or make an object
int b[4];
cout << b[0] << ' ' << b[1] << ' ' << b[2] << ' ' << b[3] << '\n';
// arrays are initialized with whatever is on the stack, random garbage!
// to initialize
int c[4] = {6, 5, 4}; // any remaining elements are initialized to zero
for (int i = 0; i < 4; i++)
cout << c[i] << ' ';
cout << '\n';
// declaration without size is possible if initialized
int d[] = {7, 1, 2, 3};
// note the loop: sizeof(d)/sizeof(int) computes the number of elements
for (int i = 0; i < sizeof(d)/sizeof(int); i++)
cout << d[i] << ' ';
cout << '\n';
}
void func1(const char x[]) { // x is a readonly pointer to a, but cannot change, const char* const x;
cout << x[2]; // array lookup
cout << *(x+2); // pointer notation
}
void func2(const char* x) { // x is a pointer to a, cannot change a, but can point somewhere else
char foo[] = "test";
x = foo;
cout << x[2]; // array lookup
cout << *(x+2); // pointer notation
}
void func3(const char* const x) { // exactly the same as func1
cout << x[2]; // array lookup
cout << *(x+2); // pointer notation
}
void arraypointerequivalence() {
char a[] = "hello";
func1(a);
func2(a);
func3(a);
}
void bitoperations() {
subject("Bit operations");
int a = 1 << 3; // 1 shifted left by 3, equivalent to 1 * 2 to the power 3 (8)
int b = 48 >> 3; // 48 = 110000 in binary. shift right by 3 = 1100 = 12
// same as / 8
int c = 0xF3AB & 0x2C39; // 1111 0011 1010 1011 BITWISE AND 0010 1100 0011 1001
int d = 0xF3AB | 0x2C39; // bitwise OR
int e = 0xF3AB ^ 0x2C39; // bitwise XOR
int f = ~0xEDCAF3B2; // NOT
int g = 0777; // leading zero is OCTAL (base 8), not decimal!!!
cout << hex
<< a << ' '
<< b << ' '
<< c << ' '
<< d << ' '
<< e << ' '
<< f << '\n';
}
void regexexamples() {
subject("regex");
// this is obviously a huge topic not covered here. this is a checklist
// if you don't know regex, go back and learn it
string s("this subject has a submarine as a subsequence");
smatch m;
regex e ("\\b(sub)([^ ]*)"); // matches words beginning by "sub"
cout << "Target sequence: " << s << '\n';
while (regex_search (s,m,e)) {
for (auto x:m)
cout << x << " ";
cout << '\n';
s = m.suffix().str();
}
}
void randomnumbergen() {
subject("Random Number generators");
// include <random>
default_random_engine generator;
uniform_int_distribution<int> distribution(1,6);
int diceRoll = distribution(generator); // generates number in the range 1..6
cout << diceRoll << '\n';
normal_distribution<double> d2(5.0,2.0); // mean 5, variance 2
for (int i=0; i < 10; ++i) {
cout << d2(generator) << ' ';
}
cout << '\n';
}
void inheritance() {
subject("inheritance");
// this is not something you test at runtime. Included here for your checklist
// know what each of these means
class A {};
class B : public A {};
// private A --> invisible in B
// protected A --> protected in B
// public A --> public B
class C : private A {};
// private A --> invisible in B
// protected and public A --> private in B
// "this is just for me" instead use: containment
class Calternate {
private:
A a_internal;
};
class D : protected A {};
// private A --> invisible in B
// protected and public A --> private in B
// "this is just for me and my children" instead use: containment
class Dalternate {
protected:
A a_internal;
};
/*
virtual inheritance is a fix for a problem involving multiple inheritance
*/
class Employee {
private:
string ssn;
};
class TempEmployee : public virtual Employee {
};
class Secretary : public virtual Employee {
};
/* were it not for virtual inheritance, this wacky construction would fail.
Really the problem is with multiple inheritance.
You should only inherit from one object, and promise to implement certain methods.
*/
class TempSecretary : public TempEmployee, public Secretary {
};
};
void polymorphism() {
}
void templatefunctions() {
}
void templateclasses() {
}
void varargs() {
subject("var args");
// varargs are not typesafe. You are just getting data off the stack
printf("%d %d", 2);
}
/*
The syntax continues to get gnarlier...
*/
template<typename T>
T adder(T v) {
return v;
}
template<typename T, typename... Args>
T adder(T first, Args... args) {
return first + adder(args...);
}
//NOT needed for the course
void variadictemplates() {
subject("Variadic Templates");
//variadic templates are safe. unlike varargs
cout << adder(2,3) << '\n';
cout << adder(2,3,4) << '\n';
cout << adder(2,3,4.2) << '\n';
}
int main() {
integeroperations();
operatorprecedence();
equaloperatorprecedence();
overflow();
roundoff();
nan();
ifstatements();
statements();
thedreadedgoto(100);
scopeandlifetime();
openingfiles();
oldcstrings();
cppstrings();
namespaces();
sizeofclasses();
bitoperations();
regexexamples();
randomnumbergen();
inheritance();
polymorphism();
varargs();
templatefunctions();
templateclasses();
variadictemplates();
}