Expert C++

Book description

Design and architect real-world scalable C++ applications by exploring advanced techniques in low-level programming, object-oriented programming (OOP), the Standard Template Library (STL), metaprogramming, and concurrency

Key Features

  • Design professional-grade, maintainable apps by learning advanced concepts such as functional programming, templates, and networking
  • Apply design patterns and best practices to solve real-world problems
  • Improve the performance of your projects by designing concurrent data structures and algorithms

Book Description

C++ has evolved over the years and the latest release – C++20 – is now available. Since C++11, C++ has been constantly enhancing the language feature set. With the new version, you'll explore an array of features such as concepts, modules, ranges, and coroutines. This book will be your guide to learning the intricacies of the language, techniques, C++ tools, and the new features introduced in C++20, while also helping you apply these when building modern and resilient software.

You'll start by exploring the latest features of C++, and then move on to advanced techniques such as multithreading, concurrency, debugging, monitoring, and high-performance programming. The book will delve into object-oriented programming principles and the C++ Standard Template Library, and even show you how to create custom templates. After this, you'll learn about different approaches such as test-driven development (TDD), behavior-driven development (BDD), and domain-driven design (DDD), before taking a look at the coding best practices and design patterns essential for building professional-grade applications. Toward the end of the book, you will gain useful insights into the recent C++ advancements in AI and machine learning.

By the end of this C++ programming book, you'll have gained expertise in real-world application development, including the process of designing complex software.

What you will learn

  • Understand memory management and low-level programming in C++ to write secure and stable applications
  • Discover the latest C++20 features such as modules, concepts, ranges, and coroutines
  • Understand debugging and testing techniques and reduce issues in your programs
  • Design and implement GUI applications using Qt5
  • Use multithreading and concurrency to make your programs run faster
  • Develop high-end games by using the object-oriented capabilities of C++
  • Explore AI and machine learning concepts with C++

Who this book is for

