< Previous | Contents | Next >
vi
Introducing the Expensive Calculator Program 13
Adding, Subtracting, and Multiplying 14
Understanding Integer and Floating Point Division 14
Understanding Order of Operations 15
Declaring and Initializing Variables 16
Introducing the Game Stats Program 16
Understanding Fundamental Types 18
Understanding Type Modifiers 18
Assigning Values to Variables 21
Defining New Names for Types 23
Understanding Which Types to Use 24
Performing Arithmetic Operations with Variables 24
Introducing the Game Stats 2.0 Program 24
Altering the Value of a Variable 26
Using Combined Assignment Operators 26
Using Increment and Decrement Operators 27
Dealing with Integer Wrap Around 28
Introducing the Game Stats 3.0 Program 29
Getting Information from the Player 33
Chapter 2 Truth, Branching, and the Game Loop:
Introducing the Score Rater Program 41
Interpreting a Value as true or false 43
Introducing the Score Rater 2.0 Program 46
Creating Two Ways to Branch 47
Using a Sequence of if Statements with else Clauses 48
Introducing the Score Rater 3.0 Program 49
Creating a Sequence of if Statements with else Clauses 50
Introducing the Menu Chooser Program 52
Creating Multiple Ways to Branch 54
Introducing the Play Again Program 54
Introducing the Play Again 2.0 Program 56
Using break and continue Statements 58
Introducing the Finicky Counter Program 58
Creating a while (true) Loop 60
Using the break Statement to Exit a Loop 60
Using the continue Statement to Jump Back
Understanding When to Use break and continue 61
Introducing the Designers Network Program 62
Using the Logical AND Operator 65
Using the Logical OR Operator 66
Using the Logical NOT Operator 66
Understanding Order of Operations 67
Introducing the Die Roller Program 68
Calling the rand() Function 69
Seeding the Random Number Generator 70
Calculating a Number within a Range 71
Understanding the Game Loop 72
Introducing Guess My Number 73
Chapter 3 For Loops, Strings, and Arrays: Word Jumble 81
Introducing the Counter Program 82
Using Empty Statements in for Loops 85
Introducing the String Tester Program 89
Concatenating string Objects 92
Using the size() Member Function 92
Iterating through string Objects 93
Using the find() Member Function 94
Using the erase() Member Function 95
Using the empty() Member Function 96
Introducing the Hero’s Inventory Program 96
Accessing Member Functions of an Array Element 100
Being Aware of Array Bounds 100
Understanding C-Style Strings 101
Using Multidimensional Arrays 103
Introducing the Tic-Tac-Toe Board Program 103
Creating Multidimensional Arrays 105
Indexing Multidimensional Arrays 105
Chapter 4 The Standard Template Library: Hangman 115
Introducing the Standard Template Library 115
Introducing the Hero’s Inventory 2.0 Program 117
Using the push_back() Member Function 120
Using the size() Member Function 120
Calling Member Functions of an Element 121
Using the pop_back() Member Function 122
Using the clear() Member Function 122
Using the empty() Member Function 122
Introducing the Hero’s Inventory 3.0 Program 123
Changing the Value of a Vector Element 128
Accessing Member Functions of a Vector Element 129
Using the insert() Vector Member Function 130
Using the erase() Vector Member Function 130
Introducing the High Scores Program 131
Preparing to Use Algorithms 133
Using the find() Algorithm 134
Using the random_shuffle() Algorithm 134
Using the sort() Algorithm 135
Understanding Vector Performance 136
Examining Element Insertion and Deletion 138
Examining Other STL Containers 138
Initializing Variables and Constants 143
Getting the Player’s Guess 144
Chapter 5 Functions: Mad Lib 151
Introducing the Instructions Program 152
Using Parameters and Return Values 155
Introducing the Yes or No Program 155
Accepting Values into Parameters 158
Understanding Encapsulation 160
Understanding Software Reuse 161
Introducing the Scoping Program 161
Working with Separate Scopes 163
Working with Nested Scopes 165
Introducing the Global Reach Program 166
Declaring Global Variables 168
Accessing Global Variables 168
Minimizing the Use of Global Variables 170
Introducing the Give Me a Number Program 171
Specifying Default Arguments 173
Assigning Default Arguments to Parameters 173
Overriding Default Arguments 174
Introducing the Triple Program 174
Creating Overloaded Functions 176
Calling Overloaded Functions 177
Introducing the Taking Damage Program 177
Specifying Functions for Inlining 179
Introducing the Mad Lib Game 180
Chapter 6 References: Tic-Tac-Toe 187
Introducing the Referencing Program 187
Accessing Referenced Values 190
Altering Referenced Values 190
Passing References to Alter Arguments 191
Introducing the Swap Program 191
Passing References for Efficiency 195
Introducing the Inventory Displayer Program 195
Understanding the Pitfalls of Reference Passing 196
Declaring Parameters as Constant References 197
Passing a Constant Reference 197
Deciding How to Pass Arguments 198
Introducing the Inventory Referencer Program 199
Displaying the Value of a Returned Reference 201
Assigning a Returned Reference to a Reference 202
Assigning a Returned Reference to a Variable 202
Altering an Object through a Returned Reference 202
Introducing the Tic-Tac-Toe Game 203
The instructions() Function 208
The displayBoard() Function 210
The computerMove() Function 213
The announceWinner() Function 217
Chapter 7 Pointers: Tic-Tac-Toe 2.0 223
Understanding Pointer Basics 223
Introducing the Pointing Program 224
Assigning Addresses to Pointers 227
Understanding Pointers and Constants 231
Using a Pointer to a Constant 232
Using a Constant Pointer to a Constant 233
Summarizing Constants and Pointers 234
Introducing the Swap Pointer Version Program 234
Passing a Constant Pointer 237
Introducing the Inventory Pointer Program 239
Using a Returned Pointer to Display a Value 241
Assigning a Returned Pointer to a Pointer 242
Assigning to a Variable the Value Pointed
Altering an Object through a Returned Pointer 243
Understanding the Relationship between Pointers and Arrays 244
Introducing the Array Passer Program 244
Using an Array Name as a Constant Pointer 246
Passing and Returning Arrays 247
Introducing the Tic-Tac-Toe 2.0 Game 248
Chapter 8 Classes: Critter Caretaker 255
Introducing the Simple Critter Program 256
Introducing the Constructor Critter Program 261
Declaring and Defining a Constructor 262
Calling a Constructor Automatically 263
Setting Member Access Levels 264
Introducing the Private Critter Program 264
Specifying Public and Private Access Levels 266
Defining Accessor Member Functions 267
Defining Constant Member Functions 268
Using Static Data Members and Member Functions 269
Introducing the Static Critter Program 270
Declaring and Initializing Static Data Members 272
Accessing Static Data Members 272
Declaring and Defining Static Member Functions 273
Calling Static Member Functions 273
Introducing the Critter Caretaker Game 274
Declaring and Defining Destructors 308
Declaring and Defining Copy Constructors 309
Overloading the Assignment Operator 313
Introducing the Game Lobby Program 315
The Lobby::AddPlayer() Member Function 320
The Lobby::RemovePlayer() Member Function 322
The Lobby::Clear() Member Function 322
The operator<<() Member Function 323
Chapter 10 Inheritance and Polymorphism: Blackjack 331
Introducing the Simple Boss Program 333
Deriving from a Base Class 335
Instantiating Objects from a Derived Class 336
Controlling Access under Inheritance 337
Introducing the Simple Boss 2.0 Program 338
Using Access Modifiers with Class Members 339
Using Access Modifiers when Deriving Classes 340
Calling and Overriding Base Class Member Functions 340
Introducing the Overriding Boss Program 341
Calling Base Class Constructors 343
Declaring Virtual Base Class Member Functions 344
Overriding Virtual Base Class Member Functions 344
Calling Base Class Member Functions 345
Using Overloaded Assignment Operators and
Copy Constructors in Derived Classes 346
Introducing the Polymorphic Bad Guy Program 347
Using Base Class Pointers to Derived Class Objects 350
Defining Virtual Destructors 351
Introducing the Abstract Creature Program 352
Declaring Pure Virtual Functions 354
Deriving a Class from an Abstract Class 355
Introducing the Blackjack Game 356
Overloading the operator<<() Function 377
Appendix A Creating Your First C++ Program 383
Appendix B Operator Precedence 389
Appendix E Escape Sequences 397