- Broschiertes Buch
- Merkliste
- Auf die Merkliste
- Bewerten Bewerten
- Teilen
- Produkt teilen
- Produkterinnerung
- Produkterinnerung
C++20 for Programmers is an introductory-through-intermediate-level, tutorial presentation of computer programming in the latest version (C++20) of the C++ programming language, which is popular for developing systems software, embedded systems programming, operating systems, real-time systems, communications systems and other high-performance computer applications. Ideal for anyone who's worked with at least one programming language before, C++20 for Programmers utilizes a proven "early objects" approach, emphasizing program clarity, software reuse, and component-oriented software…mehr
Andere Kunden interessierten sich auch für
- Greg WilsonSoftware Design by Example41,99 €
- David VandevoordeC++ Templates72,99 €
- Sanjay MadhavGame Programming in C++45,99 €
- Anthony WilliamsC++ Concurrency in Action68,99 €
- Mike McGrathC++ Programming in Easy Steps, 6th Edition16,99 €
- Greg WilsonSoftware Design by Example59,99 €
- Rainer GrimmC++ Core Guidelines Explained: Best Practices for Modern C++42,99 €
-
C++20 for Programmers is an introductory-through-intermediate-level, tutorial presentation of computer programming in the latest version (C++20) of the C++ programming language, which is popular for developing systems software, embedded systems programming, operating systems, real-time systems, communications systems and other high-performance computer applications. Ideal for anyone who's worked with at least one programming language before, C++20 for Programmers utilizes a proven "early objects" approach, emphasizing program clarity, software reuse, and component-oriented software construction. In addition to the core language, it will help you take advantage of the newest standard libraries and the newest language extensions.
Hinweis: Dieser Artikel kann nur an eine deutsche Lieferadresse ausgeliefert werden.
Hinweis: Dieser Artikel kann nur an eine deutsche Lieferadresse ausgeliefert werden.
Produktdetails
- Produktdetails
- Deitel Developer Series
- Verlag: Pearson Education (US)
- 3 ed
- Seitenzahl: 960
- Erscheinungstermin: 16. April 2022
- Englisch
- Abmessung: 232mm x 182mm x 50mm
- Gewicht: 1636g
- ISBN-13: 9780136905691
- ISBN-10: 0136905692
- Artikelnr.: 59412518
- Deitel Developer Series
- Verlag: Pearson Education (US)
- 3 ed
- Seitenzahl: 960
- Erscheinungstermin: 16. April 2022
- Englisch
- Abmessung: 232mm x 182mm x 50mm
- Gewicht: 1636g
- ISBN-13: 9780136905691
- ISBN-10: 0136905692
- Artikelnr.: 59412518
Paul Deitel, CEO and Chief Technical Officer of Deitel & Associates, Inc., is a graduate of MIT, where he studied Information Technology. He holds the Java Certified Programmer and Java Certified Developer designations, and is an Oracle Java Champion. Through Deitel & Associates, Inc., he has delivered hundreds of programming courses worldwide to clients, including Cisco, IBM, Siemens, Sun Microsystems, Dell, Fidelity, NASA at the Kennedy Space Center, the National Severe Storm Laboratory, White Sands Missile Range, Rogue Wave Software, Boeing, SunGard Higher Education, Nortel Networks, Puma, iRobot, Invensys and many more. He and his co-author, Dr. Harvey M. Deitel, are the world’s best-selling programming-language textbook/professional book/video authors. Dr. Harvey Deitel, Chairman and Chief Strategy Officer of Deitel & Associates, Inc., has over 50 years of experience in the computer field. Dr. Deitel earned B.S. and M.S. degrees in Electrical Engineering from MIT and a Ph.D. in Mathematics from Boston University. He has extensive college teaching experience, including earning tenure and serving as the Chairman of the Computer Science Department at Boston College before founding Deitel & Associates, Inc., in 1991 with his son, Paul. The Deitels’ publications have earned international recognition, with translations published in Japanese, German, Russian, Spanish, French, Polish, Italian, Simplified Chinese, Traditional Chinese, Korean, Portuguese, Greek, Urdu and Turkish. Dr. Deitel has delivered hundreds of programming courses to corporate, academic, government and military clients.
Preface xxi
Before You Begin xliii
Chapter 1: Intro and Test-Driving Popular, Free C++ Compilers 1
1.1 Introduction 2
1.2 Test-Driving a C++20 Application 4
1.3 Moore's Law, Multi-Core Processors and Concurrent Programming 16
1.4 A Brief Refresher on Object Orientation 17
1.5 Wrap-Up 20
Chapter 2: Intro to C++20 Programming 21
2.1 Introduction 22
2.2 First Program in C++: Displaying a Line of Text 22
2.3 Modifying Our First C++ Program 25
2.4 Another C++ Program: Adding Integers 26
2.5 Arithmetic 30
2.6 Decision Making: Equality and Relational Operators 31
2.7 Objects Natural: Creating and Using Objects of Standard-Library Class
string 35
2.8 Wrap-Up 38
Chapter 3: Control Statements: Part 1 39
3.1 Introduction 40
3.2 Control Structures 40
3.3 if Single-Selection Statement 43
3.4 ifelse Double-Selection Statement 44
3.5 while Iteration Statement 47
3.6 Counter-Controlled Iteration 48
3.7 Sentinel-Controlled Iteration 50
3.8 Nested Control Statements 54
3.9 Compound Assignment Operators 57
3.10 Increment and Decrement Operators 58
3.11 Fundamental Types Are Not Portable 60
3.12 Objects-Natural Case Study: Arbitrary-Sized Integers 61
3.13 C++20: Text Formatting with Function format 65
3.14 Wrap-Up 67
Chapter 4: Control Statements: Part 2 69
4.1 Introduction 70
4.2 Essentials of Counter-Controlled Iteration 70
4.3 for Iteration Statement 71
4.4 Examples Using the for Statement 74
4.5 Application: Summing Even Integers 74
4.6 Application: Compound-Interest Calculations 75
4.7 dowhile Iteration Statement 78
4.8 switch Multiple-Selection Statement 80
4.9 C++17 Selection Statements with Initializers 85
4.10 break and continue Statements 86
4.11 Logical Operators 88
4.12 Confusing the Equality (==) and Assignment (=) Operators 92
4.13 Objects-Natural Case Study: Using the miniz-cpp Library to Write and
Read ZIP files 94
4.14 C++20 Text Formatting with Field Widths and Precisions 98
4.15 Wrap-Up 100
Chapter 5: Functions and an Intro to Function Templates 101
5.1 Introduction 102
5.2 C++ Program Components 103
5.3 Math Library Functions 103
5.4 Function Definitions and Function Prototypes 105
5.5 Order of Evaluation of a Function's Arguments 108
5.6 Function-Prototype and Argument-Coercion Notes 108
5.7 C++ Standard Library Headers 111
5.8 Case Study: Random-Number Generation 113
5.9 Case Study: Game of Chance; Introducing Scoped enums 119
5.10 Scope Rules 124
5.11 Inline Functions 128
5.12 References and Reference Parameters 129
5.13 Default Arguments 132
5.14 Unary Scope Resolution Operator 133
5.15 Function Overloading 134
5.16 Function Templates 137
5.17 Recursion 139
5.18 Example Using Recursion: Fibonacci Series 142
5.19 Recursion vs. Iteration 145
5.20 Lnfylun Lhqtomh Wjtz Qarcv: Qjwazkrplm xzz Xndmwwqhlz 147
5.21 Wrap-Up 150
Chapter 6: arrays, vectors, Ranges and Functional-Style Programming 153
6.1 Introduction 154
6.2 arrays 155
6.3 Declaring arrays 155
6.4 Initializing array Elements in a Loop 155
6.5 Initializing an array with an Initializer List 158
6.6 C++11 Range-Based for and C++20 Range-Based for with Initializer 159
6.7 Calculating array Element Values and an Intro to constexpr 161
6.8 Totaling array Elements 163
6.9 Using a Primitive Bar Chart to Display array Data Graphically 164
6.10 Using array Elements as Counters 165
6.11 Using arrays to Summarize Survey Results 166
6.12 Sorting and Searching arrays 168
6.13 Multidimensional arrays 170
6.14 Intro to Functional-Style Programming 174
6.15 Objects-Natural Case Study: C++ Standard Library Class Template vector
180
6.16 Wrap-Up 187
Chapter 7: (Downplaying) Pointers in Modern C++ 189
7.1 Introduction 190
7.2 Pointer Variable Declarations and Initialization 192
7.3 Pointer Operators 192
7.4 Pass-by-Reference with Pointers 195
7.5 Built-In Arrays 199
7.6 Using C++20 to_array to Convert a Built-In Array to a std::array 201
7.7 Using const with Pointers and the Data Pointed To 202
7.8 sizeof Operator 205
7.9 Pointer Expressions and Pointer Arithmetic 208
7.10 Objects-Natural Case Study: C++20 spans--Views of Contiguous Container
Elements 210
7.11 A Brief Intro to Pointer-Based Strings 216
7.12 Looking Ahead to Other Pointer Topics 220
7.13 Wrap-Up 220
Chapter 8: strings, string_views, Text Files, CSV Files and Regex 221
8.1 Introduction 222
8.2 string Assignment and Concatenation 223
8.3 Comparing strings 225
8.4 Substrings 226
8.5 Swapping strings 227
8.6 string Characteristics 227
8.7 Finding Substrings and Characters in a string 230
8.8 Replacing and Erasing Characters in a string 232
8.9 Inserting Characters into a string 234
8.10 C++11 Numeric Conversions 235
8.11 C++17 string_view 236
8.12 Files and Streams 239
8.13 Creating a Sequential File 240
8.14 Reading Data from a Sequential File 243
8.15 C++14 Reading and Writing Quoted Text 245
8.16 Updating Sequential Files 246
8.17 String Stream Processing 247
8.18 Raw String Literals 249
8.19 Objects-Natural Case Study: Reading and Analyzing a CSV File
Containing Titanic Disaster Data 250
8.20 Objects-Natural Case Study: Intro to Regular Expressions 259
8.21 Wrap-Up 267
Chapter 9: Custom Classes 269
9.1 Introduction 270
9.2 Test-Driving an Account Object 271
9.3 Account Class with a Data Member and Set and Get Member Functions 272
9.4 Account Class: Custom Constructors 275
9.5 Software Engineering with Set and Get Member Functions 279
9.6 Account Class with a Balance 280
9.7 Time Class Case Study: Separating Interface from Implementation 283
9.8 Compilation and Linking Process 290
9.9 Class Scope and Accessing Class Members 291
9.10 Access Functions and Utility Functions 292
9.11 Time Class Case Study: Constructors with Default Arguments 292
9.12 Destructors 298
9.13 When Constructors and Destructors Are Called 298
9.14 Time Class Case Study: A Subtle Trap--Returning a Reference or a
Pointer to a private Data Member 302
9.15 Default Assignment Operator 304
9.16 const Objects and const Member Functions 306
9.17 Composition: Objects as Members of Classes 308
9.18 friend Functions and friend Classes 313
9.19 The this Pointer 314
9.20 static Class Members: Classwide Data and Member Functions 320
9.21 Aggregates in C++20 324
9.22 Objects-Natural Case Study: Serialization with JSON 326
9.23 Wrap-Up 333
Chapter 10: OOP: Inheritance and Runtime Polymorphism 335
10.1 Introduction 336
10.2 Base Classes and Derived Classes 339
10.3 Relationship Between Base and Derived Classes 341
10.4 Constructors and Destructors in Derived Classes 349
10.5 Intro to Runtime Polymorphism: Polymorphic Video Game 350
10.6 Relationships Among Objects in an Inheritance Hierarchy 351
10.7 Virtual Functions and Virtual Destructors 357
10.8 Abstract Classes and Pure virtual Functions 362
10.9 Case Study: Payroll System Using Runtime Polymorphism 363
10.10 Runtime Polymorphism, Virtual Functions and Dynamic Binding "Under
the Hood" 373
10.11 Non-Virtual Interface (NVI) Idiom 376
10.12 Program to an Interface, Not an Implementation 383
10.13 Runtime Polymorphism with std::variant and std::visit 391
10.14 Multiple Inheritance 397
10.15 protected Class Members: A Deeper Look 405
10.16 public, protected and private Inheritance 406
10.17 More Runtime Polymorphism Techniques; Compile-Time Polymorphism 408
10.18 Wrap-Up 412
Chapter 11: Operator Overloading, Copy/Move Semantics and Smart Pointers
415
11.1 Introduction 416
11.2 Using the Overloaded Operators of Standard Library Class string 418
11.3 Operator Overloading Fundamentals 423
11.4 (Downplaying) Dynamic Memory Management with new and delete 425
11.5 Modern C++ Dynamic Memory Management: RAII and Smart Pointers 427
11.6 MyArray Case Study: Crafting a Valuable Class with Operator
Overloading 430
11.7 C++20 Three-Way Comparison Operator () 459
11.8 Converting Between Types 462
11.9 explicit Constructors and Conversion Operators 463
11.10 Overloading the Function Call Operator () 466
11.11 Wrap-Up 466
Chapter 12: Exceptions and a Look Forward to Contracts 467
12.1 Introduction 468
12.2 Exception-Handling Flow of Control 471
12.3 Exception Safety Guarantees and noexcept 476
12.4 Rethrowing an Exception 477
12.5 Stack Unwinding and Uncaught Exceptions 479
12.6 When to Use Exception Handling 481
12.7 Constructors, Destructors and Exception Handling 483
12.8 Processing new Failures 487
12.9 Standard Library Exception Hierarchy 490
12.10 C++'s Alternative to the finally Block: Resource Acquisition Is
Initialization (RAII) 493
12.11 Some Libraries Support Both Exceptions and Error Codes 493
12.12 Logging 494
12.13 Looking Ahead to Contracts 495
12.14 Wrap-Up 503
Chapter 13: Standard Library Containers and Iterators 505
13.1 Introduction 506
13.2 Introduction to Containers 508
13.3 Working with Iterators 513
13.4 A Brief Introduction to Algorithms 518
13.5 Sequence Containers 518
13.6 vector Sequence Container 519
13.7 list Sequence Container 526
13.8 deque Sequence Container 531
13.9 Associative Containers 533
13.10 Container Adaptors 543
13.11 bitset Near Container 547
13.12 Optional: A Brief Intro to Big O 549
13.13 Optional: A Brief Intro to Hash Tables 552
13.14 Wrap-Up 553
Chapter 14: Standard Library Algorithms and C++20 Ranges & Views 555
14.1 Introduction 556
14.2 Algorithm Requirements: C++20 Concepts 558
14.3 Lambdas and Algorithms 560
14.4 Algorithms 563
14.5 Function Objects (Functors) 603
14.6 Projections 608
14.7 C++20 Views and Functional-Style Programming 611
14.8 Intro to Parallel Algorithms 617
14.9 Standard Library Algorithm Summary 619
14.10 A Look Ahead to C++23 Ranges 622
14.11 Wrap-Up 623
Chapter 15: Templates, C++20 Concepts and Metaprogramming 625
15.1 Introduction 626
15.2 Custom Class Templates and Compile-Time Polymorphism 629
15.3 C++20 Function Template Enhancements 634
15.4 C++20 Concepts: A First Look 636
15.5 Type Traits 644
15.6 C++20 Concepts: A Deeper Look 648
15.7 Testing C++20 Concepts with static_assert 659
15.8 Creating a Custom Algorithm 661
15.9 Creating a Custom Container and Iterators 663
15.10 Default Arguments for Template Type Parameters 678
15.11 Variable Templates 678
15.12 Variadic Templates and Fold Expressions 679
15.13 Template Metaprogramming 693
15.14 Wrap-Up 705
Chapter 16: C++20 Modules: Large-Scale Development 707
16.1 Introduction 708
16.2 Compilation and Linking Before C++20 710
16.3 Advantages and Goals of Modules 711
16.4 Example: Transitioning to Modules--Header Units 712
16.5 Modules Can Reduce Translation Unit Sizes and Compilation Times 715
16.6 Example: Creating and Using a Module 716
16.7 Global Module Fragment 724
16.8 Separating Interface from Implementation 725
16.9 Partitions 732
16.10 Additional Modules Examples 740
16.11 Migrating Code to Modules 746
16.12 Future of Modules and Modules Tooling 746
16.13 Wrap-Up 748
Chapter 17: Parallel Algorithms and Concurrency: A High-Level View 755
17.1 Introduction 756
17.2 Standard Library Parallel Algorithms (C++17) 759
17.3 Multithreaded Programming 767
17.4 Launching Tasks with std::jthread 771
17.5 ProducerConsumer Relationship: A First Attempt 776
17.6 ProducerConsumer: Synchronizing Access to Shared Mutable Data 783
17.7 ProducerConsumer: Minimizing Waits with a Circular Buffer 795
17.8 Readers and Writers 804
17.9 Cooperatively Canceling jthreads 805
17.10 Launching Tasks with std::async 808
17.11 Thread-Safe, One-Time Initialization 815
17.12 A Brief Introduction to Atomics 816
17.13 Coordinating Threads with C++20 Latches and Barriers 820
17.14 C++20 Semaphores 826
17.15 C++23: A Look to the Future of C++ Concurrency 830
17.16 Wrap-Up 831
Chapter 18: C++20 Coroutines 833
18.1 Introduction 834
18.2 Coroutine Support Libraries 835
18.3 Installing the concurrencpp and generator Libraries 837
18.4 Creating a Generator Coroutine with co_yield and the generator Library
837
18.5 Launching Tasks with concurrencpp 841
18.6 Creating a Coroutine with co_await and co_return 845
18.7 Low-Level Coroutines Concepts 853
18.8 C++23 Coroutines Enhancements 855
18.9 Wrap-Up 856
Appendix A: Operator Precedence and Grouping 857
Appendix B: Character Set 859
Index 861
Online Chapters and Appendices
Chapter 19: Stream I/O and C++20 Text Formatting
Chapter 20: Other Topics and a Look Toward the Future of C++
Appendix C: Number Systems
Appendix D: Preprocessor
Appendix E: Bit Manipulation
Before You Begin xliii
Chapter 1: Intro and Test-Driving Popular, Free C++ Compilers 1
1.1 Introduction 2
1.2 Test-Driving a C++20 Application 4
1.3 Moore's Law, Multi-Core Processors and Concurrent Programming 16
1.4 A Brief Refresher on Object Orientation 17
1.5 Wrap-Up 20
Chapter 2: Intro to C++20 Programming 21
2.1 Introduction 22
2.2 First Program in C++: Displaying a Line of Text 22
2.3 Modifying Our First C++ Program 25
2.4 Another C++ Program: Adding Integers 26
2.5 Arithmetic 30
2.6 Decision Making: Equality and Relational Operators 31
2.7 Objects Natural: Creating and Using Objects of Standard-Library Class
string 35
2.8 Wrap-Up 38
Chapter 3: Control Statements: Part 1 39
3.1 Introduction 40
3.2 Control Structures 40
3.3 if Single-Selection Statement 43
3.4 ifelse Double-Selection Statement 44
3.5 while Iteration Statement 47
3.6 Counter-Controlled Iteration 48
3.7 Sentinel-Controlled Iteration 50
3.8 Nested Control Statements 54
3.9 Compound Assignment Operators 57
3.10 Increment and Decrement Operators 58
3.11 Fundamental Types Are Not Portable 60
3.12 Objects-Natural Case Study: Arbitrary-Sized Integers 61
3.13 C++20: Text Formatting with Function format 65
3.14 Wrap-Up 67
Chapter 4: Control Statements: Part 2 69
4.1 Introduction 70
4.2 Essentials of Counter-Controlled Iteration 70
4.3 for Iteration Statement 71
4.4 Examples Using the for Statement 74
4.5 Application: Summing Even Integers 74
4.6 Application: Compound-Interest Calculations 75
4.7 dowhile Iteration Statement 78
4.8 switch Multiple-Selection Statement 80
4.9 C++17 Selection Statements with Initializers 85
4.10 break and continue Statements 86
4.11 Logical Operators 88
4.12 Confusing the Equality (==) and Assignment (=) Operators 92
4.13 Objects-Natural Case Study: Using the miniz-cpp Library to Write and
Read ZIP files 94
4.14 C++20 Text Formatting with Field Widths and Precisions 98
4.15 Wrap-Up 100
Chapter 5: Functions and an Intro to Function Templates 101
5.1 Introduction 102
5.2 C++ Program Components 103
5.3 Math Library Functions 103
5.4 Function Definitions and Function Prototypes 105
5.5 Order of Evaluation of a Function's Arguments 108
5.6 Function-Prototype and Argument-Coercion Notes 108
5.7 C++ Standard Library Headers 111
5.8 Case Study: Random-Number Generation 113
5.9 Case Study: Game of Chance; Introducing Scoped enums 119
5.10 Scope Rules 124
5.11 Inline Functions 128
5.12 References and Reference Parameters 129
5.13 Default Arguments 132
5.14 Unary Scope Resolution Operator 133
5.15 Function Overloading 134
5.16 Function Templates 137
5.17 Recursion 139
5.18 Example Using Recursion: Fibonacci Series 142
5.19 Recursion vs. Iteration 145
5.20 Lnfylun Lhqtomh Wjtz Qarcv: Qjwazkrplm xzz Xndmwwqhlz 147
5.21 Wrap-Up 150
Chapter 6: arrays, vectors, Ranges and Functional-Style Programming 153
6.1 Introduction 154
6.2 arrays 155
6.3 Declaring arrays 155
6.4 Initializing array Elements in a Loop 155
6.5 Initializing an array with an Initializer List 158
6.6 C++11 Range-Based for and C++20 Range-Based for with Initializer 159
6.7 Calculating array Element Values and an Intro to constexpr 161
6.8 Totaling array Elements 163
6.9 Using a Primitive Bar Chart to Display array Data Graphically 164
6.10 Using array Elements as Counters 165
6.11 Using arrays to Summarize Survey Results 166
6.12 Sorting and Searching arrays 168
6.13 Multidimensional arrays 170
6.14 Intro to Functional-Style Programming 174
6.15 Objects-Natural Case Study: C++ Standard Library Class Template vector
180
6.16 Wrap-Up 187
Chapter 7: (Downplaying) Pointers in Modern C++ 189
7.1 Introduction 190
7.2 Pointer Variable Declarations and Initialization 192
7.3 Pointer Operators 192
7.4 Pass-by-Reference with Pointers 195
7.5 Built-In Arrays 199
7.6 Using C++20 to_array to Convert a Built-In Array to a std::array 201
7.7 Using const with Pointers and the Data Pointed To 202
7.8 sizeof Operator 205
7.9 Pointer Expressions and Pointer Arithmetic 208
7.10 Objects-Natural Case Study: C++20 spans--Views of Contiguous Container
Elements 210
7.11 A Brief Intro to Pointer-Based Strings 216
7.12 Looking Ahead to Other Pointer Topics 220
7.13 Wrap-Up 220
Chapter 8: strings, string_views, Text Files, CSV Files and Regex 221
8.1 Introduction 222
8.2 string Assignment and Concatenation 223
8.3 Comparing strings 225
8.4 Substrings 226
8.5 Swapping strings 227
8.6 string Characteristics 227
8.7 Finding Substrings and Characters in a string 230
8.8 Replacing and Erasing Characters in a string 232
8.9 Inserting Characters into a string 234
8.10 C++11 Numeric Conversions 235
8.11 C++17 string_view 236
8.12 Files and Streams 239
8.13 Creating a Sequential File 240
8.14 Reading Data from a Sequential File 243
8.15 C++14 Reading and Writing Quoted Text 245
8.16 Updating Sequential Files 246
8.17 String Stream Processing 247
8.18 Raw String Literals 249
8.19 Objects-Natural Case Study: Reading and Analyzing a CSV File
Containing Titanic Disaster Data 250
8.20 Objects-Natural Case Study: Intro to Regular Expressions 259
8.21 Wrap-Up 267
Chapter 9: Custom Classes 269
9.1 Introduction 270
9.2 Test-Driving an Account Object 271
9.3 Account Class with a Data Member and Set and Get Member Functions 272
9.4 Account Class: Custom Constructors 275
9.5 Software Engineering with Set and Get Member Functions 279
9.6 Account Class with a Balance 280
9.7 Time Class Case Study: Separating Interface from Implementation 283
9.8 Compilation and Linking Process 290
9.9 Class Scope and Accessing Class Members 291
9.10 Access Functions and Utility Functions 292
9.11 Time Class Case Study: Constructors with Default Arguments 292
9.12 Destructors 298
9.13 When Constructors and Destructors Are Called 298
9.14 Time Class Case Study: A Subtle Trap--Returning a Reference or a
Pointer to a private Data Member 302
9.15 Default Assignment Operator 304
9.16 const Objects and const Member Functions 306
9.17 Composition: Objects as Members of Classes 308
9.18 friend Functions and friend Classes 313
9.19 The this Pointer 314
9.20 static Class Members: Classwide Data and Member Functions 320
9.21 Aggregates in C++20 324
9.22 Objects-Natural Case Study: Serialization with JSON 326
9.23 Wrap-Up 333
Chapter 10: OOP: Inheritance and Runtime Polymorphism 335
10.1 Introduction 336
10.2 Base Classes and Derived Classes 339
10.3 Relationship Between Base and Derived Classes 341
10.4 Constructors and Destructors in Derived Classes 349
10.5 Intro to Runtime Polymorphism: Polymorphic Video Game 350
10.6 Relationships Among Objects in an Inheritance Hierarchy 351
10.7 Virtual Functions and Virtual Destructors 357
10.8 Abstract Classes and Pure virtual Functions 362
10.9 Case Study: Payroll System Using Runtime Polymorphism 363
10.10 Runtime Polymorphism, Virtual Functions and Dynamic Binding "Under
the Hood" 373
10.11 Non-Virtual Interface (NVI) Idiom 376
10.12 Program to an Interface, Not an Implementation 383
10.13 Runtime Polymorphism with std::variant and std::visit 391
10.14 Multiple Inheritance 397
10.15 protected Class Members: A Deeper Look 405
10.16 public, protected and private Inheritance 406
10.17 More Runtime Polymorphism Techniques; Compile-Time Polymorphism 408
10.18 Wrap-Up 412
Chapter 11: Operator Overloading, Copy/Move Semantics and Smart Pointers
415
11.1 Introduction 416
11.2 Using the Overloaded Operators of Standard Library Class string 418
11.3 Operator Overloading Fundamentals 423
11.4 (Downplaying) Dynamic Memory Management with new and delete 425
11.5 Modern C++ Dynamic Memory Management: RAII and Smart Pointers 427
11.6 MyArray Case Study: Crafting a Valuable Class with Operator
Overloading 430
11.7 C++20 Three-Way Comparison Operator () 459
11.8 Converting Between Types 462
11.9 explicit Constructors and Conversion Operators 463
11.10 Overloading the Function Call Operator () 466
11.11 Wrap-Up 466
Chapter 12: Exceptions and a Look Forward to Contracts 467
12.1 Introduction 468
12.2 Exception-Handling Flow of Control 471
12.3 Exception Safety Guarantees and noexcept 476
12.4 Rethrowing an Exception 477
12.5 Stack Unwinding and Uncaught Exceptions 479
12.6 When to Use Exception Handling 481
12.7 Constructors, Destructors and Exception Handling 483
12.8 Processing new Failures 487
12.9 Standard Library Exception Hierarchy 490
12.10 C++'s Alternative to the finally Block: Resource Acquisition Is
Initialization (RAII) 493
12.11 Some Libraries Support Both Exceptions and Error Codes 493
12.12 Logging 494
12.13 Looking Ahead to Contracts 495
12.14 Wrap-Up 503
Chapter 13: Standard Library Containers and Iterators 505
13.1 Introduction 506
13.2 Introduction to Containers 508
13.3 Working with Iterators 513
13.4 A Brief Introduction to Algorithms 518
13.5 Sequence Containers 518
13.6 vector Sequence Container 519
13.7 list Sequence Container 526
13.8 deque Sequence Container 531
13.9 Associative Containers 533
13.10 Container Adaptors 543
13.11 bitset Near Container 547
13.12 Optional: A Brief Intro to Big O 549
13.13 Optional: A Brief Intro to Hash Tables 552
13.14 Wrap-Up 553
Chapter 14: Standard Library Algorithms and C++20 Ranges & Views 555
14.1 Introduction 556
14.2 Algorithm Requirements: C++20 Concepts 558
14.3 Lambdas and Algorithms 560
14.4 Algorithms 563
14.5 Function Objects (Functors) 603
14.6 Projections 608
14.7 C++20 Views and Functional-Style Programming 611
14.8 Intro to Parallel Algorithms 617
14.9 Standard Library Algorithm Summary 619
14.10 A Look Ahead to C++23 Ranges 622
14.11 Wrap-Up 623
Chapter 15: Templates, C++20 Concepts and Metaprogramming 625
15.1 Introduction 626
15.2 Custom Class Templates and Compile-Time Polymorphism 629
15.3 C++20 Function Template Enhancements 634
15.4 C++20 Concepts: A First Look 636
15.5 Type Traits 644
15.6 C++20 Concepts: A Deeper Look 648
15.7 Testing C++20 Concepts with static_assert 659
15.8 Creating a Custom Algorithm 661
15.9 Creating a Custom Container and Iterators 663
15.10 Default Arguments for Template Type Parameters 678
15.11 Variable Templates 678
15.12 Variadic Templates and Fold Expressions 679
15.13 Template Metaprogramming 693
15.14 Wrap-Up 705
Chapter 16: C++20 Modules: Large-Scale Development 707
16.1 Introduction 708
16.2 Compilation and Linking Before C++20 710
16.3 Advantages and Goals of Modules 711
16.4 Example: Transitioning to Modules--Header Units 712
16.5 Modules Can Reduce Translation Unit Sizes and Compilation Times 715
16.6 Example: Creating and Using a Module 716
16.7 Global Module Fragment 724
16.8 Separating Interface from Implementation 725
16.9 Partitions 732
16.10 Additional Modules Examples 740
16.11 Migrating Code to Modules 746
16.12 Future of Modules and Modules Tooling 746
16.13 Wrap-Up 748
Chapter 17: Parallel Algorithms and Concurrency: A High-Level View 755
17.1 Introduction 756
17.2 Standard Library Parallel Algorithms (C++17) 759
17.3 Multithreaded Programming 767
17.4 Launching Tasks with std::jthread 771
17.5 ProducerConsumer Relationship: A First Attempt 776
17.6 ProducerConsumer: Synchronizing Access to Shared Mutable Data 783
17.7 ProducerConsumer: Minimizing Waits with a Circular Buffer 795
17.8 Readers and Writers 804
17.9 Cooperatively Canceling jthreads 805
17.10 Launching Tasks with std::async 808
17.11 Thread-Safe, One-Time Initialization 815
17.12 A Brief Introduction to Atomics 816
17.13 Coordinating Threads with C++20 Latches and Barriers 820
17.14 C++20 Semaphores 826
17.15 C++23: A Look to the Future of C++ Concurrency 830
17.16 Wrap-Up 831
Chapter 18: C++20 Coroutines 833
18.1 Introduction 834
18.2 Coroutine Support Libraries 835
18.3 Installing the concurrencpp and generator Libraries 837
18.4 Creating a Generator Coroutine with co_yield and the generator Library
837
18.5 Launching Tasks with concurrencpp 841
18.6 Creating a Coroutine with co_await and co_return 845
18.7 Low-Level Coroutines Concepts 853
18.8 C++23 Coroutines Enhancements 855
18.9 Wrap-Up 856
Appendix A: Operator Precedence and Grouping 857
Appendix B: Character Set 859
Index 861
Online Chapters and Appendices
Chapter 19: Stream I/O and C++20 Text Formatting
Chapter 20: Other Topics and a Look Toward the Future of C++
Appendix C: Number Systems
Appendix D: Preprocessor
Appendix E: Bit Manipulation
Preface xxi
Before You Begin xliii
Chapter 1: Intro and Test-Driving Popular, Free C++ Compilers 1
1.1 Introduction 2
1.2 Test-Driving a C++20 Application 4
1.3 Moore's Law, Multi-Core Processors and Concurrent Programming 16
1.4 A Brief Refresher on Object Orientation 17
1.5 Wrap-Up 20
Chapter 2: Intro to C++20 Programming 21
2.1 Introduction 22
2.2 First Program in C++: Displaying a Line of Text 22
2.3 Modifying Our First C++ Program 25
2.4 Another C++ Program: Adding Integers 26
2.5 Arithmetic 30
2.6 Decision Making: Equality and Relational Operators 31
2.7 Objects Natural: Creating and Using Objects of Standard-Library Class
string 35
2.8 Wrap-Up 38
Chapter 3: Control Statements: Part 1 39
3.1 Introduction 40
3.2 Control Structures 40
3.3 if Single-Selection Statement 43
3.4 ifelse Double-Selection Statement 44
3.5 while Iteration Statement 47
3.6 Counter-Controlled Iteration 48
3.7 Sentinel-Controlled Iteration 50
3.8 Nested Control Statements 54
3.9 Compound Assignment Operators 57
3.10 Increment and Decrement Operators 58
3.11 Fundamental Types Are Not Portable 60
3.12 Objects-Natural Case Study: Arbitrary-Sized Integers 61
3.13 C++20: Text Formatting with Function format 65
3.14 Wrap-Up 67
Chapter 4: Control Statements: Part 2 69
4.1 Introduction 70
4.2 Essentials of Counter-Controlled Iteration 70
4.3 for Iteration Statement 71
4.4 Examples Using the for Statement 74
4.5 Application: Summing Even Integers 74
4.6 Application: Compound-Interest Calculations 75
4.7 dowhile Iteration Statement 78
4.8 switch Multiple-Selection Statement 80
4.9 C++17 Selection Statements with Initializers 85
4.10 break and continue Statements 86
4.11 Logical Operators 88
4.12 Confusing the Equality (==) and Assignment (=) Operators 92
4.13 Objects-Natural Case Study: Using the miniz-cpp Library to Write and
Read ZIP files 94
4.14 C++20 Text Formatting with Field Widths and Precisions 98
4.15 Wrap-Up 100
Chapter 5: Functions and an Intro to Function Templates 101
5.1 Introduction 102
5.2 C++ Program Components 103
5.3 Math Library Functions 103
5.4 Function Definitions and Function Prototypes 105
5.5 Order of Evaluation of a Function's Arguments 108
5.6 Function-Prototype and Argument-Coercion Notes 108
5.7 C++ Standard Library Headers 111
5.8 Case Study: Random-Number Generation 113
5.9 Case Study: Game of Chance; Introducing Scoped enums 119
5.10 Scope Rules 124
5.11 Inline Functions 128
5.12 References and Reference Parameters 129
5.13 Default Arguments 132
5.14 Unary Scope Resolution Operator 133
5.15 Function Overloading 134
5.16 Function Templates 137
5.17 Recursion 139
5.18 Example Using Recursion: Fibonacci Series 142
5.19 Recursion vs. Iteration 145
5.20 Lnfylun Lhqtomh Wjtz Qarcv: Qjwazkrplm xzz Xndmwwqhlz 147
5.21 Wrap-Up 150
Chapter 6: arrays, vectors, Ranges and Functional-Style Programming 153
6.1 Introduction 154
6.2 arrays 155
6.3 Declaring arrays 155
6.4 Initializing array Elements in a Loop 155
6.5 Initializing an array with an Initializer List 158
6.6 C++11 Range-Based for and C++20 Range-Based for with Initializer 159
6.7 Calculating array Element Values and an Intro to constexpr 161
6.8 Totaling array Elements 163
6.9 Using a Primitive Bar Chart to Display array Data Graphically 164
6.10 Using array Elements as Counters 165
6.11 Using arrays to Summarize Survey Results 166
6.12 Sorting and Searching arrays 168
6.13 Multidimensional arrays 170
6.14 Intro to Functional-Style Programming 174
6.15 Objects-Natural Case Study: C++ Standard Library Class Template vector
180
6.16 Wrap-Up 187
Chapter 7: (Downplaying) Pointers in Modern C++ 189
7.1 Introduction 190
7.2 Pointer Variable Declarations and Initialization 192
7.3 Pointer Operators 192
7.4 Pass-by-Reference with Pointers 195
7.5 Built-In Arrays 199
7.6 Using C++20 to_array to Convert a Built-In Array to a std::array 201
7.7 Using const with Pointers and the Data Pointed To 202
7.8 sizeof Operator 205
7.9 Pointer Expressions and Pointer Arithmetic 208
7.10 Objects-Natural Case Study: C++20 spans--Views of Contiguous Container
Elements 210
7.11 A Brief Intro to Pointer-Based Strings 216
7.12 Looking Ahead to Other Pointer Topics 220
7.13 Wrap-Up 220
Chapter 8: strings, string_views, Text Files, CSV Files and Regex 221
8.1 Introduction 222
8.2 string Assignment and Concatenation 223
8.3 Comparing strings 225
8.4 Substrings 226
8.5 Swapping strings 227
8.6 string Characteristics 227
8.7 Finding Substrings and Characters in a string 230
8.8 Replacing and Erasing Characters in a string 232
8.9 Inserting Characters into a string 234
8.10 C++11 Numeric Conversions 235
8.11 C++17 string_view 236
8.12 Files and Streams 239
8.13 Creating a Sequential File 240
8.14 Reading Data from a Sequential File 243
8.15 C++14 Reading and Writing Quoted Text 245
8.16 Updating Sequential Files 246
8.17 String Stream Processing 247
8.18 Raw String Literals 249
8.19 Objects-Natural Case Study: Reading and Analyzing a CSV File
Containing Titanic Disaster Data 250
8.20 Objects-Natural Case Study: Intro to Regular Expressions 259
8.21 Wrap-Up 267
Chapter 9: Custom Classes 269
9.1 Introduction 270
9.2 Test-Driving an Account Object 271
9.3 Account Class with a Data Member and Set and Get Member Functions 272
9.4 Account Class: Custom Constructors 275
9.5 Software Engineering with Set and Get Member Functions 279
9.6 Account Class with a Balance 280
9.7 Time Class Case Study: Separating Interface from Implementation 283
9.8 Compilation and Linking Process 290
9.9 Class Scope and Accessing Class Members 291
9.10 Access Functions and Utility Functions 292
9.11 Time Class Case Study: Constructors with Default Arguments 292
9.12 Destructors 298
9.13 When Constructors and Destructors Are Called 298
9.14 Time Class Case Study: A Subtle Trap--Returning a Reference or a
Pointer to a private Data Member 302
9.15 Default Assignment Operator 304
9.16 const Objects and const Member Functions 306
9.17 Composition: Objects as Members of Classes 308
9.18 friend Functions and friend Classes 313
9.19 The this Pointer 314
9.20 static Class Members: Classwide Data and Member Functions 320
9.21 Aggregates in C++20 324
9.22 Objects-Natural Case Study: Serialization with JSON 326
9.23 Wrap-Up 333
Chapter 10: OOP: Inheritance and Runtime Polymorphism 335
10.1 Introduction 336
10.2 Base Classes and Derived Classes 339
10.3 Relationship Between Base and Derived Classes 341
10.4 Constructors and Destructors in Derived Classes 349
10.5 Intro to Runtime Polymorphism: Polymorphic Video Game 350
10.6 Relationships Among Objects in an Inheritance Hierarchy 351
10.7 Virtual Functions and Virtual Destructors 357
10.8 Abstract Classes and Pure virtual Functions 362
10.9 Case Study: Payroll System Using Runtime Polymorphism 363
10.10 Runtime Polymorphism, Virtual Functions and Dynamic Binding "Under
the Hood" 373
10.11 Non-Virtual Interface (NVI) Idiom 376
10.12 Program to an Interface, Not an Implementation 383
10.13 Runtime Polymorphism with std::variant and std::visit 391
10.14 Multiple Inheritance 397
10.15 protected Class Members: A Deeper Look 405
10.16 public, protected and private Inheritance 406
10.17 More Runtime Polymorphism Techniques; Compile-Time Polymorphism 408
10.18 Wrap-Up 412
Chapter 11: Operator Overloading, Copy/Move Semantics and Smart Pointers
415
11.1 Introduction 416
11.2 Using the Overloaded Operators of Standard Library Class string 418
11.3 Operator Overloading Fundamentals 423
11.4 (Downplaying) Dynamic Memory Management with new and delete 425
11.5 Modern C++ Dynamic Memory Management: RAII and Smart Pointers 427
11.6 MyArray Case Study: Crafting a Valuable Class with Operator
Overloading 430
11.7 C++20 Three-Way Comparison Operator () 459
11.8 Converting Between Types 462
11.9 explicit Constructors and Conversion Operators 463
11.10 Overloading the Function Call Operator () 466
11.11 Wrap-Up 466
Chapter 12: Exceptions and a Look Forward to Contracts 467
12.1 Introduction 468
12.2 Exception-Handling Flow of Control 471
12.3 Exception Safety Guarantees and noexcept 476
12.4 Rethrowing an Exception 477
12.5 Stack Unwinding and Uncaught Exceptions 479
12.6 When to Use Exception Handling 481
12.7 Constructors, Destructors and Exception Handling 483
12.8 Processing new Failures 487
12.9 Standard Library Exception Hierarchy 490
12.10 C++'s Alternative to the finally Block: Resource Acquisition Is
Initialization (RAII) 493
12.11 Some Libraries Support Both Exceptions and Error Codes 493
12.12 Logging 494
12.13 Looking Ahead to Contracts 495
12.14 Wrap-Up 503
Chapter 13: Standard Library Containers and Iterators 505
13.1 Introduction 506
13.2 Introduction to Containers 508
13.3 Working with Iterators 513
13.4 A Brief Introduction to Algorithms 518
13.5 Sequence Containers 518
13.6 vector Sequence Container 519
13.7 list Sequence Container 526
13.8 deque Sequence Container 531
13.9 Associative Containers 533
13.10 Container Adaptors 543
13.11 bitset Near Container 547
13.12 Optional: A Brief Intro to Big O 549
13.13 Optional: A Brief Intro to Hash Tables 552
13.14 Wrap-Up 553
Chapter 14: Standard Library Algorithms and C++20 Ranges & Views 555
14.1 Introduction 556
14.2 Algorithm Requirements: C++20 Concepts 558
14.3 Lambdas and Algorithms 560
14.4 Algorithms 563
14.5 Function Objects (Functors) 603
14.6 Projections 608
14.7 C++20 Views and Functional-Style Programming 611
14.8 Intro to Parallel Algorithms 617
14.9 Standard Library Algorithm Summary 619
14.10 A Look Ahead to C++23 Ranges 622
14.11 Wrap-Up 623
Chapter 15: Templates, C++20 Concepts and Metaprogramming 625
15.1 Introduction 626
15.2 Custom Class Templates and Compile-Time Polymorphism 629
15.3 C++20 Function Template Enhancements 634
15.4 C++20 Concepts: A First Look 636
15.5 Type Traits 644
15.6 C++20 Concepts: A Deeper Look 648
15.7 Testing C++20 Concepts with static_assert 659
15.8 Creating a Custom Algorithm 661
15.9 Creating a Custom Container and Iterators 663
15.10 Default Arguments for Template Type Parameters 678
15.11 Variable Templates 678
15.12 Variadic Templates and Fold Expressions 679
15.13 Template Metaprogramming 693
15.14 Wrap-Up 705
Chapter 16: C++20 Modules: Large-Scale Development 707
16.1 Introduction 708
16.2 Compilation and Linking Before C++20 710
16.3 Advantages and Goals of Modules 711
16.4 Example: Transitioning to Modules--Header Units 712
16.5 Modules Can Reduce Translation Unit Sizes and Compilation Times 715
16.6 Example: Creating and Using a Module 716
16.7 Global Module Fragment 724
16.8 Separating Interface from Implementation 725
16.9 Partitions 732
16.10 Additional Modules Examples 740
16.11 Migrating Code to Modules 746
16.12 Future of Modules and Modules Tooling 746
16.13 Wrap-Up 748
Chapter 17: Parallel Algorithms and Concurrency: A High-Level View 755
17.1 Introduction 756
17.2 Standard Library Parallel Algorithms (C++17) 759
17.3 Multithreaded Programming 767
17.4 Launching Tasks with std::jthread 771
17.5 ProducerConsumer Relationship: A First Attempt 776
17.6 ProducerConsumer: Synchronizing Access to Shared Mutable Data 783
17.7 ProducerConsumer: Minimizing Waits with a Circular Buffer 795
17.8 Readers and Writers 804
17.9 Cooperatively Canceling jthreads 805
17.10 Launching Tasks with std::async 808
17.11 Thread-Safe, One-Time Initialization 815
17.12 A Brief Introduction to Atomics 816
17.13 Coordinating Threads with C++20 Latches and Barriers 820
17.14 C++20 Semaphores 826
17.15 C++23: A Look to the Future of C++ Concurrency 830
17.16 Wrap-Up 831
Chapter 18: C++20 Coroutines 833
18.1 Introduction 834
18.2 Coroutine Support Libraries 835
18.3 Installing the concurrencpp and generator Libraries 837
18.4 Creating a Generator Coroutine with co_yield and the generator Library
837
18.5 Launching Tasks with concurrencpp 841
18.6 Creating a Coroutine with co_await and co_return 845
18.7 Low-Level Coroutines Concepts 853
18.8 C++23 Coroutines Enhancements 855
18.9 Wrap-Up 856
Appendix A: Operator Precedence and Grouping 857
Appendix B: Character Set 859
Index 861
Online Chapters and Appendices
Chapter 19: Stream I/O and C++20 Text Formatting
Chapter 20: Other Topics and a Look Toward the Future of C++
Appendix C: Number Systems
Appendix D: Preprocessor
Appendix E: Bit Manipulation
Before You Begin xliii
Chapter 1: Intro and Test-Driving Popular, Free C++ Compilers 1
1.1 Introduction 2
1.2 Test-Driving a C++20 Application 4
1.3 Moore's Law, Multi-Core Processors and Concurrent Programming 16
1.4 A Brief Refresher on Object Orientation 17
1.5 Wrap-Up 20
Chapter 2: Intro to C++20 Programming 21
2.1 Introduction 22
2.2 First Program in C++: Displaying a Line of Text 22
2.3 Modifying Our First C++ Program 25
2.4 Another C++ Program: Adding Integers 26
2.5 Arithmetic 30
2.6 Decision Making: Equality and Relational Operators 31
2.7 Objects Natural: Creating and Using Objects of Standard-Library Class
string 35
2.8 Wrap-Up 38
Chapter 3: Control Statements: Part 1 39
3.1 Introduction 40
3.2 Control Structures 40
3.3 if Single-Selection Statement 43
3.4 ifelse Double-Selection Statement 44
3.5 while Iteration Statement 47
3.6 Counter-Controlled Iteration 48
3.7 Sentinel-Controlled Iteration 50
3.8 Nested Control Statements 54
3.9 Compound Assignment Operators 57
3.10 Increment and Decrement Operators 58
3.11 Fundamental Types Are Not Portable 60
3.12 Objects-Natural Case Study: Arbitrary-Sized Integers 61
3.13 C++20: Text Formatting with Function format 65
3.14 Wrap-Up 67
Chapter 4: Control Statements: Part 2 69
4.1 Introduction 70
4.2 Essentials of Counter-Controlled Iteration 70
4.3 for Iteration Statement 71
4.4 Examples Using the for Statement 74
4.5 Application: Summing Even Integers 74
4.6 Application: Compound-Interest Calculations 75
4.7 dowhile Iteration Statement 78
4.8 switch Multiple-Selection Statement 80
4.9 C++17 Selection Statements with Initializers 85
4.10 break and continue Statements 86
4.11 Logical Operators 88
4.12 Confusing the Equality (==) and Assignment (=) Operators 92
4.13 Objects-Natural Case Study: Using the miniz-cpp Library to Write and
Read ZIP files 94
4.14 C++20 Text Formatting with Field Widths and Precisions 98
4.15 Wrap-Up 100
Chapter 5: Functions and an Intro to Function Templates 101
5.1 Introduction 102
5.2 C++ Program Components 103
5.3 Math Library Functions 103
5.4 Function Definitions and Function Prototypes 105
5.5 Order of Evaluation of a Function's Arguments 108
5.6 Function-Prototype and Argument-Coercion Notes 108
5.7 C++ Standard Library Headers 111
5.8 Case Study: Random-Number Generation 113
5.9 Case Study: Game of Chance; Introducing Scoped enums 119
5.10 Scope Rules 124
5.11 Inline Functions 128
5.12 References and Reference Parameters 129
5.13 Default Arguments 132
5.14 Unary Scope Resolution Operator 133
5.15 Function Overloading 134
5.16 Function Templates 137
5.17 Recursion 139
5.18 Example Using Recursion: Fibonacci Series 142
5.19 Recursion vs. Iteration 145
5.20 Lnfylun Lhqtomh Wjtz Qarcv: Qjwazkrplm xzz Xndmwwqhlz 147
5.21 Wrap-Up 150
Chapter 6: arrays, vectors, Ranges and Functional-Style Programming 153
6.1 Introduction 154
6.2 arrays 155
6.3 Declaring arrays 155
6.4 Initializing array Elements in a Loop 155
6.5 Initializing an array with an Initializer List 158
6.6 C++11 Range-Based for and C++20 Range-Based for with Initializer 159
6.7 Calculating array Element Values and an Intro to constexpr 161
6.8 Totaling array Elements 163
6.9 Using a Primitive Bar Chart to Display array Data Graphically 164
6.10 Using array Elements as Counters 165
6.11 Using arrays to Summarize Survey Results 166
6.12 Sorting and Searching arrays 168
6.13 Multidimensional arrays 170
6.14 Intro to Functional-Style Programming 174
6.15 Objects-Natural Case Study: C++ Standard Library Class Template vector
180
6.16 Wrap-Up 187
Chapter 7: (Downplaying) Pointers in Modern C++ 189
7.1 Introduction 190
7.2 Pointer Variable Declarations and Initialization 192
7.3 Pointer Operators 192
7.4 Pass-by-Reference with Pointers 195
7.5 Built-In Arrays 199
7.6 Using C++20 to_array to Convert a Built-In Array to a std::array 201
7.7 Using const with Pointers and the Data Pointed To 202
7.8 sizeof Operator 205
7.9 Pointer Expressions and Pointer Arithmetic 208
7.10 Objects-Natural Case Study: C++20 spans--Views of Contiguous Container
Elements 210
7.11 A Brief Intro to Pointer-Based Strings 216
7.12 Looking Ahead to Other Pointer Topics 220
7.13 Wrap-Up 220
Chapter 8: strings, string_views, Text Files, CSV Files and Regex 221
8.1 Introduction 222
8.2 string Assignment and Concatenation 223
8.3 Comparing strings 225
8.4 Substrings 226
8.5 Swapping strings 227
8.6 string Characteristics 227
8.7 Finding Substrings and Characters in a string 230
8.8 Replacing and Erasing Characters in a string 232
8.9 Inserting Characters into a string 234
8.10 C++11 Numeric Conversions 235
8.11 C++17 string_view 236
8.12 Files and Streams 239
8.13 Creating a Sequential File 240
8.14 Reading Data from a Sequential File 243
8.15 C++14 Reading and Writing Quoted Text 245
8.16 Updating Sequential Files 246
8.17 String Stream Processing 247
8.18 Raw String Literals 249
8.19 Objects-Natural Case Study: Reading and Analyzing a CSV File
Containing Titanic Disaster Data 250
8.20 Objects-Natural Case Study: Intro to Regular Expressions 259
8.21 Wrap-Up 267
Chapter 9: Custom Classes 269
9.1 Introduction 270
9.2 Test-Driving an Account Object 271
9.3 Account Class with a Data Member and Set and Get Member Functions 272
9.4 Account Class: Custom Constructors 275
9.5 Software Engineering with Set and Get Member Functions 279
9.6 Account Class with a Balance 280
9.7 Time Class Case Study: Separating Interface from Implementation 283
9.8 Compilation and Linking Process 290
9.9 Class Scope and Accessing Class Members 291
9.10 Access Functions and Utility Functions 292
9.11 Time Class Case Study: Constructors with Default Arguments 292
9.12 Destructors 298
9.13 When Constructors and Destructors Are Called 298
9.14 Time Class Case Study: A Subtle Trap--Returning a Reference or a
Pointer to a private Data Member 302
9.15 Default Assignment Operator 304
9.16 const Objects and const Member Functions 306
9.17 Composition: Objects as Members of Classes 308
9.18 friend Functions and friend Classes 313
9.19 The this Pointer 314
9.20 static Class Members: Classwide Data and Member Functions 320
9.21 Aggregates in C++20 324
9.22 Objects-Natural Case Study: Serialization with JSON 326
9.23 Wrap-Up 333
Chapter 10: OOP: Inheritance and Runtime Polymorphism 335
10.1 Introduction 336
10.2 Base Classes and Derived Classes 339
10.3 Relationship Between Base and Derived Classes 341
10.4 Constructors and Destructors in Derived Classes 349
10.5 Intro to Runtime Polymorphism: Polymorphic Video Game 350
10.6 Relationships Among Objects in an Inheritance Hierarchy 351
10.7 Virtual Functions and Virtual Destructors 357
10.8 Abstract Classes and Pure virtual Functions 362
10.9 Case Study: Payroll System Using Runtime Polymorphism 363
10.10 Runtime Polymorphism, Virtual Functions and Dynamic Binding "Under
the Hood" 373
10.11 Non-Virtual Interface (NVI) Idiom 376
10.12 Program to an Interface, Not an Implementation 383
10.13 Runtime Polymorphism with std::variant and std::visit 391
10.14 Multiple Inheritance 397
10.15 protected Class Members: A Deeper Look 405
10.16 public, protected and private Inheritance 406
10.17 More Runtime Polymorphism Techniques; Compile-Time Polymorphism 408
10.18 Wrap-Up 412
Chapter 11: Operator Overloading, Copy/Move Semantics and Smart Pointers
415
11.1 Introduction 416
11.2 Using the Overloaded Operators of Standard Library Class string 418
11.3 Operator Overloading Fundamentals 423
11.4 (Downplaying) Dynamic Memory Management with new and delete 425
11.5 Modern C++ Dynamic Memory Management: RAII and Smart Pointers 427
11.6 MyArray Case Study: Crafting a Valuable Class with Operator
Overloading 430
11.7 C++20 Three-Way Comparison Operator () 459
11.8 Converting Between Types 462
11.9 explicit Constructors and Conversion Operators 463
11.10 Overloading the Function Call Operator () 466
11.11 Wrap-Up 466
Chapter 12: Exceptions and a Look Forward to Contracts 467
12.1 Introduction 468
12.2 Exception-Handling Flow of Control 471
12.3 Exception Safety Guarantees and noexcept 476
12.4 Rethrowing an Exception 477
12.5 Stack Unwinding and Uncaught Exceptions 479
12.6 When to Use Exception Handling 481
12.7 Constructors, Destructors and Exception Handling 483
12.8 Processing new Failures 487
12.9 Standard Library Exception Hierarchy 490
12.10 C++'s Alternative to the finally Block: Resource Acquisition Is
Initialization (RAII) 493
12.11 Some Libraries Support Both Exceptions and Error Codes 493
12.12 Logging 494
12.13 Looking Ahead to Contracts 495
12.14 Wrap-Up 503
Chapter 13: Standard Library Containers and Iterators 505
13.1 Introduction 506
13.2 Introduction to Containers 508
13.3 Working with Iterators 513
13.4 A Brief Introduction to Algorithms 518
13.5 Sequence Containers 518
13.6 vector Sequence Container 519
13.7 list Sequence Container 526
13.8 deque Sequence Container 531
13.9 Associative Containers 533
13.10 Container Adaptors 543
13.11 bitset Near Container 547
13.12 Optional: A Brief Intro to Big O 549
13.13 Optional: A Brief Intro to Hash Tables 552
13.14 Wrap-Up 553
Chapter 14: Standard Library Algorithms and C++20 Ranges & Views 555
14.1 Introduction 556
14.2 Algorithm Requirements: C++20 Concepts 558
14.3 Lambdas and Algorithms 560
14.4 Algorithms 563
14.5 Function Objects (Functors) 603
14.6 Projections 608
14.7 C++20 Views and Functional-Style Programming 611
14.8 Intro to Parallel Algorithms 617
14.9 Standard Library Algorithm Summary 619
14.10 A Look Ahead to C++23 Ranges 622
14.11 Wrap-Up 623
Chapter 15: Templates, C++20 Concepts and Metaprogramming 625
15.1 Introduction 626
15.2 Custom Class Templates and Compile-Time Polymorphism 629
15.3 C++20 Function Template Enhancements 634
15.4 C++20 Concepts: A First Look 636
15.5 Type Traits 644
15.6 C++20 Concepts: A Deeper Look 648
15.7 Testing C++20 Concepts with static_assert 659
15.8 Creating a Custom Algorithm 661
15.9 Creating a Custom Container and Iterators 663
15.10 Default Arguments for Template Type Parameters 678
15.11 Variable Templates 678
15.12 Variadic Templates and Fold Expressions 679
15.13 Template Metaprogramming 693
15.14 Wrap-Up 705
Chapter 16: C++20 Modules: Large-Scale Development 707
16.1 Introduction 708
16.2 Compilation and Linking Before C++20 710
16.3 Advantages and Goals of Modules 711
16.4 Example: Transitioning to Modules--Header Units 712
16.5 Modules Can Reduce Translation Unit Sizes and Compilation Times 715
16.6 Example: Creating and Using a Module 716
16.7 Global Module Fragment 724
16.8 Separating Interface from Implementation 725
16.9 Partitions 732
16.10 Additional Modules Examples 740
16.11 Migrating Code to Modules 746
16.12 Future of Modules and Modules Tooling 746
16.13 Wrap-Up 748
Chapter 17: Parallel Algorithms and Concurrency: A High-Level View 755
17.1 Introduction 756
17.2 Standard Library Parallel Algorithms (C++17) 759
17.3 Multithreaded Programming 767
17.4 Launching Tasks with std::jthread 771
17.5 ProducerConsumer Relationship: A First Attempt 776
17.6 ProducerConsumer: Synchronizing Access to Shared Mutable Data 783
17.7 ProducerConsumer: Minimizing Waits with a Circular Buffer 795
17.8 Readers and Writers 804
17.9 Cooperatively Canceling jthreads 805
17.10 Launching Tasks with std::async 808
17.11 Thread-Safe, One-Time Initialization 815
17.12 A Brief Introduction to Atomics 816
17.13 Coordinating Threads with C++20 Latches and Barriers 820
17.14 C++20 Semaphores 826
17.15 C++23: A Look to the Future of C++ Concurrency 830
17.16 Wrap-Up 831
Chapter 18: C++20 Coroutines 833
18.1 Introduction 834
18.2 Coroutine Support Libraries 835
18.3 Installing the concurrencpp and generator Libraries 837
18.4 Creating a Generator Coroutine with co_yield and the generator Library
837
18.5 Launching Tasks with concurrencpp 841
18.6 Creating a Coroutine with co_await and co_return 845
18.7 Low-Level Coroutines Concepts 853
18.8 C++23 Coroutines Enhancements 855
18.9 Wrap-Up 856
Appendix A: Operator Precedence and Grouping 857
Appendix B: Character Set 859
Index 861
Online Chapters and Appendices
Chapter 19: Stream I/O and C++20 Text Formatting
Chapter 20: Other Topics and a Look Toward the Future of C++
Appendix C: Number Systems
Appendix D: Preprocessor
Appendix E: Bit Manipulation