Learn C++ by Example

Book description

Learn the latest features of modern C++ by coding eight engaging projects.

Don’t let the multitude of C++ updates intimidate you! Learn C++ by Example takes you through the major language changes since C++ 11, with each new feature demonstrated with a fun project or minigame. It’s perfect for beginners who know C++ basics, coders coming back to the language, or current C++ developers missing out on everything the language has to offer.

Inside Learn C++ by Example you’ll find important skills such as:

  • Utilizing the new C++ features from C++ 11 to 23
  • Effectively testing your C++ code
  • What happens “under the hood” of C++ code
  • Picking efficient algorithms and data structures
  • std::format, STL algorithms, ranges, and coroutines

Learn C++ by Example rapidly gets up to speed with C++’s updates and changes, and ensures you’ll stay ahead as the language continues to change and grow. You’ll learn about vectors and ranges by generating Pascal’s triangle, create a racing game with new special member functions, build a slot machine with parameter packs, and more.

About the Technology
C++ delivers the flexibility and performance you need for everything from low-level systems programming to secure financial applications and AAA game development. First introduced in 1985, the language is still evolving, with exciting changes in every new version. Whether you’re just getting started or you’re a veteran coder adding to your toolbox, the eight hands-on projects in this book will get you up to speed on modern C++ features and practices.

About the Book
Learn C++ by Example is a fun and practical way to start writing modern C++ code. It guides you through entertaining challenges, emphasizing features and techniques made possible by C++ 17, 20, and 23. You’ll learn about objects and arrays by creating a deck of playing cards, master the C++ random library for a number guess game, use the chrono library to create a countdown timer, and much more. Along the way you’ll also pick up valuable tips for testing, project organization, and other productivity skills.

What's Inside
  • New C++ features from C++ 11 to 23
  • Effectively test your C++ code
  • What happens “under the hood”
  • Efficient algorithms and data structures


About the Reader
Requires beginner to intermediate C++ skills.

About the Author
Frances Buontempo is an experienced C++ developer and the editor of ACCU’s Overload magazine.

The technical editor on this book was Timothy Jaap van Deurzen.

Quotes
Familiarity, and then mastery, sneak up on you as you work your way through. Suddenly you find yourself to be proficient!
- Guy Davidson, BSI Chair of Programming Languages

Breaks down new C++ features in a friendly and approachable way—something even experienced C++ developers can benefit from! Very enjoyable.
- Nina Dinka Ranns, C++ committee secretary

The hands-on approach and mini-projects will bring you both joy and insight.
- Sy Brand, Microsoft

A tour de force of modern C++!
- Silas S Brown, Oracle

A great book! The examples show you the most modern code possible.
- Andreas Fertig Unique Code GmbH

