Table of Contents
Section 1 Essential skills for modern C++
Chapter 1 An introduction to C++ programming
An overview of programming and C++ 4
Four general-purpose programming languages 4
A brief history of C++ 6
A quick look at C++ development 8
The user interface for a console application 8
The source code for a console application 10
How source code compiles to an executable file 12
Four popular IDEs and compilers 14
How to use Visual Studio for Windows development 16
How to open a project and work with source code 16
How to compile and run a project 18
How to use code completion and error detection 20
How to create a new project 22
How to use Xcode for macOS development 26
How to open a project and work with source code 26
How to compile and run a project 28
How to use code completion and error detection 30
How to create a new project 32
Chapter 2 How to write your first programs
Basic coding skills 40
How to code statements 40
How to code comments 40
How to code a main() function 42
How to create identifiers 44
How to work with numeric variables 46
How to define and initialize variables 46
How to code assignment statements 46
How to code arithmetic expressions 48
How to use the console for input and output 50
How to include header files 50
How to write output to the console 52
How to read input from the console 54
The Gallons to Liters program 56
How to work with the standard library 58
How to call a function 58
How to work with the std namespace 60
The Circle Calculator program 62
How to generate random numbers 64
How to work with char and string variables 66
How to assign values to char and string variables 66
How to work with special characters 68
How to read strings and chars from the console 70
How to fix a common problem with reading strings 72
The Guest Book program 74
How to test and debug a program 76
How to test a program 76
How to debug a program 76
Chapter 3 How to make decisions
How to get started with if statements 84
How to use the relational operators 84
How to code an if statement 86
How to work with braces 88
The Invoice 1.0 program 90
More skills for coding if statements 92
How to use the logical operators 92
If statements that use the logical operators 94
How to code nested if statements 96
The Invoice 2.0 program 98
Other ways to make decisions 102
How to use the conditional operator 102
How to code switch statements 104
More examples of switch statements 106
A switch statement for the Invoice 2.0 program 108
Chapter 4 How to code loops
More skills for coding arithmetic expressions 114
How to use arithmetic unary operators 114
How to use the compound assignment operators 116
How to work with the order of precedence 118
How to code while and do-while loops 120
How to code while loops 120
More examples of while loops 122
How to code do-while loops 124
The Test Scores program 126
How to code for loops and nested loops 128
How to code for loops 128
The Future Value program 130
How to code nested loops 132
How to code break and continue statements 134
How to code break statements 134
How to code continue statements 134
The Guess the Number program 136
Chapter 5 How to work with I/O streams and files
How to work with input streams 142
An introduction to streams and buffers 142
How unexpected input can cause problems 144
How to discard data from an input stream 146
How to detect data input errors 148
How to handle data input errors 150
How to work with output streams 152
An introduction to stream manipulators 152
How to specify the width of a column 152
How to right or left justify columns 154
How to format floating-point numbers 156
The Invoice 3.0 program 158
How to work with file streams 162
How to read and write a file 162
How to append data to a file 164
How to use the fstream object to work with files 164
How to check for errors when working with files 166
How to write delimited data 168
How to read delimited data 170
The Temperature Manager program 172
How to work with string streams 176
How to use a string stream to handle unexpected data 176
The Temperature Analyzer program 178
Chapter 6 How to work with data types, strings, and vectors
Basic skills for working with data types 188
The fundamental data types 188
How to define and initialize variables 190
How to define and initialize constants 192
The Light Years Calculator program 194
More skills for working with data types 196
How to work with type conversion 196
How to convert between numbers and strings 198
How to work with data type sizes and limits 200
How to fix problems with floating-point data 202
How to work with vectors 204
How to create a vector and refer to its elements 204
How to initialize and loop through a vector 206
How to use member functions of a vector 208
The Test Scores program 210
The Temperature Manager program 214
How to work with strings 218
How to create and loop through a string 218
How to use basic member functions of a string 220
How to search a string 222
How to work with substrings 224
How to modify a string 226
How to check characters within a string 228
The Create Account program 230
The Word Jumble program 234
Chapter 7 How to code functions
How to start coding your own functions 244
How to define and call a function 244
The Miles Per Gallon program 246
How to declare a function 248
When and how to use local and global variables 250
How to plan the functions of a program 252
How to use a hierarchy chart 252
The Convert Temperatures program 254
More skills for coding functions 258
How to use default values for arguments 258
How to overload a function 260
How to use reference variables as parameters 262
How to use reference parameters to improve efficiency 264
The Temperature Manager program 266
How to work with header files and namespaces 274
How to create, implement, and use header files 274
How to define namespaces 276
A header for getting input from the console 278
The Future Value program 282
Chapter 8 How to test, debug, and deploy an application
Basic skills for testing and debugging 292
Typical test phases 292
The three types of errors 292
Common C++ errors 294
How to plan the test runs 296
A simple way to trace code execution 298
How to use Visual Studio to debug a program 300
How to set and remove breakpoints 300
How to step through code 302
How to inspect variables 302
How to inspect the stack trace 304
How to use Xcode to debug a program 306
How to set and remove breakpoints 306
How to step through code 308
How to inspect variables 308
How to inspect the stack trace 310
How to deploy and run a program 312
How to deploy a program 312
How to run a deployed program 312
Section 2 More skills as you need them
Chapter 9 How to work with structures and enumerations
Basic skills for working with structures 320
How to get started with structures 320
How to initialize a structure 322
The Movie List 1.0 program 324
More skills for working with structures 328
How to nest structures 328
How to use structures with functions 330
How to compare structures for equality 332
How to work with member functions 334
How to work with member operators 334
The Movie List 2.0 program 336
How to work with enumerations 342
Basic skills for working with scoped enumerations 342
More skills for working with scoped enumerations 344
How to work with unscoped enumerations 346
The Monthly Bonus Calculator program 348
Chapter 10 How to work with built-in arrays and C strings
Basic skills for built-in arrays 356
How to create an array and access its elements 356
How to initialize an array 358
How to loop through an array 358
How to pass an array to a function 360
How to compare and copy arrays 362
The Test Scores program 364
How to work with C strings 368
An introduction to C strings 368
How to use C strings with input streams 370
Some utility functions for working with C strings 372
How to loop through a C string 374
The Create Account program 376
Advanced skills for built-in arrays 382
How to search an array 382
How to sort an array 384
How to work with a two-dimensional array 386
How to pass a two-dimensional array to a function 388
The Top Five program 390
Chapter 11 How to work with exceptions
How to get started with exceptions 400
A function that doesn't use exceptions 400
How to throw an exception 402
How to catch an exception 404
A program that catches exceptions 406
A program that prevents exceptions from being thrown 408
More skills for working with exceptions 410
How to catch multiple exceptions 410
How to rethrow an exception 412
The Temperature Manager program 414
How to work with custom exceptions 416
How exception handling works 418
Section 3 Object-oriented programming
Chapter 12 How to define classes
An introduction to object-oriented programming 426
A Movie structure that doesn't provide encapsulation 426
A Movie class that provides encapsulation 428
How to define private data members 430
How to define getter and setter functions 432
The Movie List 1.0 program 434
More skills for coding member functions 438
How to work with private member functions 438
How to overload a setter function 440
How to define constructors 442
How to define destructors 444
How to store a class in header and source files 446
The header and source files for a Movie class 446
When and how to use inline functions 450
The Movie List 2.0 program 452
How to work with UML diagrams 458
An introduction to UML diagrams 458
UML diagrams with data types 460
A Product class that implements a UML diagram 462
The Product Viewer program 466
How to work with object composition 470
A Die class 470
A Dice class 472
The Dice Roller program 474
The Pig Dice game 476
The console 476
The code 476
Chapter 13 How to work with inheritance
How to get started with inheritance 488
How inheritance works 488
How to define a superclass 490
How to define a subclass 492
How to define another subclass 494
How polymorphism works 496
The Product Viewer program 498
More skills for working with inheritance 502
How to define an abstract class 502
How to control overriding 504
How to work with multiple inheritance 506
How multiple inheritance works 506
The DayReader superclass 506
The DayWriter superclass 506
The DayIO subclass 510
Code that uses the DayIO subclass 510
When to use inheritance 512
How to use inheritance with custom exceptions 512
Guidelines for using inheritance 514
Chapter 14 More skills for object-oriented programming
How to work with static members 522
How to code static data members and functions 522
How to access static data members and functions 524
The Console class 526
Code that uses the Console class 528
How to work with a friend function 530
The FuelTank class 530
A friend function that works with two classes 532
How to overload operators 534
How to overload arithmetic binary operators 534
How to overload arithmetic unary operators 536
How to overload relational operators 538
How to overload the insertion and extraction operators 540
How to work with modules 542
How to work with a module that exports a function 542
How to work with a module that exports namespaces 544
How to work with a module that exports classes 546
How to use the export keyword for access control 548
How to use the import keyword 550
Section 4 Data structures and algorithms
Chapter 15 How to work with STL containers and iterators
An introduction to STL containers and iterators 560
A summary of STL containers 560
A summary of STL iterators 562
Basic skills for working with iterators 564
Member functions shared by the STL containers 566
How to iterate the data in a container 568
More skills for working with vectors 570
Member functions shared by the sequence containers 570
Member functions of a vector 572
How to set capacity to improve efficiency 574
The Movie Rankings 1.0 program 576
How to work with arrays 580
Basic skills for working with arrays 580
How to pass an array to a function 582
How to work with lists 584
An introduction to lists and forward lists 584
Member functions of a list 586
The Movie Rankings 2.0 program 588
How to work with queues and stacks 592
How to work with queues 592
How to work with stacks 592
How to work with sets 594
Member functions of associative containers 594
Code examples that work with sets 594
How to work with maps 596
Member functions and operators of a map 596
How to insert key/value pairs and work with values by key 598
The Word Counter program 600
How to work with nested containers 604
How to work with a vector of vectors 604
How to work with a map of vectors 604
Chapter 16 How to work with STL algorithms
An introduction to STL algorithms 612
The relationship between containers, iterators, and algorithms 612
How to call an algorithm 614
How to pass a function as an argument 614
Basic skills for working with algoritms 616
How to use non-modifying algorithms 616
How to use modifying algorithms 618
How to use the min and max algorithms 620
How to use the numeric algorithms 620
How to use the sort and binary search algorithms 622
The Number Cruncher program 624
More skills for working with algorithms 628
How to use algorithms with intervals of key/value pairs 628
How to use algorithms with nested containers 630
More skills for passing functions to algorithms 632
How to work with function templates 632
How to work with function objects 634
How to work with lambda expressions 636
The Uptime Percentage program 638
Chapter 17 How to work with memory and pointers
An introduction to memory and pointers 648
How physical memory works 648
How to define and use pointers 650
More skills for defining and using pointers 652
How pointer variables compare to reference variables 654
How to use pointers with functions 656
How and when to pass pointers to functions 656
How to use the this pointer in a member function 658
The Step Counter 1.0 program 660
The Step Counter 2.0 program 662
How to use pointers to work with dynamic memory 664
An overview of the types of storage 664
How to allocate and deallocate free store memory 666
How to avoid memory leaks and corruption 668
How to use RAII (Resource Acquisition Is Instantiation) 668
How to implement the Rule of Three with RAII 670
How to implement the Rule of Five with RAII 672
How to work with smart pointers 674
The Sensor Analysis program 676
More skills for working with pointers 682
How to compare pointers 682
How to use pointer arithmetic 682
How to work with void pointers 682
How to use pointers with inheritance 684
How complex compound types work 686
Chapter 16 How to work with templates
How to work with function templates 694
An overloaded function 694
A function template 696
How to code a function template with one type parameter 698
How to code a function template with multiple type parameters 700
How to work with class templates 702
How to code a simple class template 702
How to code a more complex class template 704
How to use a complex class template 708
How to code a function template that works with a class template 710
The Sensor Analysis program 712
Chapter 19 How to code custom containers, iterators and algorithms
How to code a custom container 718
How to work with member types 718
The MyVector class declaration 720
The constructor and destructor definitions 722
The assignment operator definitions 724
The member function definitions 726
The Task Manager 1.0 program 728
How to code a custom iterator 730
How to work with iterator traits 730
The Link structure 732
The Mylterator class 734
The MyList class declaration 736
The destructor definition 738
The member function definitions 738
The Task Manager 2.0 program 742
How to code a custom algorithm 744
The find_midpoint() algorithm 744
The Number Cruncher program 746
Appendix A How to set up Windows for this book
How to install the Visual Studio IDE 756
How to install the source code for this book 758
Appendix B How to set up macOS for this book
How to install the Xcode IDE 762
How to install the source code for this book 764