Copyright....5
Table of Contents....8
Foreword....14
Preface....16
Who Is This Book For?....16
Structure of This Book....17
Conventions Used in This Book....18
Using Code Examples....19
OReilly Online Learning....20
How to Contact Us....20
Acknowledgments....20
Chapter 1. Hello, World!....22
Installing Tools....24
Linux and macOS....24
Windows....25
Using Your Tools....25
Running Your Program....28
Writing to the Screen....28
Using println....29
Troubleshooting....31
Using cout....32
Understanding println and cout in Depth....33
Conclusion....35
Chapter 2. Variables and Keyboard Input....36
Declaring Variables....36
Character Input....37
Detecting Input Problems....39
Input of Real Numbers....39
Detecting Problems with Numbers....40
Detecting More General Problems....43
A Function for Input with Some Tests....45
Starting with a Failing Test....45
Breaking Your Code into Functions....47
Starting with a Failing Test, Again....48
Testing Bad Input....50
Refactor....52
Calling Your New Function from main....53
Understanding Variables, std::cin, and Functions in Depth....54
Clearing Input Errors....55
More on Functions....58
Conclusion....62
Chapter 3. Exceptions and Expectations....64
Exceptions....64
Throwing Exceptions....67
Trying and Catching....68
Handling Exceptions with a trycatch Block....68
Expectations....70
Understanding Exceptions and Expectations in More Depth....72
Other Exception Types....73
Position of catch Blocks....75
Expected Without a Value....76
Conclusion....77
Chapter 4. Using Loops, Arrays, and Vectors....80
Input of Several Numbers Using a Loop....80
A while Loop....81
Using an Array....83
Displaying and Using the Numbers....89
Using a Vector....92
Adding More Elements to a Vector....93
A Few Other Container Functions....95
Getting Several Numbers in a Vector....96
Understanding Sequential Containers in More Depth....97
Initializing Containers with an Initializer List....97
What Happens When You Add to a Vector....98
What Happens When You Delete from a Vector....99
Initializing a Vector with a Fixed Value....100
Other Sequential Containers....101
Conclusion....101
Chapter 5. Using Standard Library Algorithms....104
Getting Several Numbers Into a Vector (Again)....104
Analyzing Your Numbers Using Algorithms....110
Using Predicates in Algorithms....112
Using Iterators in Algorithms....115
The Old Way to Remove Items....116
Finding an Average with an Algorithm....120
Understanding Algorithms in More Depth....124
Using for Loops....124
Binary Operators and Predicates....127
More on Iterators....128
Conclusion....128
Chapter 6. Lambdas and the Ranges Library....130
Removing Negative Numbers Using a Lambda....130
Using a Lambda to Vary Behavior via std::function....132
Filtering Out Negative Numbers Using the Ranges View....138
Using Lambda Captures for Fun and Profit....141
Understanding Lambdas and Views in More Depth....144
Lambda Captures by Value....144
Lambda Captures by Reference....148
Composing Views....149
Lazy Views....153
Conclusion....154
Chapter 7. Random Numbers....156
Generating Random Numbers....157
Writing an Overload for a Function....160
Building a Trading Game....164
Understanding Code with Random Numbers (and Vectors) in Depth....167
Using a Normal Distribution....168
Considerations for Code That Uses Random Numbers....171
Creating and Filling Vectors....175
Conclusion....177
Chapter 8. Working with Files....180
Writing to a File....180
Detecting and Reporting Problems....182
Using the Filesystem Library....184
Reading from a File....186
Understanding Files in Depth....188
Different File Modes....188
Bitwise Operators and Bitmasks....190
Reading Previous Prices....192
Conclusion....196
Chapter 9. Strings and Formatting....198
C-Style String Literals and Characters....198
Providing Arguments to main....200
Creating and Manipulating a std::string....201
Other Ways to Create a std::string....202
More std::string Functions....203
String Views....205
Formatting and More on std::println....208
std::format and Format Specifications....209
An Improved Trading Game....211
Understanding Strings in Depth....216
Joining std::strings Efficiently....216
Using std::println to Save to a File....217
Conclusion....219
Chapter 10. Classes: Member Variables and Member Functions....222
A Simple Class....223
Private and Public Access Specifiers....226
Constructors and Destructors....229
Using the Stock Class in a std::vector....232
Introducing Classes in Depth....235
Constructors and Destructors in Depth....236
Splitting a Class Between Header and Source Files....238
Conclusion....240
Chapter 11. Classes: Special Member Functions and Move Semantics....242
Copying Objects....242
Moving Objects....244
Move and Copy Assignments....246
Copies and Moves in Depth....250
How Does a std::string Work?....250
Move Constructors and Move Assignments....252
Copy Constructors and Copy Assignments....253
Conclusion....254
Chapter 12. Memory Management with std::unique_ptr....256
Creating a std::unique_ptr....257
Using a std::unique_ptr....259
Smart Pointers in Depth....260
More on Pointers and References....260
Unique Pointers in More Detail....262
Other Smart Pointers....264
Custom Deleters....265
Using a std::unique_ptr in a Class....266
Using the Exchange Class....270
Conclusion....273
Chapter 13. Classes: Virtual Functions and Inheritance....274
Base Class and Derived Classes....275
Defining an Abstract Base Class....275
A Derived Class....276
Using Derived Classes....279
A New Trading Game....282
Adding Another Derived Type....285
Virtual Functions and Inheritance in Depth....292
Virtual Destructors....292
Virtual Functions and Slicing....294
Conclusion....296
Chapter 14. Using std::variant and std::visit....298
Creating and Using a std::variant....299
Using a std::variant in std::visit....302
Using the Event in the Trading Game....305
The std::variant in Depth....308
Spotting and Handling Potential Problems with std::variant....309
Using std::optional and std::any....310
Conclusion....312
Chapter 15. Templates and std::unordered_map....314
Making a Lookup Table....314
Write Your Own Template....316
Specializing std::hash....320
Adding an Equality Operator for an Event....323
Adding a Way to Display the Events....325
Keeping a Tally of Events in Your Trading Game....327
Associative Containers and Templates in Depth....330
More on Templates....332
Conclusion....335
Index....336
About the Author....346
Colophon....346
You know how to code, but you're ready to level up. You've heard about the power and performance of C++, a language vital to fields like AI, game development, and high-performance computing. But where do you even start? Existing resources are outdated or overly academic, or they assume a deep understanding of C. You need a guide that respects your existing skills and gets you coding fast with modern C++.
This book cuts through the noise, focusing on the essential elements of C++ with hands-on projects that quickly build your skills and confidence. Author and C++ expert Frances Buontempo gets you up to speed quickly with the latest features and best practices of the language, preparing you for more advanced exploration.