< Previous | Contents | Next >

Contents


Introduction xviii


Chapter

1

Types, Variables, and Standard I/O: Lost Fortune . . . . . . . .

1



Introducing Cþþ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1



Using Cþþ for Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2



Creating an Executable File . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2



Dealing with Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

4



Understanding the ISO Standard . . . . . . . . . . . . . . . . . . . . . . . . .

5



Writing Your First Cþþ Program . . . . . . . . . . . . . . . . . . . . . . . . . . .

5



Introducing the Game Over Program . . . . . . . . . . . . . . . . . . . . . .

5



Commenting Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7



Using Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7



Including Other Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7



Defining the main() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . .

8



Displaying Text through the Standard Output . . . . . . . . . . . . . . .

8



Terminating Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

9



Returning a Value from main() . . . . . . . . . . . . . . . . . . . . . . . . .

10



Working with the std Namespace . . . . . . . . . . . . . . . . . . . . . . . . . .

10



Introducing the Game Over 2.0 Program . . . . . . . . . . . . . . . . . .

10



Employing a using Directive . . . . . . . . . . . . . . . . . . . . . . . . . . .

11



Introducing the Game Over 3.0 Program . . . . . . . . . . . . . . . . . .

11



Employing using Declarations . . . . . . . . . . . . . . . . . . . . . . . . . .

12



Understanding When to Employ using . . . . . . . . . . . . . . . . . . . .

12


vi

Contents vii

Using Arithmetic Operators 13

Introducing the Expensive Calculator Program 13

Adding, Subtracting, and Multiplying 14

Understanding Integer and Floating Point Division 14

Using the Modulus Operator 15

Understanding Order of Operations 15

Declaring and Initializing Variables 16

Introducing the Game Stats Program 16

Understanding Fundamental Types 18

Understanding Type Modifiers 18

Declaring Variables 19

Naming Variables 20

Assigning Values to Variables 21

Initializing Variables 22

Displaying Variable Values 22

Getting User Input 23

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

Working with Constants 29

Introducing the Game Stats 3.0 Program 29

Using Constants 31

Using Enumerations 31

Introducing Lost Fortune 32

Setting Up the Program 32

Getting Information from the Player 33

Telling the Story 34

Summary 35

Questions and Answers 36

Discussion Questions 38

Exercises 38

Chapter 2 Truth, Branching, and the Game Loop:

Guess My Number 39

Understanding Truth 39

viii Contents

Using the if Statement 40

Introducing the Score Rater Program 41

Testing true and false 42

Interpreting a Value as true or false 43

Using Relational Operators 44

Nesting if Statements 44

Using the else Clause 45

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

Using the switch Statement 51

Introducing the Menu Chooser Program 52

Creating Multiple Ways to Branch 54

Using while Loops 54

Introducing the Play Again Program 54

Looping with a while Loop 55

Using do Loops 56

Introducing the Play Again 2.0 Program 56

Looping with a do Loop 57

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

to the Top of a Loop 61

Understanding When to Use break and continue 61

Using Logical Operators 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

Generating Random Numbers 68

Introducing the Die Roller Program 68

Calling the rand() Function 69

Seeding the Random Number Generator 70

Calculating a Number within a Range 71

Contents ix

Understanding the Game Loop 72

Introducing Guess My Number 73

Applying the Game Loop 74

Setting Up the Game 74

Creating the Game Loop 76

Wrapping Up the Game 76

Summary 76

Questions and Answers 78

Discussion Questions 80

Exercises 80

Chapter 3 For Loops, Strings, and Arrays: Word Jumble 81

Using for Loops 81

Introducing the Counter Program 82

Counting with for Loops 84

Using Empty Statements in for Loops 85

Nesting for Loops 86

Understanding Objects 87

Using String Objects 89

Introducing the String Tester Program 89

Creating string Objects 91

Concatenating string Objects 92

Using the size() Member Function 92

Indexing a string Object 93

Iterating through string Objects 93

Using the find() Member Function 94

Using the erase() Member Function 95

Using the empty() Member Function 96

Using Arrays 96

Introducing the Hero’s Inventory Program 96

Creating Arrays 98

Indexing Arrays 99

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

x Contents

Introducing Word Jumble 106

