Switch content of the page by the Role togglethe content would be changed according to the role
![](https://www.pearson.com/store//pmccommercewebservices/v2/medias/size-W370-A1030-00-23-75-A103000237565-A103000237565-Lrg.jpg?context=bWFzdGVyfGltYWdlc3w4NDIzMXxpbWFnZS9qcGVnfHN5cy1tYXN0ZXIvaW1hZ2VzL2g4NS9oNGEvMTI5OTY1MDEyNDE4ODYvc2l6ZV9XMzcwXy9BMTAzMC8wMC8yMy83NS9BMTAzMDAwMjM3NTY1L0ExMDMwMDAyMzc1NjVfTHJnLmpwZ3w5YjZkNzgxOTYyZGNkNGE4Zjk4MjI1ODI0MTVkZmQ0MDRmMjAwYTA4Y2ZlM2EwNjNjYWI5YTFkYmI1Y2EyM2E0)
Sams Teach Yourself C++ in One Hour a Day, 9th edition
Published by Sams Publishing (January 19, 2022) © 2022
- Siddhartha Rao
$47.99
Buy nowOpens in a new tab
Sold by InformIT and ebook resellers
- Available for purchase from all major ebook resellers, including InformIT.com
$39.99
Price Reduced From: $49.99
Details
- A print text
- Free shipping
- Also available for purchase as an ebook from all major ebook resellers, including InformIT.com
The Ninth Edition of Sams Teach Yourself C++ in One Hour a Day
Starting with one hour a day, you can gain all the skills you need to begin programming in C++. This complete tutorial will help you quickly master the basics of object-oriented programming and teach you advanced C++ language features and concepts. Fully updated for the C++20 standard, this practical book is designed to help you write C++ code that's faster, simpler, and more reliable.
* Learn modern C++20 features such as concepts, ranges, views, adaptors, and modules * Apply proven Do's and Don'ts to leverage best practices and avoid pitfalls from day one
* Test your knowledge and expertise with focused exercises after every lesson* Learn using nearly 300 compiling code samples that are available for free download and have been explained in detail in the book
Learn on your own time, at your own pace
* No programming experience required: start writing well-organized, efficient C++ programs quickly!* Master object-oriented concepts such as classes, inheritance, polymorphism, encapsulation, and abstraction* Create reliable, feature-rich programs with STL classes, containers, and algorithms
* Simplify your code using automatic type deduction and other features* Program function objects with modern C++ Lambda expressions
* Accelerate learning using nearly 300 code samples explained within* Make the most of new C++20 concepts, ranges, views, adaptors, and modules
* Preview improvements expected in C++23
Introduction xxvi
PART I: The Basics
Lesson 1: Getting Started
A Brief History of C++
   Connection to C
   Advantages of C++
   Evolution of the C++ Standard
   Who Uses Programs Written in C++?
Programming a C++ Application
   Steps in Building an Executable
   Analyzing Errors and Debugging
   Integrated Development Environments
   Programming Your First C++ Application
   Building and Executing Your First C++ Application
   Understanding Compiler Errors
What’s New in C++20?
Summary
Q&A
Workshop
   Quiz
   Exercises
Lesson 2: The Anatomy of a C++ Program
Parts of the Hello World Program
Preprocessor Directive #include
The Body of Your Program: main()
   Returning a Value
The Concept of Namespaces
Comments in C++ Code
Functions in C++
Basic Input Using std::cin and Output Using std::cout
Summary
Q&A
Workshop
   Quiz
   Exercises
Lesson 3: Using Variables, Declaring Constants
What Is a Variable?
   Memory and Addressing in Brief
   Declaring Variables to Access and Use Memory
   Declaring and Initializing Multiple Variables of a Type
   Understanding the Scope of a Variable
   Global Variables
   Naming Conventions
Common Compiler-Supported C++ Variable Types
   Using Type bool to Store Boolean Values
   Using Type char to Store Character Values
   The Concept of Signed and Unsigned Integers
   Signed Integer Types short, int, long, and long long
   Unsigned Integer Types unsigned short, unsigned int, unsigned long, and unsigned long long
   Avoiding Overflow Errors by Selecting Correct Data Types
   Floating-Point Types float and double
Determining the Size of a Variable by Using sizeof()
   Avoid Narrowing Conversion Errors by Using List Initialization
Automatic Type Inference Using auto
Using typedef to Substitute a Variable’s Type
What Is a Constant?
   Literal Constants
   Declaring Variables as Constants Using const
   Constant Expressions Using constexpr
   C++20 Immediate Functions Using consteval
   Enumerations
   Scoped Enumerations
   Defining Constants by Using #define
Keywords You Cannot Use as Variable or Constant Names
Summary
Q&A
Workshop
   Quiz
   Exercises
Lesson 4: Managing Arrays and Strings
What Is an Array?
   The Need for Arrays
   Declaring and Initializing Static Arrays
   How Data Is Stored in an Array
   Accessing Data Stored in an Array
   Modifying Data Stored in an Array
Multidimensional Arrays
   Declaring and Initializing Multidimensional Arrays
   Accessing Elements in a Multidimensional Array
Dynamic Arrays
C-Style Character Strings
C++ Strings: Using std::string
Summary
Q&A
Workshop
   Quiz
   Exercises
Lesson 5: Working with Expressions, Statements, and Operators
Statements
Compound Statements, or Blocks
Using Operators
   The Assignment Operator (=)
   Understanding l-Values and r-Values
   Operators to Add (+), Subtract (-), Multiply (*), Divide (/), and Modulo Divide (%)
   Operators to Increment (++) and Decrement (--)
   To Postfix or to Prefix?
   Equality Operators (== and !=)
   Relational Operators
   C++20 Three-Way Comparison Operator (<=>)
   Logical Operations NOT, AND, OR, and XOR
   Using C++ Logical Operators NOT (!), AND (&&), and OR (||)
   Bitwise NOT (~), AND (&), OR (|), and XOR (^) Operators
   Bitwise Right Shift (>>) and Left Shift (<<) Operators
   Compound Assignment Operators
   Using the sizeof() Operator to Determine the Memory Occupied by a Variable
   Operator Precedence and Associativity
Summary
Q&A
Workshop
   Quiz
   Exercises
Lesson 6: Controlling Program Flow
Conditional Execution Using if.else
   Conditional Programming Using if.else
   Conditional Execution of Statements Within a Block
   Nested if Statements Â
   Conditional Processing Using switch-case Â
   Conditional Execution Using the ?: Operator
Getting Code to Execute in Loops
   A Rudimentary Loop Using goto
   The while Loop
   The do.while Loop
   The for Loop Â
   The Range-Based for Loop
Modifying Loop Behavior Using continue and break Â
   Loops That Don’t End: Infinite Loops
   Controlling Infinite Loops
Programming Nested Loops
   Using Nested Loops to Walk a Multidimensional Array
   Using Nested Loops to Calculate Fibonacci Numbers
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 7: Organizing Code with Functions
The Need for Functions Â
   What Is a Function Prototype?
   What Is a Function Definition?
   What Is a Function Call, and What Are Arguments?
   Programming a Function with Multiple Parameters
   Programming Functions with No Parameters or No Return Values
   Function Parameters with Default Values
   Recursion: Functions That Invoke Themselves Â
   Functions with Multiple Return Statements Â
Using Functions to Work with Different Forms of Data
   Overloading Functions
   Passing an Array of Values to a Function
   Passing Arguments by Reference
How Function Calls Are Handled by the Microprocessor
   Inline Functions
   Automatic Return Type Deduction Â
   Lambda Functions
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 8: Pointers and References Explained
What Is a Pointer?
   Declaring a Pointer
   Determining the Address of a Variable by Using the Reference Operator (&)
   Using Pointers to Store Addresses Â
   Accessing Pointed Data Using the Dereference Operator (*)
   What Is the Size of a Pointer?
Dynamic Memory Allocation
   Using the Operators new and delete to Allocate and Release
   Memory Dynamically
   Effects of the Increment (++) and Decrement (--) Operators on Pointers Â
   Using the const Keyword on Pointers
   Passing Pointers to Functions
   Similarities Between Arrays and Pointers
Common Programming Mistakes When Using Pointers
   Memory Leaks
   Pointers Pointing to Invalid Memory Locations
   Dangling Pointers (Also Called Stray or Wild Pointers) Â
   Checking Whether an Allocation Request Using new Succeeded
Pointer Programming Best Practices Â
What Is a Reference?
   What Makes References Useful?
   Using the Keyword const on References Â
   Passing Arguments by Reference to Functions
Summary
Q&A
Workshop
   Quiz Â
   Exercises
PART II: Fundamentals of Object-Oriented C++ Programming
Lesson 9: Classes and Objects
The Concept of Classes and Objects Â
   Declaring a Class
   An Object as an Instance of a Class
   Accessing Members by Using the Dot Operator (.)
   Accessing Members by Using the Pointer Operator (->)
The Keywords public and private
   Abstraction of Data via the Keyword private Â
Constructors
   Declaring and Implementing a Constructor Â
   When and How to Use Constructors Â
   Overloading Constructors Â
   A Class Without a Default Constructor
   Constructor Parameters with Default Values Â
   Constructors with Initialization Lists
Destructor
   Declaring and Implementing a Destructor
   When and How to Use a Destructor
The Copy Constructor Â
   Shallow Copying and Associated Problems Â
   Ensuring a Deep Copy Using a Copy Constructor Â
   Using Move Constructors to Improve Performance
Different Uses of Constructors and the Destructor
   A Class That Does Not Permit Copying Â
   A Singleton Class That Permits a Single Instance
   A Class That Prohibits Instantiation on the Stack
   Using Constructors to Convert Types
The this Pointer
Using sizeof() with a Class
The Keyword struct and Its Differences from class Â
Declaring a friend of a class
Union: A Special Data Storage Mechanism Â
   Declaring a Union
   Where Would You Use a Union?
Using Aggregate Initialization on Classes and structs
   constexpr with Classes and Objects
Summary
Q&A
Workshop
   Quiz
   Exercises
Lesson 10: Implementing Inheritance
Basics of Inheritance
   Inheritance and Derivation
   C++ Syntax of Derivation
   The Access Specifier Keyword protected
   Base Class Initialization: Passing Parameters to the Base Class Â
   A Derived Class Overriding the Base Class’s Methods
   Invoking Overridden Methods of a Base Class Â
   Invoking Methods of a Base Class in a Derived Class
   A Derived Class Hiding the Base Class’s Methods Â
   Order of Construction
   Order of Destruction Â
Private Inheritance
Protected Inheritance
The Problem of Slicing
Multiple Inheritance
Avoiding Inheritance Using final
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 11: Polymorphism
Basics of Polymorphism
   Need for Polymorphic Behavior Â
   Polymorphic Behavior Implemented Using Virtual Functions Â
   Need for Virtual Destructors
   How Do Virtual Functions Work? Understanding the Virtual Function Table Â
   Abstract Base Classes and Pure Virtual Functions
Using Virtual Inheritance to Solve the Diamond Problem
Using the Specifier override to Indicate the Intention to Override
Using final to Prevent Function Overriding Â
Virtual Copy Constructors?
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 12: Operator Types and Operator Overloading
What Are Operators in C++?
Unary Operators Â
   Unary Increment (++) and Decrement (--) Operators
   Conversion Operators
   The Dereference Operator (*) and Member Selection Operator (->)
Binary Operators Â
   The Binary Addition (a+b) and Subtraction (a-b) Operators Â
   The Addition Assignment (+=) and Subtraction Assignment (-=) Operators Â
   The Equality (==) and Inequality (!=) Operators Â
   The <, >, <=, and >= Operators Â
   The C++20 Three-Way Comparison Operator (<=>) Â
   The Copy Assignment Operator (=) Â
   The Subscript Operator ([]) Â
The Function Operator (())
The Move Constructor and Move Assignment Operator for High-Performance Programming
   The Problem of Unwanted Copy Steps Â
   Declaring a Move Constructor and Move Assignment Operator Â
User-Defined Literals
Operators That Cannot Be Overloaded
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 13: Casting Operators
The Need for Casting Â
Why C-Style Casts Are Not Popular with Some C++ Programmers Â
The C++ Casting Operators
   Using static_cast
   Using dynamic_cast and Runtime Type Identification Â
   Using reinterpret_cast
   Using const_cast
Problems with the C++ Casting Operators
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 14: An Introduction to Macros and Templates
The Preprocessor and the Compiler
Using the Macro #define to Define Constants
   Using Macros for Protection Against Multiple Inclusion
Using #define to Write Macro Functions
   Why All the Parentheses?
   Using the assert Macro to Validate Expressions
   Advantages and Disadvantages of Using Macro Functions Â
An Introduction to Templates
   Template Declaration Syntax Â
   The Different Types of Template Declarations
   Template Functions
   Templates and Type Safety
   Template Classes Â
   Declaring Templates with Multiple Parameters
   Declaring Templates with Default Parameters
   Sample Template Class: HoldsPair
   Template Instantiation and Specialization
   Template Classes and static Members Â
   Variable Templates Â
   Using static_assert to Perform Compile-Time Checks
   Using Templates in Practical C++ Programming
Summary
Q&A
Workshop
   Quiz Â
   Exercises
PART III: Learning the Standard Template Library (STL)
Lesson 15: An Introduction to the Standard Template Library
STL Containers Â
   Sequential Containers
   Associative Containers Â
   Container Adapters
STL Iterators Â
STL Algorithms
Interaction Between Containers and Algorithms Using Iterators
   Using the Keyword auto to Let a Compiler Define Type
Choosing the Right Container Â
STL String Classes
Summary
Q&A
Workshop
   Quiz Â
Lesson 16: The STL String Class
The Need for String Manipulation Classes Â
Working with the STL string Class
   Instantiating the STL string Class and Making Copies
   Accessing Character Contents of std::string Â
   Concatenating One String to Another Â
   Finding a Character or Substring in a String Â
   Truncating an STL String Â
   String Reversal
   String Case Conversion
Template-Based Implementation of an STL String
operator ""s in std::string
Using std::string_view (Amended in C++20)
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 17: STL Dynamic Array Classes
The Characteristics of std::vector Â
Typical Vector Operations
   Instantiating a Vector
   Inserting Elements at the End of a Vector by Using push_back() Â
   List Initialization Â
   Inserting Elements at a Given Position by Using insert() Â
   Accessing Elements in a Vector by Using Array Semantics Â
   Accessing Elements in a Vector by Using Pointer Semantics
   Removing Elements from a Vector
Understanding the Concepts of Size and Capacity
The STL deque Class
Summary
Q&A
Workshop
   Quiz
   Exercises
Lesson 18: STL list and forward_list
The Characteristics of std::list
Basic list Operations
   Instantiating a std::list Object
   Inserting Elements at the Front or Back of a List Â
   Inserting Elements in the Middle of a List Â
   Erasing Elements from a List
Reversing and Sorting Elements in a List
   Reversing Elements by Using list::reverse()
   Sorting Elements Â
   Sorting and Removing Elements from a List That Contains Instances of a Class
   std::forward_list
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 19: STL set and multiset
An Introduction to STL Set Classes Â
Basic STL set and multiset Operations Â
   Instantiating a std::set Object
   Inserting Elements in a Set or Multiset
   Finding Elements in an STL set or multiset Container
   Erasing Elements in an STL set or multiset Container
Pros and Cons of Using STL set and multiset
STL Hash Set Implementation: std::unordered_set and std::unordered_multiset
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 20: STL map and multimap
An Introduction to STL Map Classes
Basic std::map and std::multimap Operations Â
   Instantiating std::map or std::multimap
   Inserting Elements in an STL Map or Multimap Â
   Finding Elements in an STL map Container
   Finding Elements in an STL multimap Container
   Erasing Elements from an STL map or multimap Container
Supplying a Custom Sort Predicate
STL’s Hash Table–Based Key/Value Container
   How Hash Tables Work
   Using unordered_map and unordered_multimap
Summary
Q&A
Workshop
   Quiz
   Exercises
PART IV: Lambda Expressions and STL Algorithms
Lesson 21: Understanding Function Objects
Function Objects and Predicates Â
Typical Applications of Function Objects Â
   Unary Functions
   Unary Predicates
   Binary Functions
   Binary Predicates
Summary
Q&A
Workshop
   Quiz
   Exercises
Lesson 22: Lambda Expressions
What Is a Lambda Expression?
How to Define a Lambda Expression
   Capturing Variables
   Parameters
   Return Types
A Lambda Expression for a Unary Function
A Lambda Expression for a Unary Predicate
A Lambda Expression with State via Capture Lists ([.])
A Lambda Expression for a Binary Function
A Lambda Expression for a Binary Predicate
Summary
Q&A
Workshop
   Quiz
   Exercises
Lesson 23: STL Algorithms
What Are STL Algorithms?
Classification of STL Algorithms
   Non-mutating Algorithms
   Mutating Algorithms
Usage of STL Algorithms
   Finding Elements, Given a Value or a Condition
   Counting Elements Given a Value or a Condition
   Searching for an Element or a Range in a Collection
   Initializing Elements in a Container to a Specific Value
   Using std::generate() to Initialize Elements to a Value Generated at Runtime
   Processing Elements in a Range by Using for_each()
   Performing Transformations on a Range by Using std::transform()
   Copy and Remove Operations
   Replacing Values and Replacing Elements Given a Condition Â
   Sorting and Searching in a Sorted Collection and Erasing Duplicates Â
   Partitioning a Range Â
   Inserting Elements in a Sorted Collection
   Performing Fold Operations Using std::accumulate() in C++20
C++20 Constrained Algorithms Â
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 24: Adaptive Containers: Stack and Queue
The Behavioral Characteristics of Stacks and Queues
   Stacks
   Queues
Using the STL stack Class
   Instantiating a Stack
   Stack Member Functions
   Insertion and Removal at the Top, Using push() and pop()
Using the STL queue Class
   Instantiating a Queue
   Member Functions of the queue Class
   Insertion at the End and Removal at the Beginning of a Queue via push() and pop()
Using the STL Priority Queue
   Instantiating the priority_queue Class
   Member Functions of priority_queue Â
   Insertion at the End and Removal at the Beginning of a Priority Queue via push() and pop()
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 25: Working with Bit Flags Using the STL
The bitset Class
   Instantiating std::bitset
Using std::bitset and Its Members
   Useful Operators in std::bitset Â
   std::bitset Member Methods Â
The vector<bool> Class Â
   Instantiating vector<bool>
   vector<bool> Functions and Operators
Summary
Q&A
Workshop
   Quiz Â
   Exercises
PART V: Advanced C++ Concepts
Lesson 26: Understanding Smart Pointers
What Are Smart Pointers?
   The Problem with Using Conventional (Raw) Pointers
   How Do Smart Pointers Help? Â
How Are Smart Pointers Implemented?
Types of Smart Pointers Â
   Deep Copy
   Copy on Write Â
   Reference-Counted Smart Pointers Â
   Reference-Linked Smart Pointers
   Destructive Copy
   Using std::unique_ptr
Popular Smart Pointer Libraries
Summary
Q&A
Workshop
   Quiz
   Exercises
Lesson 27: Using Streams for Input and Output
The Concept of Streams
Important C++ Stream Classes and Objects
Using std::cout for Writing Formatted Data to the Console
   Changing the Display Number Format by Using std::cout
   Aligning Text and Setting Field Width by Using std::cout
Using std::cin for Input
   Using std::cin for Input into a Plain Old Data Type
   Using std::cin::get for Input into the char* Buffer Â
   Using std::cin for Input into std::string
Using std::fstream for File Handling
   Opening and Closing a File Using open() and close()
   Creating and Writing a Text File by Using open() and the Operator <<
   Reading a Text File by Using open() and the Operator >> Â
   Writing to and Reading from a Binary File Â
Using std::stringstream for String Conversions
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 28: Exception Handling
What Is an Exception?
What Causes Exceptions?
Implementing Exception Safety via try and catch
   Using catch(.) to Handle All Exceptions
   Catching Exceptions of a Type Â
Throwing Exceptions of a Type by Using throw Â
How Exception Handling Works
Class std::exception Â
A Custom Exception Class Derived from std::exception
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 29: C++20 Concepts, Ranges, Views, and Adaptors
Concepts Â
   Using Concepts Provided by the Standard Library
   Defining Custom Concepts by Using the Keyword requires Â
   Using Concepts with Classes and Objects
The Ranges Library, Views, and Adaptors
   Views and Adaptors
   Adaptors Provided by the Ranges Library
   Combining Multiple Adaptors
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 30: C++20 Threads
Multithreading
   What Is a Thread?
   Why Program Multithreaded Applications?
   Using the C++20 Thread Library
   How Can Threads Transact Data?
   Using Mutexes and Semaphores to Synchronize Threads
Summary
Q&A
Workshop
   Exercise
Lesson 31: C++20 Modules and C++23
Modules
   The Problem with #include<header>
   C++20 Modules Â
   Programming a Module Â
   Consuming a Module Â
Why import Module; Is Superior to the Preprocessor #include<header> Â
C++23 Expected Features
Learning C++ Doesn’t Stop Here!
   Online Documentation
   Communities for Guidance and Help
Summary
Q&A
Workshop
   Exercise
PART VI: Appendixes
Appendix A: Working with Numbers: Binary and Hexadecimal
Appendix B: C++ Keywords
Appendix C: Writing Great C++ Code
Appendix D: ASCII Codes
Appendix E: Answers
9780137334681Â Â TOCÂ Â 12/20/2021
PART I: The Basics
Lesson 1: Getting Started
A Brief History of C++
   Connection to C
   Advantages of C++
   Evolution of the C++ Standard
   Who Uses Programs Written in C++?
Programming a C++ Application
   Steps in Building an Executable
   Analyzing Errors and Debugging
   Integrated Development Environments
   Programming Your First C++ Application
   Building and Executing Your First C++ Application
   Understanding Compiler Errors
What’s New in C++20?
Summary
Q&A
Workshop
   Quiz
   Exercises
Lesson 2: The Anatomy of a C++ Program
Parts of the Hello World Program
Preprocessor Directive #include
The Body of Your Program: main()
   Returning a Value
The Concept of Namespaces
Comments in C++ Code
Functions in C++
Basic Input Using std::cin and Output Using std::cout
Summary
Q&A
Workshop
   Quiz
   Exercises
Lesson 3: Using Variables, Declaring Constants
What Is a Variable?
   Memory and Addressing in Brief
   Declaring Variables to Access and Use Memory
   Declaring and Initializing Multiple Variables of a Type
   Understanding the Scope of a Variable
   Global Variables
   Naming Conventions
Common Compiler-Supported C++ Variable Types
   Using Type bool to Store Boolean Values
   Using Type char to Store Character Values
   The Concept of Signed and Unsigned Integers
   Signed Integer Types short, int, long, and long long
   Unsigned Integer Types unsigned short, unsigned int, unsigned long, and unsigned long long
   Avoiding Overflow Errors by Selecting Correct Data Types
   Floating-Point Types float and double
Determining the Size of a Variable by Using sizeof()
   Avoid Narrowing Conversion Errors by Using List Initialization
Automatic Type Inference Using auto
Using typedef to Substitute a Variable’s Type
What Is a Constant?
   Literal Constants
   Declaring Variables as Constants Using const
   Constant Expressions Using constexpr
   C++20 Immediate Functions Using consteval
   Enumerations
   Scoped Enumerations
   Defining Constants by Using #define
Keywords You Cannot Use as Variable or Constant Names
Summary
Q&A
Workshop
   Quiz
   Exercises
Lesson 4: Managing Arrays and Strings
What Is an Array?
   The Need for Arrays
   Declaring and Initializing Static Arrays
   How Data Is Stored in an Array
   Accessing Data Stored in an Array
   Modifying Data Stored in an Array
Multidimensional Arrays
   Declaring and Initializing Multidimensional Arrays
   Accessing Elements in a Multidimensional Array
Dynamic Arrays
C-Style Character Strings
C++ Strings: Using std::string
Summary
Q&A
Workshop
   Quiz
   Exercises
Lesson 5: Working with Expressions, Statements, and Operators
Statements
Compound Statements, or Blocks
Using Operators
   The Assignment Operator (=)
   Understanding l-Values and r-Values
   Operators to Add (+), Subtract (-), Multiply (*), Divide (/), and Modulo Divide (%)
   Operators to Increment (++) and Decrement (--)
   To Postfix or to Prefix?
   Equality Operators (== and !=)
   Relational Operators
   C++20 Three-Way Comparison Operator (<=>)
   Logical Operations NOT, AND, OR, and XOR
   Using C++ Logical Operators NOT (!), AND (&&), and OR (||)
   Bitwise NOT (~), AND (&), OR (|), and XOR (^) Operators
   Bitwise Right Shift (>>) and Left Shift (<<) Operators
   Compound Assignment Operators
   Using the sizeof() Operator to Determine the Memory Occupied by a Variable
   Operator Precedence and Associativity
Summary
Q&A
Workshop
   Quiz
   Exercises
Lesson 6: Controlling Program Flow
Conditional Execution Using if.else
   Conditional Programming Using if.else
   Conditional Execution of Statements Within a Block
   Nested if Statements Â
   Conditional Processing Using switch-case Â
   Conditional Execution Using the ?: Operator
Getting Code to Execute in Loops
   A Rudimentary Loop Using goto
   The while Loop
   The do.while Loop
   The for Loop Â
   The Range-Based for Loop
Modifying Loop Behavior Using continue and break Â
   Loops That Don’t End: Infinite Loops
   Controlling Infinite Loops
Programming Nested Loops
   Using Nested Loops to Walk a Multidimensional Array
   Using Nested Loops to Calculate Fibonacci Numbers
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 7: Organizing Code with Functions
The Need for Functions Â
   What Is a Function Prototype?
   What Is a Function Definition?
   What Is a Function Call, and What Are Arguments?
   Programming a Function with Multiple Parameters
   Programming Functions with No Parameters or No Return Values
   Function Parameters with Default Values
   Recursion: Functions That Invoke Themselves Â
   Functions with Multiple Return Statements Â
Using Functions to Work with Different Forms of Data
   Overloading Functions
   Passing an Array of Values to a Function
   Passing Arguments by Reference
How Function Calls Are Handled by the Microprocessor
   Inline Functions
   Automatic Return Type Deduction Â
   Lambda Functions
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 8: Pointers and References Explained
What Is a Pointer?
   Declaring a Pointer
   Determining the Address of a Variable by Using the Reference Operator (&)
   Using Pointers to Store Addresses Â
   Accessing Pointed Data Using the Dereference Operator (*)
   What Is the Size of a Pointer?
Dynamic Memory Allocation
   Using the Operators new and delete to Allocate and Release
   Memory Dynamically
   Effects of the Increment (++) and Decrement (--) Operators on Pointers Â
   Using the const Keyword on Pointers
   Passing Pointers to Functions
   Similarities Between Arrays and Pointers
Common Programming Mistakes When Using Pointers
   Memory Leaks
   Pointers Pointing to Invalid Memory Locations
   Dangling Pointers (Also Called Stray or Wild Pointers) Â
   Checking Whether an Allocation Request Using new Succeeded
Pointer Programming Best Practices Â
What Is a Reference?
   What Makes References Useful?
   Using the Keyword const on References Â
   Passing Arguments by Reference to Functions
Summary
Q&A
Workshop
   Quiz Â
   Exercises
PART II: Fundamentals of Object-Oriented C++ Programming
Lesson 9: Classes and Objects
The Concept of Classes and Objects Â
   Declaring a Class
   An Object as an Instance of a Class
   Accessing Members by Using the Dot Operator (.)
   Accessing Members by Using the Pointer Operator (->)
The Keywords public and private
   Abstraction of Data via the Keyword private Â
Constructors
   Declaring and Implementing a Constructor Â
   When and How to Use Constructors Â
   Overloading Constructors Â
   A Class Without a Default Constructor
   Constructor Parameters with Default Values Â
   Constructors with Initialization Lists
Destructor
   Declaring and Implementing a Destructor
   When and How to Use a Destructor
The Copy Constructor Â
   Shallow Copying and Associated Problems Â
   Ensuring a Deep Copy Using a Copy Constructor Â
   Using Move Constructors to Improve Performance
Different Uses of Constructors and the Destructor
   A Class That Does Not Permit Copying Â
   A Singleton Class That Permits a Single Instance
   A Class That Prohibits Instantiation on the Stack
   Using Constructors to Convert Types
The this Pointer
Using sizeof() with a Class
The Keyword struct and Its Differences from class Â
Declaring a friend of a class
Union: A Special Data Storage Mechanism Â
   Declaring a Union
   Where Would You Use a Union?
Using Aggregate Initialization on Classes and structs
   constexpr with Classes and Objects
Summary
Q&A
Workshop
   Quiz
   Exercises
Lesson 10: Implementing Inheritance
Basics of Inheritance
   Inheritance and Derivation
   C++ Syntax of Derivation
   The Access Specifier Keyword protected
   Base Class Initialization: Passing Parameters to the Base Class Â
   A Derived Class Overriding the Base Class’s Methods
   Invoking Overridden Methods of a Base Class Â
   Invoking Methods of a Base Class in a Derived Class
   A Derived Class Hiding the Base Class’s Methods Â
   Order of Construction
   Order of Destruction Â
Private Inheritance
Protected Inheritance
The Problem of Slicing
Multiple Inheritance
Avoiding Inheritance Using final
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 11: Polymorphism
Basics of Polymorphism
   Need for Polymorphic Behavior Â
   Polymorphic Behavior Implemented Using Virtual Functions Â
   Need for Virtual Destructors
   How Do Virtual Functions Work? Understanding the Virtual Function Table Â
   Abstract Base Classes and Pure Virtual Functions
Using Virtual Inheritance to Solve the Diamond Problem
Using the Specifier override to Indicate the Intention to Override
Using final to Prevent Function Overriding Â
Virtual Copy Constructors?
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 12: Operator Types and Operator Overloading
What Are Operators in C++?
Unary Operators Â
   Unary Increment (++) and Decrement (--) Operators
   Conversion Operators
   The Dereference Operator (*) and Member Selection Operator (->)
Binary Operators Â
   The Binary Addition (a+b) and Subtraction (a-b) Operators Â
   The Addition Assignment (+=) and Subtraction Assignment (-=) Operators Â
   The Equality (==) and Inequality (!=) Operators Â
   The <, >, <=, and >= Operators Â
   The C++20 Three-Way Comparison Operator (<=>) Â
   The Copy Assignment Operator (=) Â
   The Subscript Operator ([]) Â
The Function Operator (())
The Move Constructor and Move Assignment Operator for High-Performance Programming
   The Problem of Unwanted Copy Steps Â
   Declaring a Move Constructor and Move Assignment Operator Â
User-Defined Literals
Operators That Cannot Be Overloaded
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 13: Casting Operators
The Need for Casting Â
Why C-Style Casts Are Not Popular with Some C++ Programmers Â
The C++ Casting Operators
   Using static_cast
   Using dynamic_cast and Runtime Type Identification Â
   Using reinterpret_cast
   Using const_cast
Problems with the C++ Casting Operators
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 14: An Introduction to Macros and Templates
The Preprocessor and the Compiler
Using the Macro #define to Define Constants
   Using Macros for Protection Against Multiple Inclusion
Using #define to Write Macro Functions
   Why All the Parentheses?
   Using the assert Macro to Validate Expressions
   Advantages and Disadvantages of Using Macro Functions Â
An Introduction to Templates
   Template Declaration Syntax Â
   The Different Types of Template Declarations
   Template Functions
   Templates and Type Safety
   Template Classes Â
   Declaring Templates with Multiple Parameters
   Declaring Templates with Default Parameters
   Sample Template Class: HoldsPair
   Template Instantiation and Specialization
   Template Classes and static Members Â
   Variable Templates Â
   Using static_assert to Perform Compile-Time Checks
   Using Templates in Practical C++ Programming
Summary
Q&A
Workshop
   Quiz Â
   Exercises
PART III: Learning the Standard Template Library (STL)
Lesson 15: An Introduction to the Standard Template Library
STL Containers Â
   Sequential Containers
   Associative Containers Â
   Container Adapters
STL Iterators Â
STL Algorithms
Interaction Between Containers and Algorithms Using Iterators
   Using the Keyword auto to Let a Compiler Define Type
Choosing the Right Container Â
STL String Classes
Summary
Q&A
Workshop
   Quiz Â
Lesson 16: The STL String Class
The Need for String Manipulation Classes Â
Working with the STL string Class
   Instantiating the STL string Class and Making Copies
   Accessing Character Contents of std::string Â
   Concatenating One String to Another Â
   Finding a Character or Substring in a String Â
   Truncating an STL String Â
   String Reversal
   String Case Conversion
Template-Based Implementation of an STL String
operator ""s in std::string
Using std::string_view (Amended in C++20)
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 17: STL Dynamic Array Classes
The Characteristics of std::vector Â
Typical Vector Operations
   Instantiating a Vector
   Inserting Elements at the End of a Vector by Using push_back() Â
   List Initialization Â
   Inserting Elements at a Given Position by Using insert() Â
   Accessing Elements in a Vector by Using Array Semantics Â
   Accessing Elements in a Vector by Using Pointer Semantics
   Removing Elements from a Vector
Understanding the Concepts of Size and Capacity
The STL deque Class
Summary
Q&A
Workshop
   Quiz
   Exercises
Lesson 18: STL list and forward_list
The Characteristics of std::list
Basic list Operations
   Instantiating a std::list Object
   Inserting Elements at the Front or Back of a List Â
   Inserting Elements in the Middle of a List Â
   Erasing Elements from a List
Reversing and Sorting Elements in a List
   Reversing Elements by Using list::reverse()
   Sorting Elements Â
   Sorting and Removing Elements from a List That Contains Instances of a Class
   std::forward_list
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 19: STL set and multiset
An Introduction to STL Set Classes Â
Basic STL set and multiset Operations Â
   Instantiating a std::set Object
   Inserting Elements in a Set or Multiset
   Finding Elements in an STL set or multiset Container
   Erasing Elements in an STL set or multiset Container
Pros and Cons of Using STL set and multiset
STL Hash Set Implementation: std::unordered_set and std::unordered_multiset
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 20: STL map and multimap
An Introduction to STL Map Classes
Basic std::map and std::multimap Operations Â
   Instantiating std::map or std::multimap
   Inserting Elements in an STL Map or Multimap Â
   Finding Elements in an STL map Container
   Finding Elements in an STL multimap Container
   Erasing Elements from an STL map or multimap Container
Supplying a Custom Sort Predicate
STL’s Hash Table–Based Key/Value Container
   How Hash Tables Work
   Using unordered_map and unordered_multimap
Summary
Q&A
Workshop
   Quiz
   Exercises
PART IV: Lambda Expressions and STL Algorithms
Lesson 21: Understanding Function Objects
Function Objects and Predicates Â
Typical Applications of Function Objects Â
   Unary Functions
   Unary Predicates
   Binary Functions
   Binary Predicates
Summary
Q&A
Workshop
   Quiz
   Exercises
Lesson 22: Lambda Expressions
What Is a Lambda Expression?
How to Define a Lambda Expression
   Capturing Variables
   Parameters
   Return Types
A Lambda Expression for a Unary Function
A Lambda Expression for a Unary Predicate
A Lambda Expression with State via Capture Lists ([.])
A Lambda Expression for a Binary Function
A Lambda Expression for a Binary Predicate
Summary
Q&A
Workshop
   Quiz
   Exercises
Lesson 23: STL Algorithms
What Are STL Algorithms?
Classification of STL Algorithms
   Non-mutating Algorithms
   Mutating Algorithms
Usage of STL Algorithms
   Finding Elements, Given a Value or a Condition
   Counting Elements Given a Value or a Condition
   Searching for an Element or a Range in a Collection
   Initializing Elements in a Container to a Specific Value
   Using std::generate() to Initialize Elements to a Value Generated at Runtime
   Processing Elements in a Range by Using for_each()
   Performing Transformations on a Range by Using std::transform()
   Copy and Remove Operations
   Replacing Values and Replacing Elements Given a Condition Â
   Sorting and Searching in a Sorted Collection and Erasing Duplicates Â
   Partitioning a Range Â
   Inserting Elements in a Sorted Collection
   Performing Fold Operations Using std::accumulate() in C++20
C++20 Constrained Algorithms Â
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 24: Adaptive Containers: Stack and Queue
The Behavioral Characteristics of Stacks and Queues
   Stacks
   Queues
Using the STL stack Class
   Instantiating a Stack
   Stack Member Functions
   Insertion and Removal at the Top, Using push() and pop()
Using the STL queue Class
   Instantiating a Queue
   Member Functions of the queue Class
   Insertion at the End and Removal at the Beginning of a Queue via push() and pop()
Using the STL Priority Queue
   Instantiating the priority_queue Class
   Member Functions of priority_queue Â
   Insertion at the End and Removal at the Beginning of a Priority Queue via push() and pop()
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 25: Working with Bit Flags Using the STL
The bitset Class
   Instantiating std::bitset
Using std::bitset and Its Members
   Useful Operators in std::bitset Â
   std::bitset Member Methods Â
The vector<bool> Class Â
   Instantiating vector<bool>
   vector<bool> Functions and Operators
Summary
Q&A
Workshop
   Quiz Â
   Exercises
PART V: Advanced C++ Concepts
Lesson 26: Understanding Smart Pointers
What Are Smart Pointers?
   The Problem with Using Conventional (Raw) Pointers
   How Do Smart Pointers Help? Â
How Are Smart Pointers Implemented?
Types of Smart Pointers Â
   Deep Copy
   Copy on Write Â
   Reference-Counted Smart Pointers Â
   Reference-Linked Smart Pointers
   Destructive Copy
   Using std::unique_ptr
Popular Smart Pointer Libraries
Summary
Q&A
Workshop
   Quiz
   Exercises
Lesson 27: Using Streams for Input and Output
The Concept of Streams
Important C++ Stream Classes and Objects
Using std::cout for Writing Formatted Data to the Console
   Changing the Display Number Format by Using std::cout
   Aligning Text and Setting Field Width by Using std::cout
Using std::cin for Input
   Using std::cin for Input into a Plain Old Data Type
   Using std::cin::get for Input into the char* Buffer Â
   Using std::cin for Input into std::string
Using std::fstream for File Handling
   Opening and Closing a File Using open() and close()
   Creating and Writing a Text File by Using open() and the Operator <<
   Reading a Text File by Using open() and the Operator >> Â
   Writing to and Reading from a Binary File Â
Using std::stringstream for String Conversions
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 28: Exception Handling
What Is an Exception?
What Causes Exceptions?
Implementing Exception Safety via try and catch
   Using catch(.) to Handle All Exceptions
   Catching Exceptions of a Type Â
Throwing Exceptions of a Type by Using throw Â
How Exception Handling Works
Class std::exception Â
A Custom Exception Class Derived from std::exception
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 29: C++20 Concepts, Ranges, Views, and Adaptors
Concepts Â
   Using Concepts Provided by the Standard Library
   Defining Custom Concepts by Using the Keyword requires Â
   Using Concepts with Classes and Objects
The Ranges Library, Views, and Adaptors
   Views and Adaptors
   Adaptors Provided by the Ranges Library
   Combining Multiple Adaptors
Summary
Q&A
Workshop
   Quiz Â
   Exercises
Lesson 30: C++20 Threads
Multithreading
   What Is a Thread?
   Why Program Multithreaded Applications?
   Using the C++20 Thread Library
   How Can Threads Transact Data?
   Using Mutexes and Semaphores to Synchronize Threads
Summary
Q&A
Workshop
   Exercise
Lesson 31: C++20 Modules and C++23
Modules
   The Problem with #include<header>
   C++20 Modules Â
   Programming a Module Â
   Consuming a Module Â
Why import Module; Is Superior to the Preprocessor #include<header> Â
C++23 Expected Features
Learning C++ Doesn’t Stop Here!
   Online Documentation
   Communities for Guidance and Help
Summary
Q&A
Workshop
   Exercise
PART VI: Appendixes
Appendix A: Working with Numbers: Binary and Hexadecimal
Appendix B: C++ Keywords
Appendix C: Writing Great C++ Code
Appendix D: ASCII Codes
Appendix E: Answers
9780137334681Â Â TOCÂ Â 12/20/2021
Need help? Get in touch