-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchapter_08_strings.cpp
More file actions
458 lines (330 loc) · 11.5 KB
/
chapter_08_strings.cpp
File metadata and controls
458 lines (330 loc) · 11.5 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
/*=============================================================================
* CHAPTER 8: STRINGS
* =============================================================================
*
* C++ Development Course - Consolidated Examples
* Original Author: Faranak Rajabi
*
* This file contains 34 code example(s) from Chapter 8.
*
* USAGE:
* - Review each example section
* - Uncomment the code you want to test
* - Compile: g++ -std=c++17 -Wall thisfile.cpp -o program
* - Run: ./program
*
* NOTE:
* Some examples are code snippets designed to illustrate specific
* concepts and may need additional context to compile.
*
=============================================================================*/
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
// ============================================================================
// EXAMPLE 1: Function: main
// ============================================================================
int main () {
unsigned
unsigned
unsigned
unsigned
short us ;
int ui ;
long ul ;
long long ull ;
return 0;
// ============================================================================
// EXAMPLE 2: Least types - minimum guaranteed size
// ============================================================================
int main () {
// Least types - minimum guaranteed size
std :: cout << " Least types ( bits ) :\ n " ;
std :: cout << sizeof ( std :: int_least8_t ) * 8 << ’\ n ’;
std :: cout << sizeof ( std :: int_least16_t ) * 8 << ’\ n ’;
std :: cout << sizeof ( std :: int_least32_t ) * 8 << ’\ n ’;
// ============================================================================
// EXAMPLE 3: Function: main
// ============================================================================
int main () {
std :: cout << " Enter a number : \ n " ;
int x {};
std :: cin >> x ;
// ============================================================================
// EXAMPLE 4: Function: main
// ============================================================================
int main () {
std :: cout << " Enter a number : \ n " ;
int x {};
std :: cin >> x ;
// ============================================================================
// EXAMPLE 5: Function: main
// ============================================================================
int main () {
std :: cout << " Enter a number : \ n " ;
int x {};
std :: cin >> x ;
// ============================================================================
// EXAMPLE 6: Function: main
// ============================================================================
int main () {
std :: cout << " Enter your name : " ;
std :: string name {};
std :: cin >> name ;
// ============================================================================
// EXAMPLE 7: Function: main
// ============================================================================
int main () {
std :: cout << " Enter your name : " ;
std :: string name {};
std :: getline ( std :: cin >> std :: ws , name ) ;
// ============================================================================
// EXAMPLE 8: Function: main
// ============================================================================
int main () {
int x {5}; // This creates a variable x
std :: string s { " Hello , world ! " }; // This creates a copy of the literal
std :: cout << s << " \ n " ;
std :: cout << x << " \ n " ;
return 0;
// ============================================================================
// EXAMPLE 9: Code Example
// ============================================================================
/*
std :: cout << large << " \ n " ;
return 0;
*/
// ============================================================================
// EXAMPLE 10: Prints 3
// ============================================================================
int main () {
doSomething () ;
// Prints 3
std :: cout << g_mode << ’\ n ’; // Also prints 3
// ============================================================================
// EXAMPLE 11: Function: main
// ============================================================================
int main () {
while ( true ) {
std :: cout << " Loop again ( y / n ) ? " ;
char c {};
std :: cin >> c ;
// ============================================================================
// EXAMPLE 12: Code Example
// ============================================================================
/*
std :: cout << " You selected : " << selection << ’\ n ’;
return 0;
*/
// ============================================================================
// EXAMPLE 13: Class: Rectangle
// ============================================================================
/*
class Rectangle {
private :
int width ;
int height ;
int * data ; // Dynamic memory
*/
// ============================================================================
// EXAMPLE 14: Code Example
// ============================================================================
/*
Rectangle ( int w , int h )
: width ( w ) , height ( h ) {
data = new int [ width * height ]; // Allocate memory
std :: cout << " Constructor : allocated memory \ n " ;
}
*/
// ============================================================================
// EXAMPLE 15: Destructor called automatically at end of scope
// ============================================================================
int main () {
Rectangle rect (10 , 20) ;
// Destructor called automatically at end of scope
return 0;
// ============================================================================
// EXAMPLE 16: Class: Rectangle
// ============================================================================
/*
class Rectangle {
private :
int width ;
int height ;
*/
// ============================================================================
// EXAMPLE 17: Non - const function
// ============================================================================
/*
// Non - const function
void doubleSize () {
width *= 2;
height *= 2;
}
*/
// ============================================================================
// EXAMPLE 18: Class: Rectangle
// ============================================================================
/*
class Rectangle {
private :
int width ;
int height ;
static int objectCount ;
*/
// ============================================================================
// EXAMPLE 19: Function: main
// ============================================================================
int main () {
std :: cout << " Objects : " << Rectangle :: ge tObject Count () << ’\ n ’; // 0
// ============================================================================
// EXAMPLE 20: Class: Rectangle
// ============================================================================
/*
class Rectangle {
private :
int width , height ;
*/
// ============================================================================
// EXAMPLE 21: Code Example
// ============================================================================
/*
Rectangle ( int w , int h ) : width ( w ) , height ( h ) {
std :: cout << " Rectangle created \ n " ;
}
*/
// ============================================================================
// EXAMPLE 22: Function: display
// ============================================================================
/*
void display () const {
std :: cout << width << " x " << height << ’\ n ’;
}
*/
// ============================================================================
// EXAMPLE 23: stackRect and smartRect automatically cleaned up
// ============================================================================
/*
}
return 0;
// stackRect and smartRect automatically cleaned up
*/
// ============================================================================
// EXAMPLE 24: All smart pointers automatically clean up
// ============================================================================
/*
}
return 0;
// All smart pointers automatically clean up
*/
// ============================================================================
// EXAMPLE 25: Class: Point
// ============================================================================
/*
class Point {
private :
int x , y ;
*/
// ============================================================================
// EXAMPLE 26: Default constructor ( required for arrays )
// ============================================================================
/*
// Default constructor ( required for arrays )
Point () : x (0) , y (0) {
std :: cout << " Default constructor \ n " ;
}
Point ( int x , int y ) : x ( x ) , y ( y ) {
std :: cout << " Parameterized constructor \ n " ;
}
*/
// ============================================================================
// EXAMPLE 27: Function: display
// ============================================================================
/*
void display () const {
std :: cout << " ( " << x << " , " << y << " ) \ n " ;
}
*/
// ============================================================================
// EXAMPLE 28: Array of objects : calls default constructor
// ============================================================================
int main () {
// Array of objects : calls default constructor
Point points1 [3]; // 3 default constructors called
// Array with in itializa tion
Point points2 [] = {
Point (1 , 2) ,
// Parameterized
Point (3 , 4) ,
// Parameterized
Point ()
// Default
};
// ============================================================================
// EXAMPLE 29: C ++11: Uniform ini tializat ion
// ============================================================================
/*
// C ++11: Uniform ini tializat ion
Point points3 [] = {
{5 , 6} ,
{7 , 8} ,
{} // Default
};
*/
// ============================================================================
// EXAMPLE 30: Iterate through array
// ============================================================================
/*
// Iterate through array
for ( const Point & p : points3 ) {
p . display () ;
}
*/
// ============================================================================
// EXAMPLE 31: Class: Widget
// ============================================================================
/*
class Widget {
public :
Widget ( int w , int h ) : width ( w ) , height ( h ) {}
Widget ( int size ) : Widget ( size , size ) {} // Delegating constructor
protected :
int width , height ;
8 };
*/
// ============================================================================
// EXAMPLE 32: Inherit all Widget constructors
// ============================================================================
/*
class TextBox : public Widget {
public :
// Inherit all Widget constructors
using Widget :: Widget ;
*/
// ============================================================================
// EXAMPLE 33: Class: TextBox
// ============================================================================
/*
class TextBox : public Widget {
public :
*/
// ============================================================================
// EXAMPLE 34: When box goes out of scope :
// ============================================================================
int main () {
TextBox box ;
// When box goes out of scope :
// Output :
// TextBox destructor
// Widget destructor
return 0;
// ============================================================================
// MAIN FUNCTION
// ============================================================================
// Uncomment the examples above and add your test code here
int main() {
std::cout << "Chapter 8: Strings" << std::endl;
// Your test code here
return 0;
}