Setting Up the Program 107

Picking a Word to Jumble 107

Jumbling the Word 108

Welcoming the Player 109

Entering the Game Loop 109

Saying Goodbye 110

Summary 110

Questions and Answers 111

Discussion Questions 113

Exercises 114

Chapter 4 The Standard Template Library: Hangman 115

Introducing the Standard Template Library 115

Using Vectors 116

Introducing the Hero’s Inventory 2.0 Program 117

Preparing to Use Vectors 119

Declaring a Vector 119

Using the push_back() Member Function 120

Using the size() Member Function 120

Indexing Vectors 121

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

Using Iterators 123

Introducing the Hero’s Inventory 3.0 Program 123

Declaring Iterators 125

Looping through a Vector 126

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

Using Algorithms 131

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

Contents xi

Understanding Vector Performance 136

Examining Vector Growth 136

Examining Element Insertion and Deletion 138

Examining Other STL Containers 138

Planning Your Programs 139

Using Pseudocode 139

Using Stepwise Refinement 140

Introducing Hangman 141

Planning the Game 141

Setting Up the Program 142

Initializing Variables and Constants 143

Entering the Main Loop 143

Getting the Player’s Guess 144

Ending the Game 145

Summary 145

Questions and Answers 146

Discussion Questions 148

Exercises 148

Chapter 5 Functions: Mad Lib 151

Creating Functions 151

Introducing the Instructions Program 152

Declaring Functions 153

Defining Functions 154

Calling Functions 154

Understanding Abstraction 155

Using Parameters and Return Values 155

Introducing the Yes or No Program 155

Returning a Value 157

Accepting Values into Parameters 158

Understanding Encapsulation 160

Understanding Software Reuse 161

Working with Scopes 161

Introducing the Scoping Program 161

Working with Separate Scopes 163

Working with Nested Scopes 165

Using Global Variables 166

Introducing the Global Reach Program 166

Declaring Global Variables 168

Accessing Global Variables 168

xii Contents

Hiding Global Variables 169

Altering Global Variables 169

Minimizing the Use of Global Variables 170

Using Global Constants 170

Using Default Arguments 171

Introducing the Give Me a Number Program 171

Specifying Default Arguments 173

Assigning Default Arguments to Parameters 173

Overriding Default Arguments 174

Overloading Functions 174

Introducing the Triple Program 174

Creating Overloaded Functions 176

Calling Overloaded Functions 177

Inlining Functions 177

Introducing the Taking Damage Program 177

Specifying Functions for Inlining 179

Calling Inlined Functions 179

Introducing the Mad Lib Game 180

Setting Up the Program 181

The main() Function 181

The askText() Function 182

The askNumber() Function 182

The tellStory() Function 183

Summary 183

Questions and Answers 184

Discussion Questions 186

Exercises 186

Chapter 6 References: Tic-Tac-Toe 187

Using References 187

Introducing the Referencing Program 187

Creating References 189

Accessing Referenced Values 190

Altering Referenced Values 190

Passing References to Alter Arguments 191

Introducing the Swap Program 191

Passing by Value 193

Passing by Reference 194

Passing References for Efficiency 195

Introducing the Inventory Displayer Program 195

Contents xiii

Understanding the Pitfalls of Reference Passing 196

Declaring Parameters as Constant References 197

Passing a Constant Reference 197

Deciding How to Pass Arguments 198

Returning References 198

Introducing the Inventory Referencer Program 199

Returning a Reference 200

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

Planning the Game 203

Setting Up the Program 205

The main() Function 207

The instructions() Function 208

The askYesNo() Function 208

The askNumber() Function 209

The humanPiece() Function 209

The opponent() Function 210

The displayBoard() Function 210

The winner() Function 211

The isLegal() Function 212

The humanMove() Function 213

The computerMove() Function 213

The announceWinner() Function 217

Summary 217

Questions and Answers 218

Discussion Questions 220

Exercises 221

Chapter 7 Pointers: Tic-Tac-Toe 2.0 223

Understanding Pointer Basics 223

Introducing the Pointing Program 224

Declaring Pointers 226

Initializing Pointers 227

Assigning Addresses to Pointers 227