Table of contents

  1. inside front cover
  2. Learn C++ by Example
  3. Copyright
  4. dedication
  5. contents
  6. Front matter
    1. foreword
    2. preface
    3. acknowledgments
    4. about this book
      1. Who should read this book
      2. How this book is organized: A road map
      3. About the code
      4. liveBook discussion forum
      5. Other resources
    5. about the author
    6. about the cover illustration
  7. 1 Hello again, C++!
    1. 1.1 Why does C++ matter?
    2. 1.2 When should you use C++?
    3. 1.3 Why read this book?
    4. 1.4 How does this book teach C++?
      1. 1.4.1 Who this book is for
      2. 1.4.2 Hello, again, C++!
      3. 1.4.3 What you’ll learn from reading this book
    5. 1.5 Some pro tips
    6. Summary
  8. 2 Containers, iterators, and ranges
    1. 2.1 Creating and displaying a vector
    2. 2.2 Creating and displaying Pascal’s triangle
      1. 2.2.1 A reminder of Pascal’s triangle
      2. 2.2.2 Coding Pascal’s triangle
      3. 2.2.3 Move semantics and perfect forwarding
      4. 2.2.4 Using ranges to display the vector
      5. 2.2.5 Using format to display output
    3. 2.3 Properties of the triangle
      1. 2.3.1 Checking the first and last elements of each row
      2. 2.3.2 Checking the number of elements in each row
      3. 2.3.3 Checking the sum of the elements in a row
      4. 2.3.4 How many rows can we generate correctly?
      5. 2.3.5 Checking whether each row is symmetric
      6. 2.3.6 Highlighting odd numbers in a row
    4. Summary
  9. 3 Input of strings and numbers
    1. 3.1 Guessing a predetermined number
      1. 3.1.1 Accepting user input the hard way
      2. 3.1.2 Accepting optional numeric input
      3. 3.1.3 Validation and feedback using std::function and lambdas
    2. 3.2 Guessing a random number
      1. 3.2.1 Setting up a random number generator
      2. 3.2.2 Using the random number generator
    3. 3.3 Guessing a prime number
      1. 3.3.1 Checking whether the number is prime
      2. 3.3.2 Checking properties with static_assert
      3. 3.3.3 Generating a random prime number
      4. 3.3.4 Deciding which digits are correct
      5. 3.3.5 Providing different clues using std::function
    4. Summary
  10. 4 Time points, duration, and literals
    1. 4.1 How long until the last day of the year?
    2. 4.2 Understanding durations in detail
      1. 4.2.1 Ratios
      2. 4.2.2 Durations
      3. 4.2.3 Literal suffixes and operator / for readable code
      4. 4.2.4 Requirements and concepts
      5. 4.2.5 How many days until the last day of the year?
      6. 4.2.6 Using last to find how long to payday
      7. 4.2.7 Writing testable code
    3. 4.3 Input, output, and formatting
      1. 4.3.1 Parsing a date
      2. 4.3.2 Formatting time points and durations
    4. 4.4 Time zones
    5. Summary
  11. 5 Creating and using objects and arrays
    1. 5.1 Creating a deck of playing cards
      1. 5.1.1 Defining a card type using a scoped enum for the suit
      2. 5.1.2 Defining a card type using a strong type for the face value
      3. 5.1.3 Constructors and default values
      4. 5.1.4 Displaying playing cards
      5. 5.1.5 Using an array to make a deck of cards
      6. 5.1.6 Using generate to fill the array
      7. 5.1.7 Comparison operators and defaults
    2. 5.2 Higher-or-lower card game
      1. 5.2.1 Shuffling the deck
      2. 5.2.2 Building the game
      3. 5.2.3 Using std::variant to support cards or jokers
      4. 5.2.4 Building the game with an extended deck of cards
    3. Summary
  12. 6 Smart pointers and polymorphism
    1. 6.1 A class hierarchy
      1. 6.1.1 An abstract base class
      2. 6.1.2 A concrete class
      3. 6.1.3 Warming up for a race
      4. 6.1.4 Using type traits to check for special member functions
    2. 6.2 Writing and using derived classes in a vector
      1. 6.2.1 A blob moving randomly
      2. 6.2.2 Smart pointers
      3. 6.2.3 Race!
      4. 6.2.4 Some design considerations
    3. Summary
  13. 7 Associative containers and files
    1. 7.1 Hardcoded answer smash
      1. 7.1.1 Creating and using an std::map
      2. 7.1.2 Pairs, tuples, and structured bindings
      3. 7.1.3 A simple answer smash game
    2. 7.2 Associative containers
      1. 7.2.1 The map type in more detail
      2. 7.2.2 Using lower and upper bound to find a key more efficiently
      3. 7.2.3 Multimaps
    3. 7.3 File-based answer smash
      1. 7.3.1 Loading data from a file
      2. 7.3.2 Picking a word randomly using std::sample
      3. 7.3.3 Answer smash
    4. Summary
  14. 8 Unordered maps and coroutines
    1. 8.1 Randomly generated matching pennies
    2. 8.2 Matching pennies using an unordered_map
      1. 8.2.1 Unordered containers and std::hash
      2. 8.2.2 Using an unordered_map to make a prediction
      3. 8.2.3 The mind reader game
    3. 8.3 Coroutines
      1. 8.3.1 How to make a coroutine
      2. 8.3.2 A coroutine function
      3. 8.3.3 The coroutine’s return object
      4. 8.3.4 RAII and the rule of zero
      5. 8.3.5 Filling in the promise_type
      6. 8.3.6 Filling in the Task type
      7. 8.3.7 A coroutine mind reader
    4. Summary
  15. 9 Parameter packs and std::visit
    1. 9.1 The triangle numbers
      1. 9.1.1 Testing our triangle numbers with algorithms
      2. 9.1.2 Execution policies for algorithms
      3. 9.1.3 Mutable lambdas
      4. 9.1.4 More properties of the triangle numbers
    2. 9.2 A simple slot machine
      1. 9.2.1 Revision of constexpr and std::format
      2. 9.2.2 Using std::rotate to spin the reels
      3. 9.2.3 The simple slot machine
    3. 9.3 A better slot machine
      1. 9.3.1 Parameter packs and fold expressions
      2. 9.3.2 Using a parameter pack to find frequencies
      3. 9.3.3 A fairer payout
      4. 9.3.4 Allowing holds, nudges, or spins
      5. 9.3.5 Spinning reels with std::visit and std::views::zip
    4. Summary
  16. appendix. Further resources
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Chapter 8
    9. Chapter 9
  17. index

Product information

  • Title: Learn C++ by Example
  • Author(s): Frances Buontempo
  • Release date: March 2024
  • Publisher(s): Manning Publications
  • ISBN: 9781633438330