Cover....1
Title Page....2
Copyright Page....5
Contents....8
Preface....24
Before You Begin....54
1 Intro to Computers and C++....60
1.1 Introduction....61
1.2 Hardware....63
1.2.1 Computer Organization....63
1.2.2 Moore’s Law, Multi-Core Processors and Concurrent Programming....66
1.3 Data Hierarchies....69
1.4 Machine Languages, Assembly Languages and High-level Languages....72
1.5 Operating Systems....73
1.6 C and C++....77
1.7 C++ Standard Library and Open-Source Libraries....78
1.8 Other Popular Programming Languages....80
1.9 Introduction to Object Orientation....82
1.10 Simplified View of a C++ Development Environment....84
1.11 Test-Driving a C++20 Application Various Ways....87
1.11.1 Compiling and Running on Windows with Visual Studio Community Edition....88
1.11.2 Compiling and Running with GNU C++ on Linux....91
1.11.3 Compiling and Running with G++ in the GCC Docker Container....93
1.11.4 Compiling and Running with Clang++ in a Docker Container....95
1.11.5 Compiling and Running with Xcode on macOS....96
1.12 Internet, World Wide Web, the Cloud and IoT....100
1.12.1 The Internet: A Network of Networks....100
1.12.2 The World Wide Web: Making the Internet User-Friendly....101
1.12.3 The Cloud....101
1.12.4 The Internet of Things (IoT)....101
1.12.5 Edge Computing....102
1.12.6 Mashups....102
1.13 Metaverse....103
1.13.1 Virtual Reality (VR)....103
1.13.2 Augmented Reality (AR)....104
1.13.3 Mixed Reality....105
1.13.4 Blockchain....105
1.13.5 Bitcoin and Cryptocurrency....105
1.13.6 Ethereum....106
1.13.7 Non-Fungible Tokens (NFTs)....106
1.13.8 Web3....106
1.14 Software Development Technologies....107
1.15 How Big Is Big Data?....108
1.15.1 Big-Data Analytics....111
1.15.2 Data Science and Big Data are Making a Difference: Use Cases....112
1.16 Ai—at the Intersection of Computer Science and Data Science....113
1.16.1 Artificial Intelligence (AI)....113
1.16.2 Artificial General Intelligence (AGI)....114
1.16.3 Artificial Intelligence Milestones....114
1.16.4 Machine Learning....115
1.16.5 Deep Learning....115
1.16.6 Reinforcement Learning....116
1.16.7 Generative AI—ChaptGPT and Dall-E 2....116
1.17 Wrap-Up....118
2 Intro to C++20 Programming....124
2.1 Introduction....125
2.2 First Program in C++: Displaying a Line of Text....125
2.3 Modifying Our First C++ Program....129
2.4 Another C++ Program: Adding Integers....130
2.5 Memory Concepts....134
2.6 Arithmetic....135
2.7 Decision Making: Equality and Relational Operators....138
2.8 Objects Natural Case Study: Creating and Using Objects of Standard-library Class String....142
2.9 Wrap-Up....145
3 Algorithm Development and Control Statements: Part 1....152
3.1 Introduction....153
3.2 Algorithms....154
3.3 Pseudocode....154
3.4 Control Structures....155
3.4.1 Sequence Structure....156
3.4.2 Selection Statements....157
3.4.3 Iteration Statements....157
3.4.4 Summary of Control Statements....158
3.5 If Single-selection Statement....159
3.6 If...else Double-Selection Statement....160
3.6.1 Nested If...else Statements....161
3.6.2 Blocks....163
3.6.3 Conditional Operator (?:)....163
37 While Iteration Statement....164
3.8 Formulating Algorithms: Counter-Controlled Iteration....166
3.8.1 Pseudocode Algorithm with Counter-Controlled Iteration....166
3.8.2 Implementing Counter-Controlled Iteration....167
3.8.3 Integer Division and Truncation....169
3.8.4 Arithmetic Overflow....169
3.8.5 Input Validation....169
3.9 Formulating Algorithms: Sentinel-Controlled Iteration....170
3.9.1 Top-Down, Stepwise Refinement: the Top and First Refinement....171
3.9.2 Proceeding to the Second Refinement....171
3.9.3 Implementing Sentinel-Controlled Iteration....173
3.9.4 Mixed-Type Expressions and Implicit Type Promotions....175
3.9.5 Formatting Floating-Point Numbers....176
3.10 Formulating Algorithms: Nested Control Statements....177
3.10.1 Problem Statement....177
3.10.2 Top-Down, Stepwise Refinement: Pseudocode Representation of the Top....178
3.10.3 Top-Down, Stepwise Refinement: First Refinement....178
3.10.4 Top-Down, Stepwise Refinement: Second Refinement....179
3.10.5 Complete Second Refinement of the Pseudocode....179
3.10.6 Implementing the Program....180
3.10.7 Preventing Narrowing Conversions with Braced Initialization....182
3.11 Compound Assignment Operators....184
3.12 Increment and Decrement Operators....184
3.13 Fundamental Types Are Not Portable....187
3.14 Objects Natural Case Study: Super-Sized Integers....188
3.15 Wrap-Up....193
4 Control Statements, Part 2....204
4.1 Introduction....205
4.2 Essentials of Counter-Controlled Iteration....205
4.3 for Iteration Statement....207
4.4 Examples Using the for Statement....210
4.5 Application: Summing Even Integers; Introducing C++20 Text Formatting....211
4.6 Application: Compound-Interest Calculations; Introducing Format Specifiers....212
4.7 Do...while Iteration Statement....217
4.8 Switch Multiple-Selection Statement....218
4.9 Selection Statements with Initializers....224
4.10 Break and Continue Ststement....226
4.11 Logical Operators....228
4.11.1 Logical AND (&&) Operator....228
4.11.2 Logical OR (||) Operator....229
4.11.3 Short-Circuit Evaluation....229
4.11.4 Logical Negation (!) Operator....230
4.11.5 Example: Producing Logical-Operator Truth Tables....230
4.12 Confusing the Equality (==) and Assignment (=) Operators....232
4.13 Structured-Programming Summary....234
4.14 Objects Natural Case Study: Precise Monetary Calculations with the Boost Multiprecision Library....239
4.15 Wrap-Up....242
5 Functions and an Intro to Function Templates....248
5.1 Introduction....249
5.2 C++ Program Components....250
5.3 Math Library Functions....251
5.4 Function Definitions and Function Prototypes....253
5.5 Order of Evaluation of a Function’s Arguments....256
5.6 Function-Prototype and Argument-Coercion Notes....256
5.6.1 Function Signatures and Function Prototypes....257
5.6.2 Argument Coercion....257
5.6.3 Argument-Promotion Rules and Implicit Conversions....257
5.7 C++ Standard Library Headers....259
5.8 Case Study: Random-Number Generation....262
5.8.1 Rolling a Six-Sided Die....263
5.8.2 Rolling a Six-Sided Die 60,000,000 Times....264
5.8.3 Seeding the Random-Number Generator....266
5.8.4 Seeding the Random-Number Generator with random_device....267
5.9 Case Study: Game of Chance; Introducing Scoped enums....268
5.10 Function-call Stack and Activation Records....273
5.11 Inline Functions....276
5.12 References and Reference Parameters....278
5.13 Default Arguments....281
5.14 Function Overloading....283
5.15 Function Templates....286
5.16 Recursion....288
5.16.1 Factorials....289
5.16.2 Recursive Factorial Example....289
5.16.3 Recursive Fibonacci Series Example....293
5.16.4 Recursion vs. Iteration....296
5.17 Scope Rules....298
5.18 Unary Scope Resolution Operator....303
5.19 Lnfylun Lhqtomh Wjtz Qarcv: Qjwazkrplm xzz Xndmwwqhlz....304
5.20 Wrap-Up....307
6 Arrays, Vectors, Ranges and Functional-Style Programming....318
6.1 Introduction....319
6.2 Arrays....320
6.3 Declaring array....321
6.4 Initializing array Elements in a Loop....321
6.5 Initializing an array with an Initializer List....324
6.6 Range-Based for Statement....326
6.7 Calculating array Element Values and an Intro to constexpr....328
6.8 Totaling array Elements....330
6.9 Using a Primitive Bar Chart to Display array Data Graphically....331
6.10 Using array Elements as Counters....333
6.11 Using arrays to Summarize Survey Results....334
6.12 Sorting and Searching arrays....336
6.13 Multidimensional arrays....338
6.14 Intro to Functional-Style Programming....342
6.14.1 What vs. How....342
6.14.2 Passing Functions as Arguments to Other Functions: Introducing Lambda Expressions....343
6.14.3 Filter, Map and Reduce: Intro to C++20’s Ranges Library....345
6.15 Objects-Natural Case Study: C++ Standard Library Class Template Vector....349
6.16 Wrap-Up....356
7 (Downplaying) Pointers in Modern C++....368
7.1 Introduction....369
7.2 Pointer Variable Declarations and Initialization....371
7.2.1 Declaring Pointers....371
7.2.2 Initializing Pointers....371
7.2.3 Null Pointers....371
7.3 Pointer Operators....372
7.3.1 Address (&) Operator....372
7.3.2 Indirection (*) Operator....373
7.3.3 Using the Address (&) and Indirection (*) Operators....374
7.4 Pass-by-Reference with Pointers....375
7.5 Built-in Arrays....379
7.5.1 Declaring and Accessing a Built-in Array....379
7.5.2 Initializing Built-in Arrays....380
7.5.3 Passing Built-in Arrays to Functions....381
7.5.4 Declaring Built-in Array Parameters....381
7.5.5 Standard Library Functions Begin and End....382
7.5.6 Built-in Array Limitations....382
7.6 Using C++20 To_array to Convert a Built-in Array to a Std::array....383
7.7 Using Const with Pointers and the Data Pointed To....384
7.7.1 Using a Nonconstant Pointer to Nonconstant Data....385
7.7.2 Using a Nonconstant Pointer to Constant Data....385
7.7.3 Using a Constant Pointer to Nonconstant Data....386
7.7.4 Using a Constant Pointer to Constant Data....386
7.8 Sizeof Operator....388
7.9 Pointer Expressions and Pointer Arithmetic....390
7.9.1 Adding Integers to and Subtracting Integers from Pointers....391
7.9.2 Subtracting One Pointer from Another....392
7.9.3 Pointer Assignment....392
7.9.4 Cannot Dereference a void* Pointer....392
7.9.5 Comparing Pointers....393
7.10 Objects-Natural Case Study: C++20 spans—views of Contiguous Container Elements....393
7.11 A Brief Intro to Pointer-Based Strings....399
7.11.1 Command-Line Arguments....400
7.11.2 Revisiting C++20’s to_array Function....401
7.12 Looking Ahead to Other Pointer Topics....403
7.13 Wrap-Up....403
8 Strings, String_views, Text Files, CSV Files and Regex....416
8.1 Introduction....417
8.2 String Assignment and Concatenation....418
8.3 Comparing Strings....420
8.4 Substrings....422
8.5 Swapping strings....423
8.6 string Characteristics....424
8.7 Finding Substrings and Characters in a string....426
8.8 Replacing and Erasing Characters in a string....429
8.9 Inserting Characters into a string....431
8.10 Numeric Conversions....432
8.11 string_view....433
8.12 Files and Streams....436
8.13 Creating a Sequential File....437
8.14 Reading Data from a Sequential File....440
8.15 Reading and Writing Quoted Text....443
8.16 Updating Sequential Files....444
8.17 String Stream Processing....445
8.18 Raw String Literals....448
8.19 Objects-Natural Data Science Case Study: Reading and Analyzing a CSV File Containing Titanic Disaster Data....449
8.19.1 Using rapidcsv to Read the Contents of a CSV File....449
8.19.2 Reading and Analyzing the Titanic Disaster Dataset....451
8.20 Objects-Natural Data Science Case Study: Intro to Regular Expressions....458
8.20.1 Matching Complete Strings to Patterns....459
8.20.2 Replacing Substrings....464
8.20.3 Searching for Matches....464
8.21 Wrap-Up....467
9 Custom Classes....490
9.1 Introduction1....491
9.2 Test-Driving an Account Object....492
9.3 Account Class with a Data Member and Set and Get Member Functions....494
9.3.1 Class Definition....494
9.3.2 Access Specifiers private and public....496
9.4 Account Class: Custom Constructors....497
9.5 Software Engineering with Set and Get Member Functions....501
9.6 Account Class with a Balance....503
9.7 Time Class Case Study: Separating Interface from Implementation....506
9.7.1 Interface of a Class....507
9.7.2 Separating the Interface from the Implementation....507
9.7.3 Class Definition....508
9.7.4 Member Functions....509
9.7.5 Including the Class Header in the Source-Code File....509
9.7.6 Scope Resolution Operator (::)....510
9.7.7 Member Function setTime and Throwing Exceptions....510
9.7.8 Member Functions to24HourString and to12HourString....510
9.7.9 Implicitly Inlining Member Functions....511
9.7.10 Member Functions vs. Global Functions....511
9.7.11 Using Class Time....511
9.7.12 Object Size....513
9.8 Compilation and Linking Process....513
9.9 Class Scope and Accessing Class Members....514
9.10 Access Functions and Utility Functions....515
9.11 Time Class Case Study: Constructors with Default Arguments....516
9.11.1 Class Time....516
9.11.2 Overloaded Constructors and Delegating Constructors....521
9.12 Destructors....522
9.13 When Constructors and Destructors Are Called....523
9.14 Time Class Case Study: a Subtle Trap — Returning a Reference or a Pointer to a private Data Member....527
9.15 Default Assignment Operator....529
9.16 Const Objects and Const Member Functions....531
9.17 Composition: Objects as Members of Classes....533
9.18 Friend Functions and friend Classes....539
9.19 The this Pointer....541
9.19.1 Implicitly and Explicitly Using the This Pointer to Access an Object’s Data Members....541
9.19.2 Using the this Pointer to Enable Cascaded Function Calls....543
9.20 static Class Members: Classwide Data and Member Functions....546
9.21 Aggregates in C++20....551
9.21.1 Initializing an Aggregate....552
9.21.2 C++20 Designated Initializers....552
9.22 Concluding Our Objects Natural Case Study Track: Studying the Vigenère Secret-key Cipher Implementation....553
9.23 Wrap-Up....566
10 Oop: Inheritance and Runtime Polymorphism....588
10.1 Introduction....589
10.2 Base Classes and Derived Classes....591
10.2.1 Communitymember Class Hierarchy....592
10.2.2 Shape Class Hierarchy and public Inheritance....593
10.3 Relationship Between Base and Derived Classes....594
10.3.1 Creating and Using a Salariedemployee Class....594
10.3.2 Creating a SalariedEmployee– SalariedCommissionemployee Inheritance Hierarchy....597
10.4 Constructors and Destructors in Derived Classes....603
10.5 Intro to Runtime Polymorphism: Polymorphic Video Game....604
10.6 Relationships Among Objects in an Inheritance Hierarchy....605
10.6.1 Invoking Base-class Functions from Derived-class Objects....606
10.6.2 Aiming Derived-Class Pointers at Base-class Objects....608
10.6.3 Derived-Class Member-function Calls Via Base-class Pointers....609
10.7 Virtual Functions and Virtual Destructors....610
10.7.1 Why Virtual Functions Are Useful....610
10.7.2 Declaring virtual Functions....611
10.7.3 Invoking a virtual Function....612
10.7.4 virtual Functions in the Salariedemployee Hierarchy....612
10.7.5 virtual Destructors....616
10.7.6 final Member Functions and Classes....616
10.8 Abstract Classes and Pure virtual Functions....617
10.8.1 Pure virtual Functions....618
10.8.2 Device Drivers: Polymorphism in Operating Systems....618
10.9 Case Study: Payroll System Using Runtime Polymorphism....619
10.9.1 Creating Abstract Base Class Employee....620
10.9.2 Creating Concrete Derived Class SalariedEmployee....622
10.9.3 Creating Concrete Derived Class CommissionEmployee....624
10.9.4 Demonstrating Runtime Polymorphic Processing....626
10.10 Runtime Polymorphism, Virtual Functions and Dynamic Binding “under the Hood”....629
10.11 Program to an Interface, Not an Implementation19....632
10.11.1 Rethinking the Employee Hierarchy—CompensationModel Interface....634
10.11.2 Class Employee....634
10.11.3 Compensationmodel Implementations....636
10.11.4 Testing the New Hierarchy....638
10.11.5 Dependency Injection Design Benefits....639
10.12 Wrap-Up....640
11 Operator Overloading, Copy/Move Semantics and Smart Pointers....646
11.1 Introduction....647
11.2 Using the Overloaded Operators of Standard Library Class string....649
11.3 Operator Overloading Fundamentals....655
11.3.1 Operator Overloading Is Not Automatic....655
11.3.2 Operators That Cannot Be Overloaded....655
11.3.3 Operators That You Do Not Have to Overload....655
11.3.4 Rules and Restrictions on Operator Overloading....656
11.4 (Downplaying) Dynamic Memory Management with New and delete....657
11.5 Modern C++ Dynamic Memory Management: RAII and Smart Pointers....659
11.5.1 Smart Pointers....660
11.5.2 Demonstrating unique_ptr....660
11.5.3 unique_ptr Ownership....662
11.5.4 unique_ptr to a Built-In Array....662
11.6 Myarray Case Study: Crafting a Valuable Class with Operator Overloading....663
11.6.1 Special Member Functions....664
11.6.2 Using Class MyArray....665
11.6.3 MyArray Class Definition....674
11.6.4 Constructor That Specifies a MyArray's Size....676
11.6.5 Passing a Braced Initializer to a Constructor....677
11.6.6 Copy Constructor and Copy Assignment Operator....678
11.6.7 Move Constructor and Move Assignment Operator....681
11.6.8 Destructor....685
11.6.9 toString and size Functions....685
11.6.10 Overloading the Equality (==) and Inequality (!=) Operators....686
11.6.11 Overloading the Subscript ([]) Operator....688
11.6.12 Overloading the Unary bool Conversion Operator....689
11.6.13 Overloading the Preincrement Operator....690
11.6.14 Overloading the Postincrement Operator....691
11.6.15 Overloading the Addition Assignment Operator (+=)....692
11.6.16 Overloading the Binary Stream Extraction (>>) and Stream Insertion (<<) Operators....692
11.7 C++20 Three-Way Comparison Operator (<=>)....695
11.6 MyArray Case Study: Crafting a Valuable Class with Operator Overloading....663
11.7 C++20 Three-Way Comparison Operator (<=>)....695
11.8 Converting Between Types....699
11.9 explicit Constructors and Conversion Operators....700
11.10 Overloading the Function Call Operator ()....703
11.11 Wrap-Up....703
12 Exceptions and a Look Forward to Contracts....712
12.1 Introduction....713
12.2 Exception-Handling Flow of Control....717
12.2.1 Defining a Custom Exception Class....717
12.2.2 Demonstrating Exception Handling....718
12.2.3 Enclosing Code in a try Block....719
12.2.4 Defining a catch Handler for DivideByZeroExceptions....720
12.2.5 Termination Model of Exception Handling....721
12.2.6 Flow of Control When the User Enters a Nonzero Denominator....722
12.2.7 Flow of Control When the User Enters a Zero Denominator....722
12.3 Exception Safety Guarantees and noexcept....723
12.4 Rethrowing an Exception....724
12.5 Stack Unwinding and Uncaught Exceptions....726
12.6 When to Use Exception Handling....728
12.6.1 assert Macro....730
12.6.2 Failing Fast....730
12.7 Constructors, Destructors and Exception Handling....731
12.7.1 Throwing Exceptions from Constructors....731
12.7.2 Catching Exceptions in Constructors via Function try Blocks....732
12.7.3 Exceptions and Destructors: Revisiting noexcept (false)....734
12.8 Processing new Failures....735
12.8.1 new Throwing bad_alloc on Failure....736
12.8.2 new Returning nullptr on Failure....737
12.8.3 Handling new Failures Using Function set_new_handler....738
12.9 Standard Library Exception Hierarchy....739
12.10 C++’s Alternative to the finally Block: Resource Acquisition Is Initialization (RAII)....742
12.11 Some Libraries Support Both Exceptions and Error Codes....742
12.12 Logging....744
12.13 Looking Ahead to Contracts49....744
12.14 Wrap-Up....753
13 Data Structures: Standard Library Containers and Iterators....756
13.1 Introduction....757
13.2 a Brief Intro to Big O....759
13.3 A Brief Intro to Hash Tables....762
13.4 Introduction to Containers4....763
13.4.1 Common Nested Types in Sequence and Associative Containers....765
13.4.2 Common Container Member and Non-Member Functions....766
13.4.3 Requirements for Container Elements....769
13.5 Working with Iterators....769
13.5.1 Using istream_iterator for Input and ostream_iterator for Output....769
13.5.2 Iterator Categories....771
13.5.3 Container Support for Iterators....771
13.5.4 Predefined Iterator Type Names....772
13.5.5 Iterator Operators....772
13.6 A Brief Introduction to Algorithms....773
13.7 Sequence Containers....774
13.8 Vector Sequence Container....774
13.9 list Sequence Container....782
13.10 deque Sequence Container....787
13.11 Associative Containers....789
13.11.1 multiset Associative Container....789
13.11.2 set Associative Container....793
13.11.3 multimap Associative Container....795
13.11.4 map Associative Container....797
13.12 Container Adaptors....798
13.12.1 stack Adaptor....799
13.12.2 queue Adaptor....801
13.12.3 priority_queue Adaptor....802
13.13 bitset Near Container....803
13.14 Wrap-Up....805
14 Standard Library Algorithms and C++20 Ranges & Views....832
14.1 Introduction....833
14.2 Algorithm Requirements: C++20 Concepts....835
14.3 Lambdas and Algorithms....837
14.4 Algorithms....840
14.4.1 fill, fill_n, generate and generate_n....840
14.4.2 equal, mismatch and lexicographical_compare....842
14.4.3 remove, remove_if, remove_copy and remove_copy_if....845
14.4.4 replace, replace_if, replace_copy and replace_copy_if....849
14.4.5 Shuffling, Counting, and Minimum and Maximum Element Algorithms....851
14.4.6 Searching and Sorting Algorithms....855
14.4.7 Swap, iter_swap and swap_ranges....859
14.4.8 copy_backward, merge, unique, reverse, copy_if and copy_n....861
14.4.9 inplace_merge, unique_copy and reverse_copy....864
14.4.10 Set Operations....866
14.4.11 lower_bound, upper_bound and equal_range....869
14.4.12 min, max and minmax....871
14.4.13 Algorithms gcd, lcm, iota, reduce and partial_sum from Header ....872
14.4.14 Heapsort and Priority Queues....875
14.5 Function Objects (Functors)....880
14.6 Projections....884
14.7 C++20 Views and Functional-Style Programming....887
14.7.1 Range Adaptors....887
14.7.2 Working with Range Adaptors and Views....889
14.8 Intro to Parallel Algorithms....893
14.9 Standard Library Algorithm Summary....895
14.10 Future Ranges Enhancements....898
14.11 Wrap-Up....899
15 Templates, C++20 Concepts and Metaprogramming....904
15.1 Introduction....905
15.2 Custom Class Templates and Compile-Time Polymorphism....908
15.3 C++20 Function Template Enhancements....913
15.3.1 C++20 Abbreviated Function Templates....913
15.3.2 C++20 Templated Lambdas....915
15.4 C++20 Concepts: A First Look....915
15.4.1 Unconstrained Function Template muliply....916
15.4.2 Constrained Function Template with a C++20 Concepts Requires Clause....919
15.4.3 C++20 Predefined Concepts....921
15.5 Type Traits....923
15.6 C++20 Concepts: A Deeper Look....927
15.6.1 Creating a Custom Concept....927
15.6.2 Using a Concept....928
15.6.3 Using Concepts in Abbreviated Function Templates....929
15.6.4 Concept-based Overloading....930
15.6.5 requires Expressions....933
15.6.6 C++20 Exposition-Only Concepts....936
15.6.7 Techniques Before C++20 Concepts: SFINAE and Tag Dispatch....937
15.7 Testing C++20 Concepts with static_assert....938
15.8 Creating a Custom Algorithm....940
15.9 Creating a Custom Container and Iterators....942
15.9.1 Class Template ConstIterator....944
15.9.2 Class Template Iterator....947
15.9.3 Class Template MyArray....949
15.9.4 MyArray Deduction Guide for Braced Initialization....952
15.9.5 Using MyArray with Std::ranges Algorithms....953
15.10 Default Arguments for Template Type Parameters....957
15.11 Variable Templates....957
15.12 Variadic Templates and Fold Expressions....958
15.12.1 tuple Variadic Class Template....958
15.12.2 Variadic Function Templates and an Intro to Fold Expressions....961
15.12.3 Types of Fold Expressions....965
15.12.4 How Unary Fold Expressions Apply Their Operators....965
15.12.5 How Binary-Fold Expressions Apply Their Operators....968
15.12.6 Using the Comma Operator to Repeatedly Perform an Operation....969
15.12.7 Constraining Parameter Pack Elements to the Same Type....970
15.13 Template Metaprogramming....972
15.13.1 C++ Templates Are Turing Complete....973
15.13.2 Computing Values at Compile-Time....973
15.13.3 Conditional Compilation with Template Metaprogramming and constexpr if....978
15.13.4 Type Metafunctions....980
15.14 Wrap-Up....984
16 C++20 Modules: Large-Scale Development....992
16.1 Introduction....993
16.2 Compilation and Linking Before C++20....995
16.3 Advantages and Goals of Modules....996
16.4 Example: Transitioning to Modules—Header Units....997
16.5 Modules Can Reduce Translation Unit Sizes and Compilation Times....1000
16.6 Example: Creating and Using a Module....1001
16.6.1 module Declaration for a Module Interface Unit....1002
16.6.2 Exporting a Declaration....1004
16.6.3 Exporting a Group of Declarations....1004
16.6.4 Exporting a namespace....1004
16.6.5 Exporting a namespace Member....1005
16.6.6 Importing a Module to Use Its Exported Declarations....1005
16.6.7 Example: Attempting to Access Non-Exported Module Contents....1007
16.7 Global Module Fragment....1010
16.8 Separating Interface from Implementation....1010
16.8.1 Example: Module Implementation Units....1010
16.8.2 Example: Modularizing a Class....1013
16.8.3 :private Module Fragment....1017
16.9 Partitions....1017
16.9.1 Example: Module Interface Partition Units....1018
16.9.2 Module Implementation Partition Units....1021
16.9.3 Example: “Submodules” vs. Partitions....1021
16.10 Additional Modules Examples....1026
16.10.1 Example: Importing the C++ Standard Library as Modules....1026
16.10.2 Example: Cyclic Dependencies Are Not Allowed....1028
16.10.3 Example: imports Are Not Transitive....1029
16.10.4 Example: Visibility vs. Reachability....1030
16.11 Migrating Code to Modules....1031
16.12 Future of Modules and Modules Tooling....1032
16.13 Wrap-Up....1034
17 Parallel Algorithms and Concurrency: a High-Level View....1046
17.1 Introduction....1047
17.2 Standard Library Parallel Algorithms....1050
17.2.1 Example: Profiling Sequential and Parallel Sorting Algorithms....1050
17.2.2 When to Use Parallel Algorithms....1053
17.2.3 Execution Policies....1054
17.2.4 Example: Profiling Parallel and Vectorized Operations....1055
17.2.5 Additional Parallel Algorithm Notes....1057
17.3 Multithreaded Programming....1058
17.3.1 Thread States and the Thread Life Cycle....1058
17.3.2 Deadlock and Indefinite Postponement....1060
17.4 Launching Tasks with std::jthread....1062
17.4.1 Defining a Task to Perform in a Thread....1062
17.4.2 Executing a Task in a jthread....1064
17.4.3 How jthread Fixes Thread....1066
17.5 Producer–Consumer Relationship: a First Attempt....1067
17.6 Producer–Consumer: Synchronizing Access to Shared Mutable Data....1074
17.6.1 Class SynchronizedBuffer: Mutexes, Locks and Condition Variables....1076
17.6.2 Testing SynchronizedBuffer....1082
17.7 Producer–Consumer: Minimizing Waits with a Circular Buffer....1086
17.8 Readers and Writers....1095
17.9 Cooperatively Canceling jthreads....1096
17.10 Launching Tasks with std::async....1099
17.11 Thread-Safe, One-Time Initialization....1106
17.12 A Brief Introduction to Atomics....1107
17.13 Coordinating Threads with C++20 Latches and Barriers....1111
17.13.1 C++20 std::latch....1111
17.13.2 C++20 std::barrier....1114
17.14 C++20 Semaphores....1117
17.15 C++23: A Look to the Future of C++ Concurrency....1121
17.15.1 Parallel Ranges Algorithms....1121
17.15.2 Concurrent Containers....1121
17.15.3 Other Concurrency-Related Proposals....1122
17.16 Wrap-Up....1122
18 C++20 Coroutines....1132
18.1 Introduction....1133
18.2 Coroutine Support Libraries....1134
18.3 Installing the concurrencpp and generator Libraries....1136
18.4 Creating a Generator Coroutine with co_yield and the generator Library....1136
18.5 Launching Tasks with concurrencpp....1140
18.6 Creating a Coroutine with co_await and co_return....1144
18.7 Low-Level Coroutines Concepts....1152
18.8 Future Coroutines Enhancements....1155
18.9 Wrap-Up....1155
19 Stream I/O & C++20 Text Formatting....1160
19.1 Introduction....1161
19.2 Streams....1161
19.2.1 Classic Streams vs. Standard Streams....1162
19.2.2 iostream Library Headers....1162
19.2.3 Stream Input/Output Classes and Objects....1162
19.3 Stream Output....1163
19.3.1 Output of char* Variables....1164
19.3.2 Character Output Using Member Function put....1165
19.4 Stream Input....1165
19.4.1 get and getline Member Functions....1165
19.4.2 istream Member Functions peek, putback and ignore....1168
19.5 Unformatted I/O Using read, write and gcount....1169
19.6 Stream Manipulators....1170
19.6.1 Integral Stream Base: dec, oct, hex and setbase....1171
19.6.2 Floating-Point Precision (setprecision, precision)....1171
19.6.3 Field Width (width, setw)....1173
19.6.4 User-Defined Output Stream Manipulators....1174
19.6.5 Trailing Zeros and Decimal Points (showpoint)....1175
19.6.6 Alignment (left, Right and Internal)....1176
19.6.7 Padding (fill, Setfill)....1177
19.6.8 Integral Stream Base (dec, oct, hex, showbase)....1178
19.6.9 Floating-Point Numbers; Scientific and Fixed Notation (scientific, fixed)....1179
19.6.10 Uppercase/lowercase Control (uppercase)....1180
19.6.11 Specifying Boolean Format (boolalpha)....1181
19.6.12 Setting and Resetting the Format State via Member Function Flags....1182
19.7 Stream Error States....1183
19.8 Tying an Output Stream to an Input Stream....1186
19.9 C++20 Text Formatting....1186
19.9.1 C++20 std::format Presentation Types....1187
19.9.2 C++20 std::format Field Widths and Alignment....1189
19.9.3 C++20 std::format Numeric Formatting....1190
19.9.4 C++20 std::format Field Width and Precision Placeholders....1191
19.10 Wrap-Up....1192
20 Other Topics and a Look Toward the Future of C++....1200
20.1 Introduction....1201
20.2 shared_ptr and Weak_ptr Smart Pointers....1202
20.2.1 Reference Counted shared_ptr....1202
20.2.2 weak_ptr: shared_ptr Observer....1206
20.3 Runtime Polymorphism with std::variant and std::visit....1213
20.4 Protected Class Members: A Deeper Look....1219
20.5 Non-Virtual Interface (NVI) Idiom....1220
20.6 Inheriting Base-Class Constructors....1227
20.7 Multiple Inheritance....1228
20.7.1 Diamond Inheritance....1233
20.7.2 Eliminating Duplicate Subobjects with virtual Base-Class Inheritance....1235
20.8 public, protected and private Inheritance....1236
20.9 namespaces: A Deeper Look....1238
20.9.1 Defining namespaces....1239
20.9.2 Accessing namespace Members with Qualified Names....1240
20.9.3 using Directives Should Not Be Placed in Headers....1240
20.9.4 Nested namespaces....1240
20.9.5 Aliases for namespace Names....1240
20.10 Storage Classes and Storage Duration....1241
20.10.1 Storage Duration....1241
20.10.2 Local Variables and Automatic Storage Duration....1241
20.10.3 Static Storage Duration....1242
20.10.4 mutable Class Members....1243
20.11 Operator Keywords....1244
20.12 decltype Operator....1245
20.13 Trailing Return Types for Functions....1246
20.14 [[nodiscard]] Attribute....1246
20.15 Some Key C++23 Features....1248
20.16 Wrap-Up....1253
21 Computer Science Thinking: Searching, Sorting and Big O....1256
21.1 Introduction....1257
21.2 Efficiency of Algorithms: Big O....1258
21.2.1 O(1) Algorithms....1258
21.2.2 O(n) Algorithms....1258
21.2.3 O(n2) Algorithms....1259
21.3 Linear Search....1260
21.3.1 Implementation....1260
21.3.2 Efficiency of Linear Search....1261
21.4 Binary Search....1262
21.4.1 Implementation....1262
21.4.2 Efficiency of Binary Search....1266
21.5 Insertion Sort....1267
21.5.1 Implementation....1268
21.5.2 Efficiency of Insertion Sort....1269
21.6 Selection Sort....1269
21.6.1 Implementation....1270
21.6.2 Efficiency of Selection Sort....1272
21.7 Merge Sort (a Recursive Implementation)....1272
21.7.1 Implementation....1273
21.7.2 Efficiency of Merge Sort....1278
21.7.3 Summarizing Various Algorithms’ Big O Notations....1278
21.8 Wrap-Up....1280
Glossary....1284
Index....1299
C++ How to Program: An Objects-Natural Approach is a code-intensive, modular introduction to C++ programming. The Deitel live-code approach presents concepts using full working programs rather than code snippets so that you can immediately start to run programs as you read. Interesting, entertaining, and challenging examples, exercises, and projects help you see how what you're learning applies to real-world scenarios.
The 11th Edition presents new features of C++ 20 and even more hands-on application opportunities. Hundreds of new self-checks let you test your code and understanding of key concepts. New case studies and exercises focus on security, data science, ethics, and more.