You are previewing Beginning C++ Through Game Programming, Third Edition.
O'Reilly logo
Beginning C++ Through Game Programming, Third Edition

Book Description

BEGINNING C++ THROUGH GAME PROGRAMMING, THIRD EDITION approaches learning C++ from the unique and fun perspective of games. Written for the beginning game developer or programmer, the book assumes no previous programming experience and each new skill and concept is taught using simple language and step-by-step instructions. Readers will complete small projects in each chapter to reinforce what they've learned and a final project at the end combines all of the major topics covered in the book. Featuring twenty five percent new material, this third edition covers all the latest technology and advances.

Table of Contents

  1. Copyright
    1. Dedication
  2. Acknowledgments
  3. About the Author
  4. Introduction
    1. Who This Book Is For
    2. How This Book Is Organized
    3. Conventions Used in This Book
    4. Source Code for the Programs in This Book
    5. A Word about Compilers
  5. 1. Types, Variables, and Standard I/O: Lost Fortune
    1. Introducing C++
      1. Using C++ for Games
      2. Creating an Executable File
      3. Dealing with Errors
      4. Understanding the ISO Standard
    2. Writing Your First C++ Program
      1. Introducing the Game Over Program
      2. Commenting Code
      3. Using Whitespace
      4. Including Other Files
      5. Defining the main() Function
      6. Displaying Text through the Standard Output
      7. Terminating Statements
      8. Returning a Value from main()
    3. Working with the std Namespace
      1. Introducing the Game Over 2.0 Program
      2. Employing a using Directive
      3. Introducing the Game Over 3.0 Program
      4. Employing using Declarations
      5. Understanding When to Employ using
    4. Using Arithmetic Operators
      1. Introducing the Expensive Calculator Program
      2. Adding, Subtracting, and Multiplying
      3. Understanding Integer and Floating Point Division
      4. Using the Modulus Operator
      5. Understanding Order of Operations
    5. Declaring and Initializing Variables
      1. Introducing the Game Stats Program
      2. Understanding Fundamental Types
      3. Understanding Type Modifiers
      4. Declaring Variables
      5. Naming Variables
      6. Assigning Values to Variables
        1. Assigning Values to Integer Variables
        2. Assigning Values to Floating Point Variables
        3. Assigning Values to Character Variables
        4. Assigning Values to Boolean Variables
      7. Initializing Variables
      8. Displaying Variable Values
      9. Getting User Input
      10. Defining New Names for Types
      11. Understanding Which Types to Use
    6. Performing Arithmetic Operations with Variables
      1. Introducing the Game Stats 2.0 Program
      2. Altering the Value of a Variable
      3. Using Combined Assignment Operators
      4. Using Increment and Decrement Operators
      5. Dealing with Integer Wrap Around
    7. Working with Constants
      1. Introducing the Game Stats 3.0 Program
      2. Using Constants
      3. Using Enumerations
    8. Introducing Lost Fortune
      1. Setting Up the Program
      2. Getting Information from the Player
      3. Telling the Story
    9. Summary
    10. Questions and Answers
    11. Discussion Questions
    12. Exercises
  6. 2. Truth, Branching, and the Game Loop: Guess My Number
    1. Understanding Truth
    2. Using the if Statement
      1. Introducing the Score Rater Program
      2. Testing true and false
      3. Interpreting a Value as true or false
      4. Using Relational Operators
      5. Nesting if Statements
    3. Using the else Clause
      1. Introducing the Score Rater 2.0 Program
      2. Creating Two Ways to Branch
    4. Using a Sequence of if Statements with else Clauses
      1. Introducing the Score Rater 3.0 Program
      2. Creating a Sequence of if Statements with else Clauses
    5. Using the switch Statement
      1. Introducing the Menu Chooser Program
      2. Creating Multiple Ways to Branch
    6. Using while Loops
      1. Introducing the Play Again Program
      2. Looping with a while Loop
    7. Using do Loops
      1. Introducing the Play Again 2.0 Program
      2. Looping with a do Loop
    8. Using break and continue Statements
      1. Introducing the Finicky Counter Program
      2. Creating a while (true) Loop
      3. Using the break Statement to Exit a Loop
      4. Using the continue Statement to Jump Back to the Top of a Loop
      5. Understanding When to Use break and continue
    9. Using Logical Operators
      1. Introducing the Designers Network Program
      2. Using the Logical AND Operator
      3. Using the Logical OR Operator
      4. Using the Logical NOT Operator
      5. Understanding Order of Operations
    10. Generating Random Numbers
      1. Introducing the Die Roller Program
      2. Calling the rand() Function
      3. Seeding the Random Number Generator
      4. Calculating a Number within a Range
    11. Understanding the Game Loop
    12. Introducing Guess My Number
      1. Applying the Game Loop
      2. Setting Up the Game
      3. Creating the Game Loop
      4. Wrapping Up the Game
    13. Summary
    14. Questions and Answers
    15. Discussion Questions
    16. Exercises
  7. 3. For Loops, Strings, and Arrays: Word Jumble
    1. Using for Loops
      1. Introducing the Counter Program
      2. Counting with for Loops
      3. Using Empty Statements in for Loops
      4. Nesting for Loops
    2. Understanding Objects
    3. Using String Objects
      1. Introducing the String Tester Program
      2. Creating string Objects
      3. Concatenating string Objects
      4. Using the size() Member Function
      5. Indexing a string Object
      6. Iterating through string Objects
      7. Using the find() Member Function
      8. Using the erase() Member Function
      9. Using the empty() Member Function
    4. Using Arrays
      1. Introducing the Hero’s Inventory Program
      2. Creating Arrays
      3. Indexing Arrays
      4. Accessing Member Functions of an Array Element
      5. Being Aware of Array Bounds
    5. Understanding C-Style Strings
    6. Using Multidimensional Arrays
      1. Introducing the Tic-Tac-Toe Board Program
      2. Creating Multidimensional Arrays
      3. Indexing Multidimensional Arrays
    7. Introducing Word Jumble
      1. Setting Up the Program
      2. Picking a Word to Jumble
      3. Jumbling the Word
      4. Welcoming the Player
      5. Entering the Game Loop
      6. Saying Goodbye
    8. Summary
    9. Questions and Answers
    10. Discussion Questions
    11. Exercises
  8. 4. The Standard Template Library: Hangman
    1. Introducing the Standard Template Library
    2. Using Vectors
      1. Introducing the Hero’s Inventory 2.0 Program
      2. Preparing to Use Vectors
      3. Declaring a Vector
      4. Using the push_back() Member Function
      5. Using the size() Member Function
      6. Indexing Vectors
      7. Calling Member Functions of an Element
      8. Using the pop_back() Member Function
      9. Using the clear() Member Function
      10. Using the empty() Member Function
    3. Using Iterators
      1. Introducing the Hero’s Inventory 3.0 Program
      2. Declaring Iterators
      3. Looping through a Vector
        1. Calling the begin() Vector Member Function
        2. Calling the end() Vector Member Function
        3. Altering an Iterator
        4. Dereferencing an Iterator
      4. Changing the Value of a Vector Element
      5. Accessing Member Functions of a Vector Element
      6. Using the insert() Vector Member Function
      7. Using the erase() Vector Member Function
    4. Using Algorithms
      1. Introducing the High Scores Program
      2. Preparing to Use Algorithms
      3. Using the find() Algorithm
      4. Using the random_shuffle() Algorithm
      5. Using the sort() Algorithm
    5. Understanding Vector Performance
      1. Examining Vector Growth
        1. Using the capacity() Member Function
        2. Using the reserve() Member Function
      2. Examining Element Insertion and Deletion
    6. Examining Other STL Containers
    7. Planning Your Programs
      1. Using Pseudocode
      2. Using Stepwise Refinement
    8. Introducing Hangman
      1. Planning the Game
      2. Setting Up the Program
      3. Initializing Variables and Constants
      4. Entering the Main Loop
      5. Getting the Player’s Guess
      6. Ending the Game
    9. Summary
    10. Questions and Answers
    11. Discussion Questions
    12. Exercises
  9. 5. Functions: Mad Lib
    1. Creating Functions
      1. Introducing the Instructions Program
      2. Declaring Functions
      3. Defining Functions
      4. Calling Functions
      5. Understanding Abstraction
    2. Using Parameters and Return Values
      1. Introducing the Yes or No Program
      2. Returning a Value
        1. Specifying a Return Type
        2. Using the return Statement
        3. Using a Returned Value
      3. Accepting Values into Parameters
        1. Specifying Parameters
        2. Passing Values to Parameters
        3. Using Parameter Values
      4. Understanding Encapsulation
    3. Understanding Software Reuse
    4. Working with Scopes
      1. Introducing the Scoping Program
      2. Working with Separate Scopes
      3. Working with Nested Scopes
    5. Using Global Variables
      1. Introducing the Global Reach Program
      2. Declaring Global Variables
      3. Accessing Global Variables
      4. Hiding Global Variables
      5. Altering Global Variables
      6. Minimizing the Use of Global Variables
    6. Using Global Constants
    7. Using Default Arguments
      1. Introducing the Give Me a Number Program
      2. Specifying Default Arguments
      3. Assigning Default Arguments to Parameters
      4. Overriding Default Arguments
    8. Overloading Functions
      1. Introducing the Triple Program
      2. Creating Overloaded Functions
      3. Calling Overloaded Functions
    9. Inlining Functions
      1. Introducing the Taking Damage Program
      2. Specifying Functions for Inlining
      3. Calling Inlined Functions
    10. Introducing the Mad Lib Game
      1. Setting Up the Program
      2. The main() Function
      3. The askText() Function
      4. The askNumber() Function
      5. The tellStory() Function
    11. Summary
    12. Questions and Answers
    13. Discussion Questions
    14. Exercises
  10. 6. References: Tic-Tac-Toe
    1. Using References
      1. Introducing the Referencing Program
      2. Creating References
      3. Accessing Referenced Values
      4. Altering Referenced Values
    2. Passing References to Alter Arguments
      1. Introducing the Swap Program
      2. Passing by Value
      3. Passing by Reference
    3. Passing References for Efficiency
      1. Introducing the Inventory Displayer Program
      2. Understanding the Pitfalls of Reference Passing
      3. Declaring Parameters as Constant References
      4. Passing a Constant Reference
    4. Deciding How to Pass Arguments
    5. Returning References
      1. Introducing the Inventory Referencer Program
      2. Returning a Reference
      3. Displaying the Value of a Returned Reference
      4. Assigning a Returned Reference to a Reference
      5. Assigning a Returned Reference to a Variable
      6. Altering an Object through a Returned Reference
    6. Introducing the Tic-Tac-Toe Game
      1. Planning the Game
        1. Writing the Pseudocode
        2. Representing the Data
        3. Creating a List of Functions
      2. Setting Up the Program
      3. The main() Function
      4. The instructions() Function
      5. The askYesNo() Function
      6. The askNumber() Function
      7. The humanPiece() Function
      8. The opponent() Function
      9. The displayBoard() Function
      10. The winner() Function
      11. The isLegal() Function
      12. The humanMove() Function
      13. The computerMove() Function
      14. The announceWinner() Function
    7. Summary
    8. Questions and Answers
    9. Discussion Questions
    10. Exercises
  11. 7. Pointers: Tic-Tac-Toe 2.0
    1. Understanding Pointer Basics
      1. Introducing the Pointing Program
      2. Declaring Pointers
      3. Initializing Pointers
      4. Assigning Addresses to Pointers
      5. Dereferencing Pointers
      6. Reassigning Pointers
      7. Using Pointers to Objects
    2. Understanding Pointers and Constants
      1. Using a Constant Pointer
      2. Using a Pointer to a Constant
      3. Using a Constant Pointer to a Constant
      4. Summarizing Constants and Pointers
    3. Passing Pointers
      1. Introducing the Swap Pointer Version Program
      2. Passing by Value
      3. Passing a Constant Pointer
    4. Returning Pointers
      1. Introducing the Inventory Pointer Program
      2. Returning a Pointer
      3. Using a Returned Pointer to Display a Value
      4. Assigning a Returned Pointer to a Pointer
      5. Assigning to a Variable the Value Pointed to by a Returned Pointer
      6. Altering an Object through a Returned Pointer
    5. Understanding the Relationship between Pointers and Arrays
      1. Introducing the Array Passer Program
      2. Using an Array Name as a Constant Pointer
      3. Passing and Returning Arrays
    6. Introducing the Tic-Tac-Toe 2.0 Game
    7. Summary
    8. Questions and Answers
    9. Discussion Questions
    10. Exercises
  12. 8. Classes: Critter Caretaker
    1. Defining New Types
      1. Introducing the Simple Critter Program
      2. Defining a Class
        1. Declaring Data Members
        2. Declaring Member Functions
      3. Defining Member Functions
      4. Instantiating Objects
      5. Accessing Data Members
      6. Calling Member Functions
    2. Using Constructors
      1. Introducing the Constructor Critter Program
      2. Declaring and Defining a Constructor
      3. Calling a Constructor Automatically
    3. Setting Member Access Levels
      1. Introducing the Private Critter Program
      2. Specifying Public and Private Access Levels
      3. Defining Accessor Member Functions
      4. Defining Constant Member Functions
    4. Using Static Data Members and Member Functions
      1. Introducing the Static Critter Program
      2. Declaring and Initializing Static Data Members
      3. Accessing Static Data Members
      4. Declaring and Defining Static Member Functions
      5. Calling Static Member Functions
    5. Introducing the Critter Caretaker Game
      1. Planning the Game
      2. Planning the Pseudocode
      3. The Critter Class
        1. The Class Definition
        2. The Class Constructor
        3. The GetMood() Member Function
        4. The PassTime() Member Function
        5. The Talk() Member Function
        6. The Eat() Member Function
        7. The Play() Member Function
      4. The main() Function
    6. Summary
    7. Questions and Answers
    8. Discussion Questions
    9. Exercises
  13. 9. Advanced Classes and Dynamic Memory: Game Lobby
    1. Using Aggregation
      1. Introducing the Critter Farm Program
      2. Using Object Data Members
      3. Using Container Data Members
    2. Using Friend Functions and Operator Overloading
      1. Introducing the Friend Critter Program
      2. Creating Friend Functions
      3. Overloading Operators
    3. Dynamically Allocating Memory
      1. Introducing the Heap Program
      2. Using the new Operator
      3. Using the delete Operator
      4. Avoiding Memory Leaks
    4. Working with Data Members and the Heap
      1. Introducing the Heap Data Member Program
      2. Declaring Data Members that Point to Values on the Heap
      3. Declaring and Defining Destructors
      4. Declaring and Defining Copy Constructors
      5. Overloading the Assignment Operator
    5. Introducing the Game Lobby Program
      1. The Player Class
      2. The Lobby Class
      3. The Lobby::AddPlayer() Member Function
      4. The Lobby::RemovePlayer() Member Function
      5. The Lobby::Clear() Member Function
      6. The operator<<() Member Function
      7. The main() Function
    6. Summary
    7. Questions and Answers
    8. Discussion Questions
    9. Exercises
  14. 10. Inheritance and Polymorphism: Blackjack
    1. Introducing Inheritance
      1. Introducing the Simple Boss Program
      2. Deriving from a Base Class
      3. Instantiating Objects from a Derived Class
      4. Using Inherited Members
    2. Controlling Access under Inheritance
      1. Introducing the Simple Boss 2.0 Program
      2. Using Access Modifiers with Class Members
      3. Using Access Modifiers When Deriving Classes
    3. Calling and Overriding Base Class Member Functions
      1. Introducing the Overriding Boss Program
      2. Calling Base Class Constructors
      3. Declaring Virtual Base Class Member Functions
      4. Overriding Virtual Base Class Member Functions
      5. Calling Base Class Member Functions
    4. Using Overloaded Assignment Operators and Copy Constructors in Derived Classes
    5. Introducing Polymorphism
      1. Introducing the Polymorphic Bad Guy Program
      2. Using Base Class Pointers to Derived Class Objects
      3. Defining Virtual Destructors
    6. Using Abstract Classes
      1. Introducing the Abstract Creature Program
      2. Declaring Pure Virtual Functions
      3. Deriving a Class from an Abstract Class
    7. Introducing the Blackjack Game
      1. Designing the Classes
      2. Planning the Game Logic
      3. The Card Class
      4. The Hand Class
      5. The GenericPlayer Class
      6. The Player Class
      7. The House Class
      8. The Deck Class
      9. The Game Class
      10. The main() Function
      11. Overloading the operator<<() Function
    8. Summary
    9. Questions and Answers
    10. Discussion Questions
    11. Exercises
  15. A. Creating Your First C++ Program
  16. B. Operator Precedence
  17. C. Keywords
  18. D. ASCII Chart
  19. E. Escape Sequences