This C++ book is for experienced C++ developers who are looking to take their knowledge to the next level and perfect their skills in building professional-grade applications.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Expert C++
  3. Dedication
  4. About Packt
    1. Why subscribe?
  5. Contributors
    1. About the authors
    2. About the reviewers
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  7. Section 1: Under the Hood of C++ Programming
  8. Introduction to Building C++ Applications
    1. Technical requirements
    2. Introduction to C++20
      1. Concepts
      2. Coroutines
      3. Ranges
      4. More C++20 features
    3. Building and running programs
    4. Understanding preprocessing
      1. Header files
      2. Using modules in C++20
    5. Understanding Compiling
      1. Tokenization
      2. Syntax analysis
      3. Semantic analysis
      4. Intermediate code generation
      5. Optimization
      6. Machine code generation
      7. Platforms and object files
    6. Introducing Linking
      1. Linking libraries
    7. Summary
    8. Questions
    9. Further reading
  9. Low-Level Programming with C++
    1. Technical requirements
    2. Program execution
      1. main()
      2. Special properties of main()
      3. constexpr
      4. Recursion
    3. Working with data
      1. Virtual memory
      2. Addressing
      3. Data types
      4. Pointers
      5. Memory segments
      6. The heap
      7. Arrays
    4. Control flow
      1. Conditionals
      2. The switch statement
      3. Replacing conditionals with function pointers
      4. Functions as types
      5. Loops
    5. Summary
    6. Questions
    7. Further reading
  10. Details of Object-Oriented Programming
    1. Technical requirements
    2. Understanding objects
      1. Low-level details of objects
      2. High-level details of objects
        1. State
        2. Identity
        3. Behavior
      3. Mimicking a class
    3. Working with classes
      1. Classes from the compiler perspective
      2. Initialization and destruction
      3. Copying objects
      4. Moving objects
        1. Lvalue references
        2. Rvalue references
        3. Notes on operator overloading
      5. Encapsulation and the public interface
      6. Structs in C++
    4. Class relationships
      1. Aggregation and composition
      2. Inheritance
      3. Inheritance from the compiler perspective
      4. Composition versus inheritance
      5. Protected inheritance
    5. Polymorphism
      1. Virtual functions under the hood
    6. Design patterns
    7. Summary
    8. Questions
    9. Further reading
  11. Understanding and Designing Templates
    1. Technical requirements
    2. Exploring function and class templates
      1. Motivation
      2. Function templates
        1. Syntax
        2. Instantiation
          1. Explicit instantiations
          2. Implicit instantiations
          3. Deduction
        3. Specialization and overloading
      3. Class templates
        1. Syntax
        2. Instantiation
          1. Explicit instantiation
          2. Implicit instantiation
        3. Specialization
    3. Understanding variadic templates
      1. Syntax
      2. Examples
    4. Exploring template parameters and arguments
      1. Template parameters
        1. Non-type template parameter
        2. Type template parameter
        3. Template template parameter
      2. Template arguments
        1. Template non-type arguments
        2. Template type arguments
        3. Template template arguments
        4. Default template arguments
    5. Exploring traits
      1. Type trait implementation
        1. boost::is_void
        2. boost::is_pointer
      2. Optimizing algorithms using traits
    6. Exploring template metaprogramming
      1. Compile-time computation
      2. Compile-time code optimization
      3. Static polymorphism
    7. Summary
    8. Questions
    9. Further reading
  12. Memory Management and Smart Pointers
    1. Technical requirements
    2. Understanding computer memory
      1. Designing a memory storage device
      2. Understanding computer memory from a higher-level perspective
        1. Registers
        2. Cache memory
        3. Main memory
        4. Permanent storage
    3. The basics of memory management
      1. An example of memory management
    4. Using smart pointers
      1. Leveraging the RAII idiom
      2. std::unique_ptr
      3. std::shared_ptr and std::weak_ptr
    5. Garbage collection
      1. Using allocators
    6. Summary
    7. Questions
    8. Further reading
  13. Section 2: Designing Robust and Efficient Applications
  14. Digging into Data Structures and Algorithms in STL
    1. Technical requirements
    2. Data structures
      1. Sequential data structures
      2. Node-based data structures
      3. Containers in memory
    3. STL containers
      1. Using std::vector and std::list
      2. Using container adapters
      3. Iterating containers
    4. Concepts and iterators
      1. Understanding concepts
      2. Using iterators in C++20
    5. Mastering algorithms
      1. Searching
      2. Binary search
      3. Sorting
    6. Exploring trees and graphs
      1. Hash tables
      2. Graphs
      3. Strings
    7. Summary
    8. Questions
    9. Further reading
  15. Functional Programming
    1. Technical requirements
    2. Unveiling functional programming
      1. Using ranges
      2. Why use functional programming?
    3. Principles of functional programming
      1. Pure functions
      2. Higher-order functions
      3. Folding
    4. Diving deeper into recursion
      1. Head recursion
      2. Tail recursion
    5. Metaprogramming in functional C++
    6. Summary
    7. Questions
    8. Further reading
  16. Concurrency and Multithreading
    1. Technical requirements
    2. Understanding concurrency and multithreading
      1. Processes
        1. Challenges with processes
      2. Threads
    3. Working with threads
      1. Waiting for threads
      2. Using std::jthread
      3. Passing arguments to the thread function
    4. Managing threads and sharing data
      1. Sharing data
      2. Protecting shared data using a mutex
      3. Avoiding deadlocks
      4. Designing concurrent code
    5. Introducing coroutines
    6. Summary
    7. Questions
    8. Further reading
  17. Designing Concurrent Data Structures
    1. Technical requirements
    2. A closer look at data races
      1. A synchronized increment
      2. Implementing a thread-safe stack
    3. Designing lock-free data structures
      1. Using atomic types
      2. Operations on atomic types
      3. Designing a lock-free stack
      4. More operations on atomics
    4. Summary
    5. Questions
    6. Further reading
  18. Designing World-Ready Applications
    1. Technical requirements
    2. Project development life cycle
      1. Requirements gathering and analysis
      2. Specification creation
      3. Design and test planning
        1. Decomposing entities
      4. Coding
      5. Testing and stabilization
      6. Release and maintenance
    3. Diving into the design process
      1. Using SOLID principles
        1. The single responsibility principle
        2. The open-closed principle
        3. The Liskov substitution principle
        4. The interface segregation principle
        5. The dependency inversion principle
      2. Using domain-driven design
    4. Leveraging design patterns
      1. The repository pattern
      2. The factory pattern
    5. Summary
    6. Questions
    7. Further reading
  19. Designing a Strategy Game Using Design Patterns
    1. Technical requirements
    2. Introduction to game design
      1. Introduction to the Readers and Disturbers game
      2. Strategy game components
      3. Interaction between the components
    3. Designing the game
      1. Designing character units
      2. Designing buildings
      3. Designing game controllers
        1. Concurrent actions
        2. The game event loop
    4. Using design patterns
      1. The Command pattern
      2. The Observer pattern
      3. The Flyweight pattern
      4. The Prototype pattern
    5. Designing the game loop
    6. Summary
    7. Questions
    8. Further reading
  20. Networking and Security
    1. Technical requirements
    2. Discovering network programming in C++
      1. Network applications under the hood
      2. Programming network applications using sockets
      3. Network protocols
    3. Designing a network application
      1. Using POSIX sockets
      2. Implementing a POSIX socket wrapper class
    4. Securing the C++ code
      1. Securing network applications
    5. Summary
    6. Questions
    7. Further reading
  21. Debugging and Testing
    1. Technical requirements
    2. Understanding the root cause of an issue
      1. The RCA overview
      2. Prevention is better than the cure – a good coding behavior
        1. Uninitialized variable problem
        2. Side effects in compound expressions
        3. Mixing signed and unsigned problems
        4. Order of evaluation problem
        5. Compile-time checking versus runtime checking
        6. Avoiding memory leaks
    3. Debugging C++ programs
      1. Tools to debug a C/C++ program
      2. GDB overview
      3. Examples of GDB
        1. Setting breakpoints and inspection variable values
        2. Function breakpoints, conditional breakpoints, watchpoint, and the continue and finish commands
        3. Logging gdb into a text file
      4. Practical debugging strategies
    4. Understanding static and dynamic analysis
      1. Static analysis
      2. Dynamic analysis
    5. Exploring unit testing, TDD, and BDD
      1. Unit testing
      2. TDD
        1. Example of TDD
          1. Step 1 – writing a failing test
          2. Step 2 – developing code to let the test pass
          3. Step 3 – refactoring
      3. BDD
    6. Summary
    7. Further reading
    8. Exercises and questions
  22. Graphical User Interface with Qt
    1. Technical requirements
    2. Understanding cross-platform GUI programming
      1. Using C++ as Java
      2. Qt's cross-platform model
      3. Writing a simple application
    3. Discovering Qt
      1. Grasping signals and slots
      2. Understanding Model/View programming
    4. Using Qt widgets
      1. Common Qt widgets
      2. Composing widgets using layouts
    5. Summary
    6. Questions
    7. Further reading
  23. Section 3: C++ in the AI World
  24. Using C++ in Machine Learning Tasks
    1. Technical requirements
    2. Introduction to AI
      1. Computer vision
      2. NLP
      3. Knowledge reasoning
      4. ML
    3. Understanding ML
      1. Designing an algorithm that learns
      2. Categories of ML
      3. Applications of ML
    4. Neural networks
      1. Clustering
      2. Regression analysis
    5. C++ and ML
    6. Summary
    7. Questions
    8. Further reading
  25. Implementing a Dialog-Based Search Engine
    1. Technical requirements
    2. Understanding the structure of a search engine
      1. Providing a convenient user interface
      2. Dealing with typos in queries
      3. Crawling websites
    3. Indexing documents
      1. Tokenizing documents
      2. Sorting the results
    4. Building a recommendation engine
      1. Using a knowledge graph
    5. Implementing a dialog-based search engine
      1. Implementing the query parser
      2. Implementing the query processor
    6. Summary
    7. Questions
    8. Further reading
  26. Assessments
    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
    10. Chapter 10
    11. Chapter 11
    12. Chapter 12
    13. Chapter 13
    14. Chapter 14
    15. Chapter 15
    16. Chapter 16
  27. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Expert C++
  • Author(s): Vardan Grigoryan, Shunguang Wu
  • Release date: April 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781838552657