Beginning C++23: From Beginner to Pro [7 ed.] 1484293428, 9781484293423, 9781484293430

Begin your programming journey with C++ , starting with the basics and progressing through step-by-step examples that wi

169 54 6MB

English Pages XXX, 918 (948) Year 2023

Report DMCA / Copyright


Beginning C++23: From Beginner to Pro [7 ed.]
 1484293428, 9781484293423, 9781484293430

  • Commentary
  • Published Due: 21 October 2023

Table of contents :
Table of Contents
About the Authors
About the Technical Reviewer
Chapter 1: Basic Ideas
Modern C++
Standard Libraries
C++ Program Concepts
Source Files
Comments and Whitespace
The Standard Library and Modules
Text Output
return Statements
Identifiers and Keywords
Classes and Objects
Code Appearance and Programming Style
Creating an Executable
Procedural and Object-Oriented Programming
Representing Numbers
Binary Numbers
Hexadecimal Numbers
Negative Binary Numbers
Octal Values
Big-Endian and Little-Endian Systems
Floating-Point Numbers
Representing Characters
UCS and Unicode
Chapter 2: Introducing Fundamental Types of Data
Variables, Data, and Data Types
Defining Integer Variables
Signed Integer Types
Unsigned Integer Types
Zero Initialization
Defining Variables with Fixed Values
Integer Literals
Decimal Integer Literals
Hexadecimal Literals
Octal Literals
Binary Literals
Calculations with Integers
Compound Arithmetic Expressions
Assignment Operations
The op= Assignment Operators
Incrementing and Decrementing Integers
Postfix Increment and Decrement Operations
Floating-Point Variables
Standard Floating-Point Data Types
Extended Floating-Point Data Types
Floating-Point Literals
Finding the Limits
Finding Other Properties of Fundamental Types
The sizeof Operator
Floating-Point Calculations
Mathematical Constants
Mathematical Functions
Invalid Floating-Point Results
Mixed Expressions and Type Conversion
Explicit Type Conversion
Old-Style Casts
The auto Keyword
Working with Character Variables
Working with Unicode Characters
Unicode Literals and Source File Encodings
Escape Sequences
Escaping Unicode Characters
Modern Formatting
Format Specifiers
Formatting Tabular Data
Formatting Numbers
Argument Indexes
Formatting Stream Output
Chapter 3: Working with Fundamental Data Types
Operator Precedence and Associativity
Bitwise Operators
The Bitwise Shift Operators
Shifting Signed Integers
Logical Operations on Bit Patterns
Using the Bitwise AND
Using the Bitwise OR
Using the Bitwise Complement Operator
Using the Bitwise Exclusive OR
Using the Bitwise Operators: An Example
The Lifetime of a Variable
Global Variables
Enumerated Data Types
Aliases for Data Types
Chapter 4: Making Decisions
Comparing Data Values
Applying the Comparison Operators
Comparing Floating-Point Values
The Spaceship Operator
Comparison Categories
Named Comparison Functions
The if Statement
Nested if Statements
Character Classification and Conversion
The if-else Statement
Nested if-else Statements
Understanding Nested ifs
Logical Operators
Logical AND
Logical OR
Logical Negation
Combining Logical Operators
Logical Operators on Integer Operands
Logical Operators vs. Bitwise Operators
Short-Circuit Evaluation
Logical XOR
The Conditional Operator
The switch Statement
Switching on Enumeration Values
Statement Blocks and Variable Scope
Initialization Statements
Chapter 5: Arrays and Loops
Using an Array
Understanding Loops
The for Loop
Avoiding Magic Numbers
Defining the Array Size with the Braced Initializer
Determining the Size of an Array
Controlling a for Loop with Floating-Point Values
More Complex for Loop Control Expressions
The Comma Operator
The Range-Based for Loop
The while Loop
The do-while Loop
Nested Loops
Skipping Loop Iterations
Breaking Out of a Loop
Indefinite Loops
Controlling a for Loop with Unsigned Integers
Arrays of Characters
Multidimensional Arrays
Initializing Multidimensional Arrays
Setting Dimensions by Default
Multidimensional Character Arrays
Allocating an Array at Runtime
Alternatives to Using an Array
Using array Containers
Accessing Individual Elements
Operations on arrays As a Whole
Conclusion and Example
Using std::vector Containers
Deleting Elements
Example and Conclusion
Formatting Ranges
Chapter 6: Pointers and References
What Is a Pointer?
The Address-Of Operator
The Indirection Operator
Why Use Pointers?
Pointers to Type char
Arrays of Pointers
Constant Pointers and Pointers to Constants
Pointers and Arrays
Pointer Arithmetic
The Difference Between Pointers
Comparing Pointers
Using Pointer Notation with an Array Name
Dynamic Memory Allocation
The Stack and the Free Store
Using the new and delete Operators
Dynamic Allocation of Arrays
Multidimensional Arrays
Member Selection Through a Pointer
Hazards of Dynamic Memory Allocation
Dangling Pointers and Multiple Deallocations
Allocation/Deallocation Mismatch
Memory Leaks
Fragmentation of the Free Store
Golden Rule of Dynamic Memory Allocation
Raw Pointers and Smart Pointers
Using unique_ptr Pointers
Using shared_ptr Pointers
Understanding References
Defining References
Using a Reference Variable in a Range-Based for Loop
Chapter 7: Working with Strings
A Better Class of String
Defining string Objects
Copying and Marking Substrings
Formatting and Reading String Objects
Operations with String Objects
Concatenating Strings
Concatenating Strings and Characters
Concatenating Strings and Numbers
Accessing Characters in a String
Accessing Substrings
Comparing Strings
Three-Way Comparisons
Comparing Substrings Using compare()
Comparing Substrings Using substr()
Checking the Start or End of a String
Searching Strings
Searching Within Substrings
Searching for Any of a Set of Characters
Searching a String Backward
Modifying a String
Inserting a String
Replacing a Substring
Removing Characters from a String
std::string vs. std::vector
Converting Strings into Numbers
Strings of International Characters
Strings of wchar_t Characters
Objects That Contain Unicode Strings
Raw String Literals
Chapter 8: Defining Functions
Segmenting Your Programs
Functions in Classes
Characteristics of a Function
Defining Functions
The Function Body
Return Values
How the return Statement Works
Function Declarations
Function Prototypes
Passing Arguments to a Function
Passing a Pointer to a Function
Passing an Array to a Function
const Pointer Parameters
Passing a Multidimensional Array to a Function
References vs. Pointers
Input vs. Output Parameters
Passing Arrays by Reference
References and Implicit Conversions
Default Argument Values
Multiple Default Argument Values
Arguments to main()
Returning Values from a Function
Returning a Pointer
Returning a Reference
Returning vs. Output Parameters
Return Type Deduction
Static Local Variables
Function Overloading
Overloading and Pointer Parameters
Overloading and Reference Parameters
Overloading and const Parameters
Overloading with const Pointer Parameters
Overloading and Reference-to-const Parameters
Overloading and Default Argument Values
Basic Examples of Recursion
Recursive Algorithms
The Quicksort Algorithm
The main() Function
The extract_words() Function
The swap() Function
The sort() Functions
The max_word_length() Function
The print_words() Function
Constant Expressions
constexpr Variables
constexpr Functions
Immediate Functions (consteval Functions)
Limitations to Constant Expressions
constexpr Function Evaluations Only
Creating constexpr-Ready Functions
Dynamic Memory during Constant Evaluation
Chapter 9: Vocabulary Types
Working with Optional Values
String Views: The New Reference-to-const-string
Using String View Function Parameters
A Proper Motivation
Spans: The New Reference-to-vector or -array
Writing Through a Span
Spans of const Elements
Fixed-Size Spans
Chapter 10: Function Templates
Function Templates
Creating Instances of a Function Template
Template Type Parameters
Explicit Template Arguments
Function Template Specialization
Function Templates and Overloading
Function Templates with Multiple Parameters
Return Type Deduction in Templates
constexpr If
Default Values for Template Parameters
Non-Type Template Parameters
Templates for Functions with Fixed-Size Array Arguments
Abbreviated Function Templates
Limitations to Abbreviated Function Templates
Chapter 11: Modules and Namespaces
Your First Module
Export Blocks
Separating Interface from Implementation
Module Implementation Files
Limitations to Implementation Files
Implicit Imports in Implementation Files
Reachability vs. Visibility
Exporting Import Declarations
Managing Larger Modules
Simulating Submodules
Module Partitions
Module Implementation Partitions
Module Interface Partitions
The Global Namespace
Defining a Namespace
Nested Namespaces
Namespaces and Modules
Organizing Larger Namespaces and Modules
Functions and Namespaces
Using Directives and Declarations
Namespace Aliases
Chapter 12: Defining Your Own Data Types
Classes and Object-Oriented Programming
Data Hiding
Defining a Class
Creating Objects of a Class
Default Constructors
Defining a Class Constructor
Using the default Keyword
Defining Functions Outside the Class
Default Arguments for Constructor Parameters
Using a Member Initializer List
Using the explicit Keyword
Delegating Constructors
The Copy Constructor
Implementing the Copy Constructor
Deleting the Copy Constructor
Defining Classes in Modules
Accessing Private Class Members
The this Pointer
Returning this from a Function
const Objects and const Member Functions
const Member Functions
const Correctness
Overloading on const
Casting Away const
Using the mutable Keyword
The Friend Functions of a Class
Friend Classes
Arrays of Class Objects
The Size of a Class Object
Static Members of a Class
Static Member Variables
Accessing Static Member Variables
Static Constants
Static Member Variables of the Class Type Itself
Static Member Functions
constexpr Member Functions
consteval Constructors
Using Pointers as Class Members
The Truckload Example
The Box Class
The SharedBox Type Alias
Defining the Package Class
Defining the Truckload Class
Traversing the Boxes Contained in a Truckload
Adding and Removing Boxes
Generating Random Boxes
Putting It All Together
Nested Classes
Nested Classes with Public Access
A Better Mechanism for Traversing a Truckload: Iterators
Chapter 13: Operator Overloading
Implementing Operators for a Class
Operator Overloading
Implementing an Overloaded Operator
Nonmember Operator Functions
Implementing Full Support for an Operator
Operators That Can Be Overloaded
Restrictions and Key Guideline
Operator Function Idioms
Supporting All Comparison Operators
Defaulting Comparison Operators
Overloading the 

Polecaj historie