53,99 €
Expand your C++ knowledge quickly and efficiently with this advanced resource In the newly revised sixth edition of Professional C++, veteran software engineer and developer Marc Gregoire delivers yet another volume that raises the bar for advanced programming manuals. Covering almost all features of the new C++ standard codenamed C++23, the book offers case studies with working code that's been tested on Windows and Linux. As the leading resource for dedicated and knowledgeable professionals seeking to advance their C++ skills, this book provides resources that help readers: * Master new features of the latest standard, C++23 * Maximize C++ capabilities with effective design solutions * Discover little-known elements and learn about pitfalls and what practices to avoid * Grasp testing and debugging best practices * Learn about tips and tricks for efficiency and performance C++ is a complex language. Professional C++, 6th Edition, allows dedicated practitioners to remain current and abreast of the latest developments and advances.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 2406
Veröffentlichungsjahr: 2024
COVER
TABLE OF CONTENTS
TITLE PAGE
INTRODUCTION
WHO THIS BOOK IS FOR
WHAT THIS BOOK COVERS
HOW THIS BOOK IS STRUCTURED
CONVENTIONS
WHAT YOU NEED TO USE THIS BOOK
READER SUPPORT FOR THIS BOOK
NOTE
PART I: Introduction to Professional C++
1 A Crash Course in C++ and the Standard Library
C++ CRASH COURSE
YOUR FIRST BIGGER C++ PROGRAM
SUMMARY
EXERCISES
NOTE
2 Working with Strings and String Views
DYNAMIC STRINGS
FORMATTING AND PRINTING STRINGS
SUMMARY
EXERCISES
NOTES
3 Coding with Style
THE IMPORTANCE OF LOOKING GOOD
DOCUMENTING YOUR CODE
DECOMPOSITION
NAMING
USING LANGUAGE FEATURES WITH STYLE
FORMATTING
STYLISTIC CHALLENGES
SUMMARY
EXERCISES
NOTE
PART II: Professional C++ Software Design
4 Designing Professional C++ Programs
WHAT IS PROGRAMMING DESIGN?
THE IMPORTANCE OF PROGRAMMING DESIGN
DESIGNING FOR C++
TWO RULES FOR YOUR OWN C++ DESIGNS
REUSING EXISTING CODE
DESIGNING A CHESS PROGRAM
SUMMARY
EXERCISES
5 Designing with Classes
AM I THINKING PROCEDURALLY?
THE OBJECT-ORIENTED PHILOSOPHY
LIVING IN A WORLD OF CLASSES
CLASS RELATIONSHIPS
SUMMARY
EXERCISES
6 Designing for Reuse
THE REUSE PHILOSOPHY
HOW TO DESIGN REUSABLE CODE
SUMMARY
EXERCISES
NOTE
PART III: C++ Coding the Professional Way
7 Memory Management
WORKING WITH DYNAMIC MEMORY
ARRAY-POINTER DUALITY
LOW-LEVEL MEMORY OPERATIONS
COMMON MEMORY PITFALLS
SMART POINTERS
SUMMARY
EXERCISES
NOTES
8 Gaining Proficiency with Classes and Objects
INTRODUCING THE SPREADSHEET EXAMPLE
WRITING CLASSES
UNDERSTANDING OBJECT LIFE CYCLES
SUMMARY
EXERCISES
9 Mastering Classes and Objects
FRIENDS
DYNAMIC MEMORY ALLOCATION IN OBJECTS
MORE ABOUT MEMBER FUNCTIONS
CONSTEXPR AND CONSTEVAL
DIFFERENT KINDS OF DATA MEMBERS
NESTED CLASSES
ENUMERATIONS INSIDE CLASSES
OPERATOR OVERLOADING
BUILDING STABLE INTERFACES
SUMMARY
EXERCISES
NOTES
10 Discovering Inheritance Techniques
BUILDING CLASSES WITH INHERITANCE
INHERITANCE FOR REUSE
RESPECT YOUR PARENTS
INHERITANCE FOR POLYMORPHISM
MULTIPLE INHERITANCE
INTERESTING AND OBSCURE INHERITANCE ISSUES
CASTS
SUMMARY
EXERCISES
11 Modules, Header Files, and Miscellaneous Topics
MODULES
PREPROCESSOR DIRECTIVES
LINKAGE
HEADER FILES
FEATURE-TEST MACROS FOR CORE LANGUAGE FEATURES
THE STATIC KEYWORD
C-STYLE VARIABLE-LENGTH ARGUMENT LISTS
SUMMARY
EXERCISES
12 Writing Generic Code with Templates
OVERVIEW OF TEMPLATES
CLASS TEMPLATES
FUNCTION TEMPLATES
VARIABLE TEMPLATES
CONCEPTS
SUMMARY
EXERCISES
13 Demystifying C++ I/O
USING STREAMS
STRING STREAMS
SPAN-BASED STREAMS
FILE STREAMS
BIDIRECTIONAL I/O
FILESYSTEM SUPPORT LIBRARY
SUMMARY
EXERCISES
14 Handling Errors
ERRORS AND EXCEPTIONS
EXCEPTION MECHANICS
EXCEPTIONS AND POLYMORPHISM
RETHROWING EXCEPTIONS
STACK UNWINDING AND CLEANUP
SOURCE LOCATION
STACK TRACE
COMMON ERROR-HANDLING ISSUES
EXCEPTION SAFETY GUARANTEES
SUMMARY
EXERCISES
NOTE
15 Overloading C++ Operators
OVERVIEW OF OPERATOR OVERLOADING
OVERLOADING THE ARITHMETIC OPERATORS
OVERLOADING THE BITWISE AND BINARY LOGICAL OPERATORS
OVERLOADING THE INSERTION AND EXTRACTION OPERATORS
OVERLOADING THE SUBSCRIPTING OPERATOR
OVERLOADING THE FUNCTION CALL OPERATOR
OVERLOADING THE DEREFERENCING OPERATORS
WRITING CONVERSION OPERATORS
OVERLOADING THE MEMORY ALLOCATION AND DEALLOCATION OPERATORS
OVERLOADING USER-DEFINED LITERAL OPERATORS
SUMMARY
EXERCISES
16 Overview of the C++ Standard Library
CODING PRINCIPLES
OVERVIEW OF THE C++ STANDARD LIBRARY
SUMMARY
EXERCISES
17 Understanding Iterators and the Ranges Library
ITERATORS
STREAM ITERATORS
ITERATOR ADAPTERS
RANGES
SUMMARY
EXERCISES
NOTE
18 Standard Library Containers
CONTAINERS OVERVIEW
SEQUENTIAL CONTAINERS
SEQUENTIAL VIEWS
CONTAINER ADAPTERS
ASSOCIATIVE CONTAINERS
OTHER CONTAINERS
SUMMARY
EXERCISES
19 Function Pointers, Function Objects, and Lambda Expressions
FUNCTION POINTERS
POINTERS TO MEMBER FUNCTIONS (AND DATA MEMBERS)
FUNCTION OBJECTS
POLYMORPHIC FUNCTION WRAPPERS
LAMBDA EXPRESSIONS
INVOKERS
SUMMARY
EXERCISES
NOTE
20 Mastering Standard Library Algorithms
OVERVIEW OF ALGORITHMS
ALGORITHM DETAILS
SUMMARY
EXERCISES
21 String Localization and Regular Expressions
LOCALIZATION
REGULAR EXPRESSIONS
SUMMARY
EXERCISES
22 Date and Time Utilities
COMPILE-TIME RATIONAL NUMBERS
DURATION
CLOCK
TIME POINT
DATE
TIME ZONE
SUMMARY
EXERCISES
23 Random Number Facilities
C-STYLE RANDOM NUMBER GENERATION
RANDOM NUMBER ENGINES
RANDOM NUMBER ENGINE ADAPTERS
PREDEFINED ENGINES AND ENGINE ADAPTERS
GENERATING RANDOM NUMBERS
RANDOM NUMBER DISTRIBUTIONS
SUMMARY
EXERCISES
24 Additional Vocabulary Types
VARIANT
ANY
TUPLE
OPTIONAL: MONADIC OPERATIONS
EXPECTED
SUMMARY
EXERCISES
PART IV: Mastering Advanced Features of C++
25 Customizing and Extending the Standard Library
ALLOCATORS
EXTENDING THE STANDARD LIBRARY
SUMMARY
EXERCISES
NOTE
26 Advanced Templates
MORE ABOUT TEMPLATE PARAMETERS
CLASS TEMPLATE PARTIAL SPECIALIZATION
EMULATING FUNCTION PARTIAL SPECIALIZATION WITH OVERLOADING
TEMPLATE RECURSION
VARIADIC TEMPLATES
METAPROGRAMMING
SUMMARY
EXERCISES
27 Multithreaded Programming with C++
INTRODUCTION
THREADS
ATOMIC OPERATIONS LIBRARY
MUTUAL EXCLUSION
CONDITION VARIABLES
LATCHES
BARRIERS
SEMAPHORES
FUTURES
EXAMPLE: MULTITHREADED LOGGER CLASS
THREAD POOLS
COROUTINES
THREADING DESIGN AND BEST PRACTICES
SUMMARY
EXERCISES
NOTES
PART V: C++ Software Engineering
28 Maximizing Software Engineering Methods
THE NEED FOR PROCESS
SOFTWARE LIFE CYCLE MODELS
SOFTWARE ENGINEERING METHODOLOGIES
BUILDING YOUR OWN PROCESS AND METHODOLOGY
VERSION CONTROL
SUMMARY
EXERCISES
29 Writing Efficient C++
OVERVIEW OF PERFORMANCE AND EFFICIENCY
LANGUAGE-LEVEL EFFICIENCY
DESIGN-LEVEL EFFICIENCY
PROFILING
SUMMARY
EXERCISES
NOTES
30 Becoming Adept at Testing
QUALITY CONTROL
UNIT TESTING
FUZZ TESTING
HIGHER-LEVEL TESTING
TIPS FOR SUCCESSFUL TESTING
SUMMARY
EXERCISES
NOTE
31 Conquering Debugging
THE FUNDAMENTAL LAW OF DEBUGGING
BUG TAXONOMIES
AVOID BUGS
PLAN FOR BUGS
DEBUGGING TECHNIQUES
SUMMARY
EXERCISES
32 Incorporating Design Techniques and Frameworks
“I CAN NEVER REMEMBER HOW TO…”
THERE MUST BE A BETTER WAY
OBJECT-ORIENTED FRAMEWORKS
SUMMARY
EXERCISES
33 Applying Design Patterns
THE STRATEGY PATTERN
THE ABSTRACT FACTORY PATTERN
THE FACTORY METHOD PATTERN
OTHER FACTORY PATTERNS
THE ADAPTER PATTERN
THE PROXY PATTERN
THE ITERATOR PATTERN
THE OBSERVER PATTERN
THE DECORATOR PATTERN
THE CHAIN OF RESPONSIBILITY PATTERN
THE SINGLETON PATTERN
SUMMARY
EXERCISES
34 Developing Cross-Platform and Cross-Language Applications
CROSS-PLATFORM DEVELOPMENT
CROSS-LANGUAGE DEVELOPMENT
SUMMARY
EXERCISES
NOTE
PART VI: Appendices
A: C++ Interviews
CHAPTER 1: A CRASH COURSE IN C++ AND THE STANDARD LIBRARY
CHAPTERS 2 AND 21: WORKING WITH STRINGS AND STRING VIEWS, AND STRING LOCALIZATION AND REGULAR EXPRESSIONS
CHAPTER 3: CODING WITH STYLE
CHAPTER 4: DESIGNING PROFESSIONAL C++ PROGRAMS
CHAPTER 5: DESIGNING WITH CLASSES
CHAPTER 6: DESIGNING FOR REUSE
CHAPTER 7: MEMORY MANAGEMENT
CHAPTERS 8 AND 9: GAINING PROFICIENCY WITH CLASSES AND OBJECTS, AND MASTERING CLASSES AND OBJECTS
CHAPTER 10: DISCOVERING INHERITANCE TECHNIQUES
CHAPTER 11: MODULES, HEADER FILES, AND MISCELLANEOUS TOPICS
CHAPTERS 12 AND 26: WRITING GENERIC CODE WITH TEMPLATES, AND ADVANCED TEMPLATES
CHAPTER 13: DEMYSTIFYING C++ I/O
CHAPTER 14: HANDLING ERRORS
CHAPTER 15: OVERLOADING C++ OPERATORS
CHAPTERS 16–20 AND 25: THE STANDARD LIBRARY
CHAPTER 22: DATE AND TIME UTILITIES
CHAPTER 23: RANDOM NUMBER FACILITIES
CHAPTER 24: ADDITIONAL VOCABULARY TYPES
CHAPTER 27: MULTITHREADED PROGRAMMING WITH C++
CHAPTER 28: MAXIMIZING SOFTWARE ENGINEERING METHODS
CHAPTER 29: WRITING EFFICIENT C++
CHAPTER 30: BECOMING ADEPT AT TESTING
CHAPTER 31: CONQUERING DEBUGGING
CHAPTER 32: INCORPORATING DESIGN TECHNIQUES AND FRAMEWORKS
CHAPTER 33: APPLYING DESIGN PATTERNS
CHAPTER 34: DEVELOPING CROSS-PLATFORM AND CROSS-LANGUAGE APPLICATIONS
B: Annotated Bibliography
C++
UNIFIED MODELING LANGUAGE
ALGORITHMS AND DATA STRUCTURES
RANDOM NUMBERS
OPEN-SOURCE SOFTWARE
SOFTWARE ENGINEERING METHODOLOGY
PROGRAMMING STYLE
COMPUTER ARCHITECTURE
EFFICIENCY
TESTING
DEBUGGING
DESIGN PATTERNS
OPERATING SYSTEMS
MULTITHREADED PROGRAMMING
C: Standard Library Header Files
THE C STANDARD LIBRARY
CONTAINERS
ALGORITHMS, ITERATORS, RANGES, AND ALLOCATORS
GENERAL UTILITIES
MATHEMATICAL UTILITIES
EXCEPTIONS
I/O STREAMS
THREADING SUPPORT LIBRARY
D: Introduction to UML
DIAGRAM TYPES
CLASS DIAGRAMS
INTERACTION DIAGRAMS
INDEX
COPYRIGHT
DEDICATION
ABOUT THE AUTHOR
ABOUT THE TECHNICAL EDITORS
ACKNOWLEDGMENTS
END USER LICENSE AGREEMENT
Chapter 1
FIGURE 1.1
FIGURE 1.2
FIGURE 1.3
Chapter 2
FIGURE 2.1
Chapter 3
FIGURE 3.1
FIGURE 3.2
FIGURE 3.3
Chapter 4
FIGURE 4.1
FIGURE 4.2
FIGURE 4.3
FIGURE 4.4
FIGURE 4.5
FIGURE 4.6
FIGURE 4.7
FIGURE 4.8
Chapter 5
FIGURE 5.1
FIGURE 5.2
FIGURE 5.3
FIGURE 5.4
FIGURE 5.5
FIGURE 5.6
FIGURE 5.7
FIGURE 5.8
FIGURE 5.9
FIGURE 5.10
FIGURE 5.11
FIGURE 5.12
Chapter 6
FIGURE 6.1
FIGURE 6.2
Chapter 7
FIGURE 7.1
FIGURE 7.2
FIGURE 7.3
FIGURE 7.4
FIGURE 7.5
FIGURE 7.6
FIGURE 7.7
FIGURE 7.8
FIGURE 7.9
FIGURE 7.10
FIGURE 7.11
FIGURE 7.12
Chapter 9
FIGURE 9.1
FIGURE 9.2
FIGURE 9.3
FIGURE 9.4
FIGURE 9.5
Chapter 10
FIGURE 10.1
FIGURE 10.2
FIGURE 10.3
FIGURE 10.4
FIGURE 10.5
FIGURE 10.6
FIGURE 10.7
FIGURE 10.8
FIGURE 10.9
FIGURE 10.10
FIGURE 10.11
FIGURE 10.12
Chapter 14
FIGURE 14.1
FIGURE 14.2
FIGURE 14.3
Chapter 17
FIGURE 17.1
Chapter 18
FIGURE 18.1
Chapter 23
FIGURE 23.1
FIGURE 23.2
Chapter 25
FIGURE 25.1
FIGURE 25.2
Chapter 27
FIGURE 27.1
FIGURE 27.2
FIGURE 27.3
FIGURE 27.4
Chapter 28
FIGURE 28.1
FIGURE 28.2
FIGURE 28.3
FIGURE 28.4
FIGURE 28.5
FIGURE 28.6
Chapter 29
FIGURE 29.1
FIGURE 29.2
FIGURE 29.3
Chapter 30
FIGURE 30.1
FIGURE 30.2
FIGURE 30.3
FIGURE 30.4
FIGURE 30.5
FIGURE 30.6
FIGURE 30.7
Chapter 31
FIGURE 31.1
FIGURE 31.2
FIGURE 31.3
FIGURE 31.4
Chapter 32
FIGURE 32.1
FIGURE 32.2
FIGURE 32.3
FIGURE 32.4
Chapter 33
FIGURE 33.1
FIGURE 33.2
FIGURE 33.3
FIGURE 33.4
FIGURE 33.5
FIGURE 33.6
FIGURE 33.7
FIGURE 33.8
Appendix D
FIGURE D.1
FIGURE D.2
FIGURE D.3
FIGURE D.4
FIGURE D.5
FIGURE D.6
FIGURE D.7
FIGURE D.8
FIGURE D.9
Cover
Title Page
Copyright
Dedication
About the Author
Acknowledgments
Introduction
Table of Contents
Begin Reading
A: C++ Interviews
B: Annotated Bibliography
C: Standard Library Header Files
D: Introduction to UML
Index
End User License Agreement
i
xli
xlii
xliii
xliv
xlv
xlvi
xlvii
xlviii
xlix
1
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
143
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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
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
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
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
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
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
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
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
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
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
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
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
907
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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1237
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
ii
iii
iv
v
1327
Sixth Edition
Marc Gregoire
The development of C++ started in 1982 by Bjarne Stroustrup, a Danish computer scientist, as the successor of C with Classes. In 1985, the first edition of The C++ Programming Language book was released. The first standardized version of C++ was released in 1998, called C++98. In 2003, C++03 came out and contained a few small updates. After that, it was silent for a while, but traction slowly started building up, resulting in a major update of the language in 2011, called C++11. From then on, the C++ Standard Committee has been on a three-year cycle to release updated versions, giving us C++14, C++17, C++20, and now C++23. All in all, with the release of C++23 in 2023, C++ is almost 40 years old and still going strong. In most rankings of programming languages in 2023, C++ is in the top four. It is being used on an extremely wide range of hardware, going from small devices with embedded microprocessors all the way up to multi-rack supercomputers. Besides wide hardware support, C++ can be used to tackle almost any programming job, be it games on mobile platforms, performance-critical artificial intelligence (AI) and machine learning (ML) software, components for self-driving cars, real-time 3-D graphics engines, low-level hardware drivers, entire operating systems, software stacks for networking equipment, web browsers, and so on. The performance of C++ programs is hard to match with any other programming language, and as such, it is the de facto language for writing fast, powerful, and enterprise-class programs. Big tech companies, such as Microsoft, Facebook, Amazon, Google, and many more, use services written in C++ to run their infrastructure. As popular as C++ has become, the language can be difficult to grasp in full. There are simple, but powerful, techniques that professional C++ programmers use that don't show up in traditional texts, and there are useful parts of C++ that remain a mystery even to experienced C++ programmers.
Too often, programming books focus on the syntax of the language instead of its real-world use. The typical C++ text introduces a major part of the language in each chapter, explaining the syntax and providing an example. Professional C++ does not follow this pattern. Instead of giving you just the nuts and bolts of the language with little practical context, this book will teach you how to use C++ in the real world. It will show you the little-known features that will make your life easier, as well as the programming techniques that separate novices from professional programmers.
Even if you have used the language for years, you might still be unfamiliar with the more advanced features of C++, or you might not be using the full capabilities of the language. Maybe you don't yet know all the new features introduced with the latest release, C++23. Perhaps you write competent C++ code but would like to learn more about design and good programming style in C++. Or maybe you're relatively new to C++ but want to learn the “right” way to program from the start. This book will meet those needs and bring your C++ skills to the professional level.
Because this book focuses on advancing from basic or intermediate knowledge of C++ to becoming a professional C++ programmer, it assumes that you have some knowledge about programming. Chapter 1, “A Crash Course in C++ and the Standard Library,” covers the basics of C++ as a refresher, but it is not a substitute for actual training in programming. If you are just starting with C++ but you have experience in another programming language such as C, Java, or C#, you should be able to pick up most of what you need from Chapter 1.
In any case, you should have a solid foundation in programming fundamentals. You should know about loops, functions, and variables. You should know how to structure a program, and you should be familiar with fundamental techniques such as recursion. You should have some knowledge of common data structures such as queues, and useful algorithms such as sorting and searching. You don't need to know about object-oriented programming just yet—that is covered in Chapter 5, “Designing with Classes.”
You will also need to be familiar with the compiler you will be using to compile your code. Two compilers, Microsoft Visual C++ and GCC, are introduced later in this introduction. For other compilers, refer to the documentation that came with your compiler.
Professional C++ uses an approach to C++ programming that will both increase the quality of your code and improve your programming efficiency. You will find discussions on new C++23 features throughout this sixth edition. These features are not just isolated to a few chapters or sections; instead, examples have been updated to use new features when appropriate.
Professional C++ teaches you more than just the syntax and language features of C++. It also emphasizes programming methodologies, reusable design patterns, and good programming style. The Professional C++ methodology incorporates the entire software development process, from designing and writing code to debugging and working in groups. This approach will enable you to master the C++ language and its idiosyncrasies, as well as take advantage of its powerful capabilities for large-scale software development.
Imagine users who have learned all of the syntax of C++ without seeing a single example of its use. They know just enough to be dangerous! Without examples, they might assume that all code should go in the main() function of the program or that all variables should be global—practices that are generally not considered hallmarks of good programming.
Professional C++ programmers understand the correct way to use the language, in addition to the syntax. They recognize the importance of good design, the theories of object-oriented programming, and the best ways to use existing libraries. They have also developed an arsenal of useful code and reusable ideas.
By reading and understanding this book, you will become a professional C++ programmer. You will expand your knowledge of C++ to cover lesser known and often misunderstood language features. You will gain an appreciation for object-oriented design and acquire top-notch debugging skills. Perhaps most important, you will finish this book armed with a wealth of reusable ideas that you can actually apply to your daily work.
There are many good reasons to make the effort to be a professional C++ programmer as opposed to a programmer who knows C++. Understanding the true workings of the language will improve the quality of your code. Learning about different programming methodologies and processes will help you to work better with your team. Discovering reusable libraries and common design patterns will improve your daily efficiency and help you stop reinventing the wheel. All of these lessons will make you a better programmer and a more valuable employee. While this book can't guarantee you a promotion, it certainly won't hurt.
This book is made up of five parts.
Part I, “Introduction to Professional C++,” begins with a crash course in C++ basics to ensure a foundation of C++ knowledge. Following the crash course, Part I goes deeper into working with strings, because strings are used extensively in most examples throughout the book. The last chapter of Part I explores how to write readable C++ code.
Part II, “Professional C++ Software Design,” discusses C++ design methodologies. You will read about the importance of design, the object-oriented methodology, and the importance of code reuse.
Part III, “C++ Coding the Professional Way,” provides a technical tour of C++ from the professional point of view. You will read about the best ways to manage memory in C++, how to create reusable classes, and how to leverage important language features such as inheritance. You will also learn techniques for input and output, error handling, string localization, how to work with regular expressions, and how to structure your code in reusable components called modules. You will read about how to implement operator overloading, how to write templates, how to put restrictions on template parameters using concepts, and how to unlock the power of lambda expressions and function objects. This part also explains the C++ Standard Library, including containers, iterators, ranges, and algorithms. You will also read about some additional libraries that are available in the standard, such as the libraries to work with time, dates, time zones, random numbers, and the filesystem.
Part IV, “Mastering Advanced Features of C++,” demonstrates how you can get the most out of C++. This part of the book exposes the mysteries of C++ and describes how to use some of its more advanced features. You will read about how to customize and extend the C++ Standard Library to your needs, advanced details on template programming, including template metaprogramming, and how to use multithreading to take advantage of multiprocessor and multicore systems.
Part V, “C++ Software Engineering,” focuses on writing enterprise-quality software. You'll read about the engineering practices being used by programming organizations today; how to write efficient C++ code; software testing concepts, such as unit testing and regression testing; techniques used to debug C++ programs; how to incorporate design techniques, frameworks, and conceptual object-oriented design patterns into your own code; and solutions for cross-language and cross-platform code.
The book concludes with a useful chapter-by-chapter guide to succeeding in a C++ technical interview, an annotated bibliography, a summary of the C++ header files available in the standard, and a brief introduction to the Unified Modeling Language (UML).
This book is not a reference of every single class, member function, and function available in C++. The book C++17 Standard Library Quick Reference by Peter Van Weert and Marc Gregoire (Apress, 2019. ISBN: 978-1-4842-4923-9) is a condensed reference to all essential data structures, algorithms, and functions provided by the C++ Standard Library up until the C++17 standard.1Appendix B, “Annotated Bibliography,” lists a couple more references. Two excellent online references are:
cppreference.com
: You can use this reference online or download an offline version for use when you are not connected to the Internet.
cplusplus.com/reference
When I refer to a “Standard Library Reference” in this book, I am referring to one of these detailed C++ references.
The following are additional excellent online resources:
github.com/isocpp/CppCoreGuidelines
: The
C++ Core Guidelines
are a collaborative effort led by Bjarne Stroustrup, inventor of the C++ language itself. They are the result of many person-years of discussion and design across a number of organizations. The aim of the guidelines is to help people to use modern C++ effectively. The guidelines are focused on relatively higher-level issues, such as interfaces, resource management, memory management, and concurrency.
github.com/Microsoft/GSL
: This is an implementation by Microsoft of the
Guidelines Support Library
(GSL) containing functions and types that are suggested for use by the C++ Core Guidelines. It's a header-only library.
isocpp.org/faq
: This is a large collection of frequently asked C++ questions.
stackoverflow.com
: Search for answers to common programming questions—or ask your own questions.
To help you get the most from the text and keep track of what's happening, a number of conventions are used throughout this book.
WARNING Boxes like this one hold important, not-to-be-forgotten information that is directly relevant to the surrounding text.
NOTE Tips, hints, tricks, and asides to the current discussion are placed in boxes like this one.
As for styles in the text:
Important words are
italic
when they are introduced.
Keyboard strokes are shown like this: Ctrl+A.
Filenames and code within the text are shown like so:
monkey.cpp
.
URLs are shown like this:
wiley.com
Code is presented in three different ways:
// Comments in code are shown like this.
In code examples, new and important code is highlighted like this.
Code that's less important in the present context or that has been shown before is formatted like this.
Paragraphs or sections that are specific to the C++23 standard have a little C++23 icon on the left, just as this paragraph does. C++11, C++14, C++17, and C++20 features are not marked with any icon.
All you need to use this book is a computer with a C++ compiler. This book focuses only on parts of C++ that have been standardized, and not on vendor-specific compiler extensions.
You can use whichever C++ compiler you like. If you don't have a C++ compiler yet, you can download one for free. There are a lot of choices. For example, for Windows, you can download Microsoft Visual Studio Community Edition, which is free and includes Visual C++. For Linux, you can use GCC or Clang, which are also free.
The following two sections briefly explain how to use Visual C++ and GCC. Refer to the documentation that came with your compiler for more details.
This book discusses new features introduced with the C++23 standard. At the time of this writing, no compilers were fully C++23-compliant yet. Some new features were only supported by some compilers and not others, while other features were not yet supported by any compiler. Compiler vendors are hard at work to catch up with all new features, and I'm sure it won't take long before there will be full C++23-compliant compilers available. You can keep track of which compiler supports which features at en.cppreference.com/w/cpp/compiler_support.
At the time of this writing, not all compilers fully support modules yet; though all major compilers do, at least partially. This book uses modules everywhere. If your compiler does not yet support modules, you can convert modularized code to non-modularized code, as explained briefly in Chapter 11, “Modules, Header Files, and Miscellaneous Topics.”
First, you need to create a project. Start Visual C++ 2022, and on the welcome screen, click the Create A New Project button. If the welcome screen is not shown, select File ➪ New ➪ Project. In the Create A New Project dialog, search for the Console App project template with tags C++, Windows, and Console, and click Next. Specify a name for the project and a location where to save it and click Create.
Once your new project is loaded, you can see a list of project files in the Solution Explorer. If this docking window is not visible, select View ➪ Solution Explorer. A newly created project will contain a file called <projectname>.cpp under the Source Files section in the Solution Explorer. You can start writing your C++ code in that .cpp file, or if you want to compile source code files from the downloadable source archive for this book, select the <projectname>.cpp file in the Solution Explorer and delete it. You can add new files or existing files to a project by right-clicking the project name in the Solution Explorer and then selecting Add ➪ New Item or Add ➪ Existing Item.
At the time of this writing, Visual C++ 2022 does not yet automatically enable C++23 features. To enable C++23 features, in the Solution Explorer window, right-click your project and click Properties. In the Properties window, go to Configuration Properties ➪ General, set the C++ Language Standard option to ISO C++23 Standard or Preview - Features from the Latest C++ Working Draft, whichever is available in your version of Visual C++, and click OK.
Finally, select Build ➪ Build Solution to compile your code. When it compiles without errors, you can run it with Debug ➪ Start Debugging.
NOTE Microsoft Visual C++ has full support for modules, including the C++23 standard named module std.
You can create your source code files with any text editor you prefer and save them to a directory. To compile your code, open a terminal and run the following command, specifying all your .cpp files that you want to compile:
g++ -std=c++2b -o <executable_name> <source1.cpp> [source2.cpp …]
The -std=c++2b option is required to tell GCC to enable C++23 features. This option will change to -std=C++23 once GCC is fully C++23-compliant.
Support for modules in GCC is enabled with the -fmodules-ts option.
At the time of this writing, GCC does not yet support the C++23 standard named module std, introduced in Chapter 1. To make such code compile, you have to replace import std; declarations with import declarations of individual Standard Library headers. Once that is done, import declarations of Standard Library headers, such as the following, require you to precompile them:
import <iostream>;
Here is an example of precompiling <iostream>:
g++ -std=c++2b -fmodules-ts -xc++-system-header iostream
As an example, the AirlineTicket code from Chapter 1 uses modules. To compile it with GCC, first replace the use of std::println() with std::cout as GCC does not yet support <print> functionality at the time of this writing. After that, replace the import std; declarations with the appropriate import declarations, <string> and <iostream> for this example. You can find the adapted code in the Examples\Ch00\AirlineTicket directory in the downloadable source code archive.
Then, compile the two standard headers <iostream> and <string>:
g++ -std=c++2b -fmodules-ts -xc++-system-header iostream
g++ -std=c++2b -fmodules-ts -xc++-system-header string
Compile the module interface file:
g++ -std=c++2b -fmodules-ts -c -x c++ AirlineTicket.cppm
Finally, compile the application itself:
g++ -std=c++2b -fmodules-ts -o AirlineTicket AirlineTicket.cpp AirlineTicketTest.cpp AirlineTicket.o
When it compiles without errors, you can run it as follows:
./AirlineTicket
NOTE The process of compiling C++ code using C++ modules with GCC might change in the future. Also, support for the C++23 standard named module std will be added. In that case, please consult the GCC documentation for an updated procedure on how to compile such code.
Chapter 2, “Working with Strings and String Views,” explains that you can easily print the entire contents of Standard Library containers, such as std::vector, to the screen. This is a new feature since C++23 and not all compilers support this yet at the time of this writing.
As an example, Chapter 2 explains that you can write the contents of an std::vector as follows. Don't worry if you don't understand all the syntax yet, you will at the end of Chapter 2.
std::vector values { 11, 22, 33 };
std::print("{:n}", values);
This outputs:
11, 22, 33
If your compiler does not yet support this C++23 feature to print the contents of a container using std::print(), then you can convert the second line of code to the following:
for (const auto& value : values) { std::cout << value << ", "; }
This outputs:
11, 22, 33,
Again, don't worry if you don't understand the syntax yet. All will be clear at the end of Chapter 2.
The following sections describe different options to get support for this book.
As you work through the examples in this book, you may choose either to type in all the code manually or to use the source code files that accompany the book. However, I suggest you type in all the code manually because it greatly benefits the learning process and your memory. All of the source code used in this book is available for download at www.wiley.com/go/proc++6e or from GitHub at github.com/Professional-CPP/edition-6.
NOTE Because many books have similar titles, you may find it easiest to search by ISBN; for this book, the ISBN is 978-1-394-19317-2.
Once you've downloaded the code, just decompress it with your favorite decompression tool.
If you believe you've found a mistake in this book, please bring it to our attention. At John Wiley & Sons, we understand how important it is to provide our customers with accurate content, but even with our best efforts an error may occur.
To submit your possible errata, please e-mail it to our Customer Service Team at [email protected] with “Possible Book Errata Submission” as a subject line.
If you have any questions while reading this book, the author can easily be reached at [email protected] and will try to get back to you in a timely manner.
1
At the time of this writing, an updated edition called
C++23 Standard Library Quick Reference
is being worked on, which is a similar condensed reference but includes all C++20 and C++23 features.
CHAPTER 1:
A Crash Course in C++ and the Standard Library
CHAPTER 2:
Working with Strings and String Views
CHAPTER 3:
Coding with Style