0%

Book Description

Cut through the noise and get real results with a step-by-step approach to learning C++ development

Key Features

  • Ideal for the C++ beginner who is getting started for the first time
  • A C++ tutorial with step-by-step exercises and activities that help build key skills
  • Structured to let you progress at your own pace, on your own terms
  • Use your physical print copy to redeem free access to the online interactive edition

Book Description

You already know you want to learn C++ development, and a smarter way to learn C++ 11 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.

What you will learn

  • Get to grips with fundamental concepts and conventions of C++ 11
  • Learn about best practices for clean code and how to avoid common pitfalls
  • Reuse and reduce common code using the C++ standard library
  • Debug and compile logical errors and handle exceptions in your programs

Who this book is for

Our goal at Packt is to help you be successful, in whatever it is you choose to do. The C++ Workshop is an ideal C++ tutorial for the C++ beginner who is just getting started. Pick up a Workshop today, and let Packt help you develop skills that stick with you for life.

Table of Contents

  1. Preface
    1. About the Book
      1. About the Chapters
      2. Conventions
      3. Before You Begin
      4. Online Compilers
      5. Installing the Code Bundle
  2. 1. Your First C++ Application
    1. Introduction
      1. Advantages of C++
    2. Anatomy of a C++ Application
      1. Exercise 1: Compiling Our First Application
      2. C++ Build Pipeline
    3. C++ Keywords
      1. Keyword Examples
    4. Preprocessor Directives
      1. Include
      2. Macros
      3. Conditional Compilation
      4. Exercise 2: Defining Values with Preprocessor Directives
    5. Basic I/O Statements
      1. Exercise 3: Reading User Details
    6. Functions
      1. Passing by Value, Passing by Reference
        1. Why Are We Outputting 10?
      2. Function Overloading
      3. Default Parameters
      4. Exercise 4: Functions
      5. Activity 1: Writing Your Own C++ Application
    7. Summary
  3. 2. Control Flow
    1. Introduction
    2. if/else
      1. Exercise 5: Implementing if/else Statements
      2. Ternary Operator
      3. Exercise 6: Creating a Simple Menu Program Using an if/else Statement
    3. switch/case
      1. Exercise 7: Refactor an if/else Chain into switch/case
    4. Loops
      1. while
      2. Exercise 8: Implementing a while Loop
      3. do while
      4. Exercise 9: Implementing while and do while Loops with a False Condition
      5. for
      6. Exercise 10: Implementing a for Loop
      7. Range-based for loop
      8. Exercise 11: Generating Random Numbers Using Loops
    5. break/continue
      1. break
      2. continue
      3. Exercise 12: Making a Loop More Efficient Using break and continue
      4. Activity 2: Creating a Number-Guessing Game Using Loops and Conditional Statements
    6. Summary
  4. 3. Built-In Data Types
    1. Introduction
    2. Data Types
      1. Type Modifiers
      2. Built-In Types
      3. Reference Table
      4. Exercise 13: Declaring Data Types
    3. Containers
      1. Arrays
      2. Initialization
      3. Accessing Elements
      4. Array Memory
      5. Exercise 14: Implementing Containers to Store Usernames
      6. Multidimensional Arrays
      7. Exercise 15: Using Multidimensional Arrays to Store More Data
      8. Vectors
      9. Accessing Elements
      10. Exercise 16: Looping over a Vector
      11. Initialization
      12. Modifying Elements
      13. Exercise 17: Modifying a Vector
    4. Classes/Structs
      1. Classes
      2. Structs
      3. Access Modifiers
      4. Exercise 18: Using Accessibility Modifiers to Control Access
      5. Constructors/Destructors
      6. Exercise 19: Classes/Struct
    5. Storage Lifetime
      1. Exercise 20: Storage Lifetime Example
      2. Static
      3. Activity 3: Sign-Up Application
    6. Summary
  5. 4. Operators
    1. Introduction
    2. Arithmetic Operators
      1. Exercise 21: The Prime Number Checker
    3. Relational Operators
      1. Equality
      2. Comparison
      3. Exercise 22: The Time-of-Day Calculator
    4. Unary Operators
      1. Exercise 23: A Pre-Increment/Post-Increment Example
    5. Assignment Operators
    6. Logical Operators
      1. Exercise 24: Logical Operators Example
    7. Operator Overloading
      1. Exercise 25: Operator Overloading Example
    8. Bitwise Operators
      1. Activity 4: Fizz Buzz
    9. Summary
  6. 5. Pointers and References
    1. Introduction
    2. Memory Addresses
      1. Pointers
      2. Exercise 26: Pointers
      3. Exercise 27: Dereferencing nullptr
      4. Pointers to Arrays
      5. Exercise 28: Pointers to Arrays
      6. Pointer Arithmetic
      7. Exercise 29: Pointer Arithmetic
      8. Exercise 30: Incrementing Pointers
      9. Pointers to Pointers
      10. Exercise 31: Pointers to Pointers
    3. References
      1. Exercise 32: References
      2. Exercise 33: Bad References
      3. Pointers and References as Function Arguments
      4. Exercise 34: Pointers as Function Arguments
      5. Pointers to Classes or Structs
      6. Exercise 35: Pointers to Class Instance
      7. References as Function Arguments
      8. Exercise 36: References as Function Arguments
      9. Activity 5: Using Pointers and References to Manipulate an Array of Strings
    4. Summary
  7. 6. Dynamic Variables
    1. Introduction
    2. Dynamic Variables
      1. Exercise 37: Creating and Deleting Dynamic Variables of Basic Types
      2. Exercise 38: Creating and Deleting Dynamic Class Instances
      3. Dynamic Arrays
      4. Exercise 39: Creating and Deleting Dynamic Arrays of Basic Types
      5. Exercise 40: Creating and Deleting Dynamic Arrays of Classes
    3. Seven Dynamic Variable Sins
      1. Exercise 41: Using a Dynamic Variable before Creating It
      2. Exercise 42: Using a Dynamic Variable after Deleting It
      3. Exercise 43: Not Deleting a Dynamic Variable
      4. Exercise 44: Overwriting a Pointer to a Dynamic Variable
      5. Exercise 45: Deleting a Dynamic Variable Twice
      6. Exercise 46: Deleting a Dynamic Array with delete instead of delete[]
      7. Exercise 47: Deleting a Dynamic Variable with delete[] instead of delete
    4. Dynamic Containers
      1. Linked Lists
      2. Binary Search Trees
      3. Recursive Data Structures
      4. Visiting Items in a Recursive Data Structure
      5. Finding Items
      6. Adding Items
      7. Deleting Dynamic Items
      8. Exercise 48: Creating Linked Lists of Class Instances
      9. Activity 6: Creating Binary Search Trees of Class Instances
    5. Summary
  8. 7. Ownership and Lifetime of Dynamic Variables
    1. Introduction
      1. The Lifetime of Dynamic Variables
      2. Ownership of Dynamic Variables
      3. Resource Acquisition Is Initialization (RAII)
      4. Exercise 49: Lifetime Demonstration
      5. Exercise 50: Owned Pointers in Data Structures
      6. Exercise 51: Transfer of Ownership
    2. Smart Pointers — Automated Ownership of Dynamic Variables
      1. unique_ptr<>
      2. Exercise 52: Working with unique_ptr<>
      3. make_unique()
      4. Exercise 53: Using make_unique()
      5. unique_ptr<> as a Class Member Variable
      6. Exercise 54: Using unique_ptr<> as a Class Member Variable
      7. unique_ptr<> in Function Arguments and Return Values
      8. Exercise 55: Using unique_ptr<> in Function Return Values
    3. Shared Ownership of Dynamic Variables
      1. Exercise 56: Using shared_ptr<>
      2. make_shared()
      3. Exercise 57: Using make_shared()
      4. Activity 7: Storing the Words of a Book Using Dynamic Variables
    4. Summary
  9. 8. Classes and Structs
    1. Introduction
    2. Classes versus Structs
    3. Unions
    4. Constructors and Destructors
      1. Constructors
      2. Default Constructors
      3. Exercise 58: Defining a Default Constructor
      4. Parameterized Constructors
      5. Exercise 59: Defining a Parameterized Constructor
      6. Copy Constructors
      7. Shallow Copy or Deep Copy
      8. Exercise 60: Defining a Copy Constructor
      9. Copy Assignment Operator
      10. Exercise 61: Overloading the Assignment Operator
      11. Destructors
      12. Activity 8: Creating a VideoClip Class
    5. Summary
  10. 9. Object-Oriented Principles
    1. Introduction
    2. Classes and OOP
      1. S in SOLID
      2. Exercise 62: Creating a Class that Prints Values
      3. Encapsulation
      4. Exercise 63: Creating a Position Class with Private Member Variables
      5. Getters and Setters
      6. Exercise 64: Getters and Setters in a Position Class
    3. Return Value or Reference
      1. Return by Value
      2. Return by Reference
      3. const
      4. Returning const References
      5. Const Functions
      6. Abstraction
      7. Activity 9: A Basic RPG Combat System
    4. Summary
  11. 10. Advanced Object-Oriented Principles
    1. Introduction
    2. Inheritance
      1. Exercise 65: Inheritance
    3. Multiple Inheritance
      1. Exercise 66: Multiple Inheritance
      2. Access Modifiers and Inheritance
      3. Exercise 67: Access Modifiers and Inheritance
      4. Virtual Functions
      5. Pure Virtual Functions/Abstract Classes
      6. Exercise 68: Virtual Functions
    4. Polymorphism
      1. Exercise 69: Polymorphism
    5. Casting between Types
      1. Static Cast
      2. Dynamic Cast
      3. C-Style Cast
      4. Exercise 70: Casting
      5. Activity 10: An Encyclopedia Application
    6. Summary
  12. 11. Templates
    1. Introduction
    2. Syntax
      1. Template Classes
      2. Exercise 71: Creating Different Types for the Position Objects
      3. Multiple Template Parameters
      4. Template Functions
      5. Exercise 72: Comparing Position Values Using a Template Function
      6. Template Specialization
      7. Additional Template Considerations
        1. Forcing Accepted Types
        2. Templates and Default Constructors
    3. Creating a Generic Queue
      1. What Is a Queue?
      2. Implementing Constructors and Destructors in the Queue
      3. Dynamic Memory
        1. Allocators
      4. Resizing and Appending
      5. Pushing and Popping
      6. Finalizing and Testing
      7. Activity 11: Creating a Generic Stack
    4. Summary
  13. 12. Containers and Iterators
    1. Introduction
    2. Containers
      1. A String Is a Container
      2. String Constructors
      3. Exercise 73: Creating Strings
      4. Assigning to Strings
      5. Operations on Strings
      6. Iterators
      7. Further Research
      8. Exercise 74: Is It a Palindrome?
    3. Vectors-Handy, Resizable Arrays
      1. Vector Constructors
      2. Vector Assignment
      3. Exercise 75: Accessing Elements in a Vector
      4. Operations on Vectors
      5. Searching Vectors
      6. Exercise 76: Sorting Vectors with a Custom Comparison
    4. Map/Unordered Map: Our Associative Containers
      1. Constructing Maps and Unordered Maps
      2. Operations on Maps and Unordered Maps
      3. Exercise 77: Map Quiz
    5. Sets/Multisets
      1. Constructors
      2. Exercise 78: A Custom Comparator for a Set
      3. Operations
      4. Exercise 79: Using a Set to Get the Number of Unique Elements in a Multiset
    6. Queues/Stacks
      1. Constructors
      2. Operations
      3. Activity 12: Converting RPG Combat to Use Standard Library Containers
    7. Summary
  14. 13. Exception Handling in C++
    1. Introduction
    2. Responding to Unexpected Events
      1. Throwing Exceptions
      2. Uncaught Exceptions
      3. Exercise 80: Throwing Uncaught Exceptions
      4. Catching Exceptions
      5. Exercise 81: try/catch Blocks
      6. Exercise 82: Exceptions Thrown by C++
      7. Unwinding the Stack
      8. RAII (Resource Acquisition Is Initialization) and Exception Handling
      9. Exercise 83: Unwinding the Stack
      10. Activity 13: Handling Exceptions
    3. Summary
  15. Appendix
    1. 1. Your First C++ Application
      1. Activity 1: Write Your Own C++ Application
    2. 2. Control Flow
      1. Activity 2: Creating a Number-Guessing Game Using Loops and Conditional Statements
    3. 3. Built-in Data Types
      1. Activity 3: Sign-Up Application
    4. 4. Operators
      1. Activity 4: Fizz Buzz
    5. 5. Pointers and References
      1. Activity 5: Using Pointers and References to Manipulate an Array of Strings
    6. 6. Dynamic Variables
      1. Activity 6: Creating Binary Search Trees of Class Instances
    7. 7. Ownership and Lifetime Of Dynamic Variables
      1. Activity 7: Storing the Words of a Book Using Dynamic Variables
    8. 8. Classes and Structs
      1. Activity 8: Creating a Video Clip Class
    9. 9. Object-Oriented Principles
      1. Activity 9: A Basic RPG Combat System
    10. 10. Advanced Object-Oriented Principles
      1. Activity 10: An Encyclopedia Application
    11. 11. Templates
      1. Activity 11: Creating a Generic Stack
    12. 12. Containers and Iterators
      1. Activity 12: Converting RPG Combat to Use Standard Library Containers
    13. 13. Exception Handling in C++
      1. Activity 13: Handling Exceptions
3.145.93.136