Cover
FM
Copyright
Table of Contents
Preface
Chapter 1: Your First C++ Application
Introduction
Advantages of C++
Anatomy of a C++ Application
Exercise 1: Compiling Our First Application
C++ Build Pipeline
C++ Keywords
Keyword Examples
Preprocessor Directives
Include
Macros
Conditional Compilation
Exercise 2: Defining Values with Preprocessor Directives
Basic I/O Statements
Exercise 3: Reading User Details
Functions
Passing by Value, Passing by Reference
Why Are We Outputting 10?
Function Overloading
Default Parameters
Exercise 4: Functions
Activity 1: Writing Your Own C++ Application
Summary
Chapter 2: Control Flow
Introduction
if/else
Exercise 5: Implementing if/else Statements
Ternary Operator
Exercise 6: Creating a Simple Menu Program Using an if/else Statement
switch/case
Exercise 7: Refactor an if/else Chain into switch/case
Loops
while
Exercise 8: Implementing a while Loop
do while
Exercise 9: Implementing while and do while Loops with a False Condition
for
Exercise 10: Implementing a for Loop
Range-based for loop
Exercise 11: Generating Random Numbers Using Loops
break/continue
break
continue
Exercise 12: Making a Loop More Efficient Using break and continue
Activity 2: Creating a Number-Guessing Game Using Loops and Conditional Statements
Summary
Chapter 3: Built-In Data Types
Introduction
Data Types
Type Modifiers
Built-In Types
Reference Table
Exercise 13: Declaring Data Types
Containers
Arrays
Initialization
Accessing Elements
Array Memory
Exercise 14: Implementing Containers to Store Usernames
Multidimensional Arrays
Exercise 15: Using Multidimensional Arrays to Store More Data
Vectors
Accessing Elements
Exercise 16: Looping over a Vector
Initialization
Modifying Elements
Exercise 17: Modifying a Vector
Classes/Structs
Classes
Structs
Access Modifiers
Exercise 18: Using Accessibility Modifiers to Control Access
Constructors/Destructors
Exercise 19: Classes/Struct
Storage Lifetime
Exercise 20: Storage Lifetime Example
Static
Activity 3: Sign-Up Application
Summary
Chapter 4: Operators
Introduction
Arithmetic Operators
Exercise 21: The Prime Number Checker
Relational Operators
Equality
Comparison
Exercise 22: The Time-of-Day Calculator
Unary Operators
Exercise 23: A Pre-Increment/Post-Increment Example
Assignment Operators
Logical Operators
Exercise 24: Logical Operators Example
Operator Overloading
Exercise 25: Operator Overloading Example
Bitwise Operators
Activity 4: Fizz Buzz
Summary
Chapter 5: Pointers and References
Introduction
Memory Addresses
Pointers
Exercise 26: Pointers
Exercise 27: Dereferencing nullptr
Pointers to Arrays
Exercise 28: Pointers to Arrays
Pointer Arithmetic
Exercise 29: Pointer Arithmetic
Exercise 30: Incrementing Pointers
Pointers to Pointers
Exercise 31: Pointers to Pointers
References
Exercise 32: References
Exercise 33: Bad References
Pointers and References as Function Arguments
Exercise 34: Pointers as Function Arguments
Pointers to Classes or Structs
Exercise 35: Pointers to Class Instance
References as Function Arguments
Exercise 36: References as Function Arguments
Activity 5: Using Pointers and References to Manipulate an Array of Strings
Summary
Chapter 6: Dynamic Variables
Introduction
Dynamic Variables
Exercise 37: Creating and Deleting Dynamic Variables of Basic Types
Exercise 38: Creating and Deleting Dynamic Class Instances
Dynamic Arrays
Exercise 39: Creating and Deleting Dynamic Arrays of Basic Types
Exercise 40: Creating and Deleting Dynamic Arrays of Classes
Seven Dynamic Variable Sins
Exercise 41: Using a Dynamic Variable before Creating It
Exercise 42: Using a Dynamic Variable after Deleting It
Exercise 43: Not Deleting a Dynamic Variable
Exercise 44: Overwriting a Pointer to a Dynamic Variable
Exercise 45: Deleting a Dynamic Variable Twice
Exercise 46: Deleting a Dynamic Array with delete instead of delete[]
Exercise 47: Deleting a Dynamic Variable with delete[] instead of delete
Dynamic Containers
Linked Lists
Binary Search Trees
Recursive Data Structures
Visiting Items in a Recursive Data Structure
Finding Items
Adding Items
Deleting Dynamic Items
Exercise 48: Creating Linked Lists of Class Instances
Activity 6: Creating Binary Search Trees of Class Instances
Summary
Chapter 7: Ownership and Lifetime of Dynamic Variables
Introduction
The Lifetime of Dynamic Variables
Ownership of Dynamic Variables
Resource Acquisition Is Initialization (RAII)
Exercise 49: Lifetime Demonstration
Exercise 50: Owned Pointers in Data Structures
Exercise 51: Transfer of Ownership
Smart Pointers — Automated Ownership of Dynamic Variables
unique_ptr<>
Exercise 52: Working with unique_ptr<>
make_unique()
Exercise 53: Using make_unique()
unique_ptr<> as a Class Member Variable
Exercise 54: Using unique_ptr<> as a Class Member Variable
unique_ptr<> in Function Arguments and Return Values
Exercise 55: Using unique_ptr<> in Function Return Values
Shared Ownership of Dynamic Variables
Exercise 56: Using shared_ptr<>
make_shared()
Exercise 57: Using make_shared()
Activity 7: Storing the Words of a Book Using Dynamic Variables
Summary
Chapter 8: Classes and Structs
Introduction
Classes versus Structs
Unions
Constructors and Destructors
Constructors
Default Constructors
Exercise 58: Defining a Default Constructor
Parameterized Constructors
Exercise 59: Defining a Parameterized Constructor
Copy Constructors
Shallow Copy or Deep Copy
Exercise 60: Defining a Copy Constructor
Copy Assignment Operator
Exercise 61: Overloading the Assignment Operator
Destructors
Activity 8: Creating a VideoClip Class
Summary
Chapter 9: Object-Oriented Principles
Introduction
Classes and OOP
S in SOLID
Exercise 62: Creating a Class that Prints Values
Encapsulation
Exercise 63: Creating a Position Class with Private Member Variables
Getters and Setters
Exercise 64: Getters and Setters in a Position Class
Return Value or Reference
Return by Value
Return by Reference
const
Returning const References
Const Functions
Abstraction
Activity 9: A Basic RPG Combat System
Summary
Chapter 10: Advanced Object-Oriented Principles
Introduction
Inheritance
Exercise 65: Inheritance
Multiple Inheritance
Exercise 66: Multiple Inheritance
Access Modifiers and Inheritance
Exercise 67: Access Modifiers and Inheritance
Virtual Functions
Pure Virtual Functions/Abstract Classes
Exercise 68: Virtual Functions
Polymorphism
Exercise 69: Polymorphism
Casting between Types
Static Cast
Dynamic Cast
C-Style Cast
Exercise 70: Casting
Activity 10: An Encyclopedia Application
Summary
Chapter 11: Templates
Introduction
Syntax
Template Classes
Exercise 71: Creating Different Types for the Position Objects
Multiple Template Parameters
Template Functions
Exercise 72: Comparing Position Values Using a Template Function
Template Specialization
Additional Template Considerations
Forcing Accepted Types
Templates and Default Constructors
Creating a Generic Queue
What Is a Queue?
Implementing Constructors and Destructors in the Queue
Dynamic Memory
Allocators
Resizing and Appending
Pushing and Popping
Finalizing and Testing
Activity 11: Creating a Generic Stack
Summary
Chapter 12: Containers and Iterators
Introduction
Containers
A String Is a Container
String Constructors
Exercise 73: Creating Strings
Assigning to Strings
Operations on Strings
Iterators
Further Research
Exercise 74: Is It a Palindrome?
Vectors-Handy, Resizable Arrays
Vector Constructors
Vector Assignment
Exercise 75: Accessing Elements in a Vector
Operations on Vectors
Searching Vectors
Exercise 76: Sorting Vectors with a Custom Comparison
Map/Unordered Map: Our Associative Containers
Constructing Maps and Unordered Maps
Operations on Maps and Unordered Maps
Exercise 77: Map Quiz
Sets/Multisets
Constructors
Exercise 78: A Custom Comparator for a Set
Operations
Exercise 79: Using a Set to Get the Number of Unique Elements in a Multiset
Queues/Stacks
Constructors
Operations
Activity 12: Converting RPG Combat to Use Standard Library Containers
Summary
Chapter 13: Exception Handling in C++
Introduction
Responding to Unexpected Events
Throwing Exceptions
Uncaught Exceptions
Exercise 80: Throwing Uncaught Exceptions
Catching Exceptions
Exercise 81: try/catch Blocks
Exercise 82: Exceptions Thrown by C++
Unwinding the Stack
RAII (Resource Acquisition Is Initialization) and Exception Handling
Exercise 83: Unwinding the Stack
Activity 13: Handling Exceptions
Summary
Appendix
Index
You already know you want to learn C++ development, and a smarter way to learn C++ is to learn by doing. The C++ Workshop focuses on building up your practical skills so that you can develop high-performance software applications or even your own independent games with effective modern C++. You'll learn from real examples that lead to real results.
Throughout The C++ Workshop, you'll take an engaging step-by-step approach to understanding C++ code. You won't have to sit through any unnecessary theory. If you're short on time you can jump into a single exercise each day or spend an entire weekend learning about advanced object-oriented principles. It's your choice. Learning on your terms, you'll build up and reinforce key skills in a way that feels rewarding.
Every physical print copy of The C++ Workshop unlocks access to the interactive edition. With videos detailing all exercises and activities, you'll always have a guided solution. You can also benchmark yourself against assessments, track progress, and receive content updates. You'll even earn a secure credential that you can share and verify online upon completion. It's a premium learning experience that's included with your printed copy. To redeem, follow the instructions located at the start of your C++ book.
Fast-paced and direct, The C++ Workshop is the ideal companion for C++ beginners. You'll build and iterate on your code like a software developer, learning along the way. This process means that you'll find that your new skills stick, embedded as best practice. A solid foundation for the years ahead.