C++ is the backbone of many games, GUI-based applications, and operating systems. Learning C++ effectively is more than a matter of simply reading through theory, as the real challenge is understanding the fundamentals in depth and being able to use them in the real world. If you're looking to learn C++ programming efficiently, this Workshop is a comprehensive guide that covers all the core features of C++ and how to apply them. It will help you take the next big step toward writing efficient, reliable C++ programs.
The C++ Workshop begins by explaining the basic structure of a C++ application, showing you how to write and run your first program to understand data types, operators, variables and the flow of control structures. You'll also see how to make smarter decisions when it comes to using storage space by declaring dynamic variables during program runtime.
Moving ahead, you'll use object-oriented programming (OOP) techniques such as inheritance, polymorphism, and class hierarchies to make your code structure organized and efficient. Finally, you'll use the C++ standard library?s built-in functions and templates to speed up different programming tasks.
By the end of this C++ book, you will have the knowledge and skills to confidently tackle your own ambitious projects and advance your career as a C++ developer.
==================
What you will learn:-
Understand how a C++ program is written, executed, and compiled
Efficiently work with the essential C++ data types and variables
Build your own C++ applications by writing clear and error-free code
Grasp the core principles behind object-oriented programming
Simplify your code by using templates and the standard library
Debug logical errors and handle exceptions in your program
============================
Who this book is for:-
This Workshop is for anyone who is new to C++ who wants to build a strong foundation for C++ game programming or application development. Basic prior knowledge of data structures and OOP concepts, as well as experience in any other programming language, will help you grasp the concepts covered in this book more easily.
==========================
Dale Green is a computer programmer who has worked in a range of disciplines, from full stack web development to C++ programming in AAA video games. As a passionate video game programmer, Dale has worked on multiple AAA titles for both PC and console, self-published a PC title, and authored a book on Procedural Content Generation for C++ Game Development.
Kurt Guntheroth has been a software engineer for nearly 40 years and has a mix of embedded, Windows, and Linux experience. Kurt has worked with C++ for over 20 years. He's written a book on optimization of C++ programs called Optimized C++. He is working on a second book, tentatively called Nonstop C++, about building C++ programs that never crash.
Shaun Ross Mitchell is a games programmer with experience in many areas related to game development. Shaun has worked on platformers, tower defense, and VR games for consoles and PC. Shaun is the author of SDL Game Development.
Author(s): Dale Green, Kurt Guntheroth, Shaun Ross Mitchell
Edition: 1
Publisher: Packt
Year: 2021
Language: English
Pages: 605
Tags: C++20 C++17 C++14
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