Dereferencing Pointers 228

Reassigning Pointers 229

Using Pointers to Objects 230

xiv Contents

Understanding Pointers and Constants 231

Using a Constant Pointer 231

Using a Pointer to a Constant 232

Using a Constant Pointer to a Constant 233

Summarizing Constants and Pointers 234

Passing Pointers 234

Introducing the Swap Pointer Version Program 234

Passing by Value 236

Passing a Constant Pointer 237

Returning Pointers 238

Introducing the Inventory Pointer Program 239

Returning a Pointer 240

Using a Returned Pointer to Display a Value 241

Assigning a Returned Pointer to a Pointer 242

Assigning to a Variable the Value Pointed

to by a Returned Pointer 242

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

Summary 248

Questions and Answers 250

Discussion Questions 252

Exercises 252

Chapter 8 Classes: Critter Caretaker 255

Defining New Types 255

Introducing the Simple Critter Program 256

Defining a Class 257

Defining Member Functions 258

Instantiating Objects 259

Accessing Data Members 259

Calling Member Functions 260

Using Constructors 260

Introducing the Constructor Critter Program 261

Declaring and Defining a Constructor 262

Calling a Constructor Automatically 263

Contents xv

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

Planning the Game 275

Planning the Pseudocode 276

The Critter Class 277

The main() Function 280

Summary 281

Questions and Answers 283

Discussion Questions 285

Exercises 285


Chapter

9

Advanced Classes and Dynamic Memory:




Game Lobby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

287



Using Aggregation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

287



Introducing the Critter Farm Program . . . . . . . . . . . . . . . . . . .

288



Using Object Data Members . . . . . . . . . . . . . . . . . . . . . . . . . .

290



Using Container Data Members . . . . . . . . . . . . . . . . . . . . . . . .

291



Using Friend Functions and Operator Overloading . . . . . . . . . . . .

292



Introducing the Friend Critter Program . . . . . . . . . . . . . . . . . .

292



Creating Friend Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . .

295



Overloading Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

295



Dynamically Allocating Memory . . . . . . . . . . . . . . . . . . . . . . . . . .

296



Introducing the Heap Program . . . . . . . . . . . . . . . . . . . . . . . .

297



Using the new Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

299



Using the delete Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . .

300



Avoiding Memory Leaks . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

301



Working with Data Members and the Heap . . . . . . . . . . . . . . . . .

303



Introducing the Heap Data Member Program . . . . . . . . . . . . .

Declaring Data Members that Point to

Values on the Heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

303


307

xvi Contents


Declaring and Defining Destructors 308

Declaring and Defining Copy Constructors 309

Overloading the Assignment Operator 313

Introducing the Game Lobby Program 315

The Player Class 316

The Lobby Class 318

The Lobby::AddPlayer() Member Function 320

The Lobby::RemovePlayer() Member Function 322

The Lobby::Clear() Member Function 322

The operator<<() Member Function 323

The main() Function 324

Summary 325

Questions and Answers 326

Discussion Questions 328

Exercises 328

Chapter 10 Inheritance and Polymorphism: Blackjack 331

Introducing Inheritance 331

Introducing the Simple Boss Program 333

Deriving from a Base Class 335

Instantiating Objects from a Derived Class 336

Using Inherited Members 337

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 Polymorphism 347

Introducing the Polymorphic Bad Guy Program 347

Using Base Class Pointers to Derived Class Objects 350

Defining Virtual Destructors 351

Using Abstract Classes 352

Introducing the Abstract Creature Program 352

Contents xvii

Declaring Pure Virtual Functions 354

Deriving a Class from an Abstract Class 355

Introducing the Blackjack Game 356

Designing the Classes 356

Planning the Game Logic 360

The Card Class 361

The Hand Class 363

The GenericPlayer Class 366

The Player Class 368

The House Class 369

The Deck Class 370

The Game Class 373

The main() Function 376

Overloading the operator<<() Function 377

Summary 379

Questions and Answers 380

Discussion Questions 382

Exercises 382

Appendix A Creating Your First C++ Program 383

Appendix B Operator Precedence 389

Appendix C Keywords 391

Appendix D ASCII Chart 393

Appendix E Escape Sequences 397

Index 399


image