Beginning C++ Through Game Programming 3rd Edition Book

Free Download C++ Programming Books |
Click Here To Download Book
Pages 433
Views 17294
Size 5.7 MB
Download 131

Table Of Contents 7

Introduction 19

Chapter 1 Types, Variables, and Standard I/O: Lost Fortune 24

Introducing C++ 24

Using Cþþfor Games25

Creating an Executable File25

Dealing with Errors27

Understanding the ISO Standard28

Writing Your First CþþProgram28

Introducing the Game Over Progra28

Commenting Code .30

Using Whitespace 30

Including Other Files 30

Defining the main() Function 31

Displaying Text through the Standard Output 31

Terminating Statements32

Returning a Value from main() 33

Working with the std Namespace 33

Introducing the Game Over 2.0 Program33

Employing a using Directive 34

Introducing the Game Over 3.0 Program34

Employing using Declarations35

Understanding When to Employ using35

Using Arithmetic Operators36

Introducing the Expensive Calculator Program36

Adding, Subtracting, and Multiplying37

Understanding Integer and Floating Point Division37

Using the Modulus Operator 38

Understanding Order of Operations 38

Declaring and Initializing Variables 39

Introducing the Game Stats Program 39

Understanding Fundamental Types41

Understanding Type Modifiers41

Declaring Variables42

Naming Variables 43

Assigning Values to Variables44

Initializing Variables 45

Displaying Variable Values45

Getting User Input46

Defining New Names for Types 46

Understanding Which Types to Use47

Performing Arithmetic Operations with Variables47

Introducing the Game Stats 2.0 Program47

Altering the Value of a Variable49

Using Combined Assignment Operators 49

Using Increment and Decrement Operators50

Dealing with Integer Wrap Around51

Working with Constants 52

Introducing the Game Stats 3.0 Program52

Using Constants54

Using Enumerations 54

Introducing Lost Fortune55

Setting Up the Program55

Getting Information from the Player 56

Questions and Answers 59

Discussion Questions 61


Chapter 2 Truth, Branching, and the Game Lo62

Understanding Truth 62

Using the if Statement63

Introducing the Score Rater Program 64

Testing true and false65

Interpreting a Value as true or false 66

Using Relational Operators67

Nesting if Statements67

Using the else Clause 68

Introducing the Score Rater 2.0 Program69

Creating Two Ways to Branch70

Using a Sequence of if Statements with else Clauses71

Introducing the Score Rater 3.0 Program72

Creating a Sequence of if Statements with else Clauses 73

Using the switch Statement74

Introducing the Menu Chooser Program75

Creating Multiple Ways to Branch 77

Using while Loops 77

Introducing the Play Again Program 77

Looping with a while Loop78

Using do Loops 79

Introducing the Play Again 2.0 Program79

Looping with a do Loop80

Using break and continue Statements81

Introducing the Finicky Counter Progra81

Creating a while (true) Loop 83

Using the break Statement to Exit a Loop 83

to the Top of a Loop84

Understanding When to Use break and continue84

Using Logical Operators 84

Introducing the Designers Network Program 85

Using the Logical AND Operator88

Using the Logical OR Operator 89

Using the Logical NOT Operator89

Understanding Order of Operations 90

Generating Random Numbers91

Introducing the Die Roller Program91

Calling the rand() Function92

Seeding the Random Number Generator93

Calculating a Number within a Range94

Understanding the Game Loop 95

Introducing Guess My Number96

Applying the Game Loop 97

Setting Up the Game97

Creating the Game Loop99

Wrapping Up the Game99

Questions and Answers 101

Discussion Questions 103

Chapter 3 For Loops, Strings, and Arrays: Word Jumble104

Using for Loops 104

Introducing the Counter Program 105

Counting with for Loops107

Using Empty Statements in for Loops108

Nesting for Loops 109

Understanding Objects110

Using String Objects112

Introducing the String Tester Program112

Creating string Objects 114

Concatenating string Objects 115

Using the size() Member Function 115

Indexing a string Object116

Iterating through string Objects116

Using the find() Member Function 117

Using the erase() Member Function118

Using the empty() Member Function 119

Introducing the Hero’s Inventory Program 119

Accessing Member Functions of an Array Element123

Being Aware of Array Bounds 123

Understanding C-Style Strings 124

Using Multidimensional Arrays 126

Introducing the Tic-Tac-Toe Board Program 126

Creating Multidimensional Arrays128

Indexing Multidimensional Arrays128

Introducing Word Jumble 129

Setting Up the Program 130

Picking a Word to Jumble130

Jumbling the Word 131

Welcoming the Player 132

Entering the Game Loop 132

Saying Goodbye 133

Questions and Answers134

Discussion Questions136

Chapter 4 The Standard Template Library: Hangman.138

Introducing the Standard Template Library 138

Using Vectors139

Introducing the Hero’s Inventory 2.0 Program 140

Preparing to Use Vectors 142

Declaring a Vector 142

Using the push_back() Member Function 143

Using the size() Member Function143

Indexing Vectors 144

Calling Member Functions of an Element144

Using the pop_back() Member Function 145

Using the clear() Member Function 145

Using the empty() Member Function145

Using Iterators 146

Introducing the Hero’s Inventory 3.0 Program 146

Declaring Iterators 148

Looping through a Vector149

Changing the Value of a Vector Element 151

Accessing Member Functions of a Vector Element152

Using the insert() Vector Member Function 153

Using the erase() Vector Member Function 153

Using Algorithms 154

Introducing the High Scores Program 154

Preparing to Use Algorithms156

Using the find() Algorithm 157

Using the random_shuffle() Algorithm 157

Using the sort() Algorithm158

UnderstandingVectorPerformance 159


ExaminingElementInsertion andDeletion 161

ExaminingOtherSTLContainers 161

PlanningYourPrograms 162



Introducing Hangman.164


SettingUptheProgram 165

Initializing VariablesandConstants.166

EnteringtheMainLoop 166






Exercises 171

Chapter 5 Functions:MadLib174


Introducing theInstructions Progra175


DefiningFunctions 177




Introducing theYesorNoProgram.178


AcceptingValuesinto Parameter181

UnderstandingEncapsulation 183



Introducing theScopingProgram184




Introducing theGlobalReachProgram189

DeclaringGlobalVariables 191

AccessingGlobalVariables 191

Hiding Global Variables192

Altering Global Variables192

Minimizing the Use of Global Variables193

Using Global Constants193

Using Default Arguments 194

Introducing the Give Me a Number Program194

Specifying Default Arguments 196

Assigning Default Arguments to Parameters196

Overriding Default Arguments197

Overloading Functions 197

Introducing the Triple Program197

Creating Overloaded Functions199

Calling Overloaded Functions 200

Inlining Functions 200

Introducing the Taking Damage Program200

Specifying Functions for Inlining 202

Calling Inlined Functions 202

Introducing the Mad Lib Game203

Setting Up the Program 204

The main() Function204

The askText() Function205

The askNumber() Function205

The tellStory() Function206

Summary 206

Questions and Answers207

Discussion Questions209

Exercises 209

Chapter 6 References: Tic-Tac-Toe.210

Using References 210

Introducing the Referencing Program 210

Creating References212

Accessing Referenced Values213

Altering Referenced Values 213

Passing References to Alter Arguments214

Introducing the Swap Program214

Passing by Value 216

Passing by Reference217

Passing References for Efficiency 218

Introducing the Inventory Displayer Program218

Understanding the Pitfalls of Reference Passing219

Declaring Parameters as Constant References 220

Passing a Constant Reference 220

Deciding How to Pass Arguments 221

Returning References221

Introducing the Inventory Referencer Program222

Returning a Reference223

Displaying the Value of a Returned Reference 224

Assigning a Returned Reference to a Reference225

Assigning a Returned Reference to a Variable 225

Altering an Object through a Returned Reference225

Introducing the Tic-Tac-Toe Game226

Planning the Game 226

Setting Up the Program 228

The main() Function230

The instructions() Function 231

The askYesNo() Function 231

The askNumber() Function232

The humanPiece() Function 232

The opponent() Function 233

The displayBoard() Function233

The winner() Function 234

The isLegal() Function 235

The humanMove() Function 236

The computerMove() Function 236

The announceWinner() Function 240

Chapter 7 Pointers: Tic-Tac-Toe 2.0 246

Understanding Pointer Basics246

Introducing the Pointing Program247

Declaring Pointers249

Initializing Pointers 250

Assigning Addresses to Pointers250

Dereferencing Pointers251

Reassigning Pointers252

Using Pointers to Objects253

Understanding Pointers and Constants 254

Using a Constant Pointer 254

Using a Pointer to a Constant 255

Using a Constant Pointer to a Constant256

Summarizing Constants and Pointers257

Passing Pointers257

Introducing the Swap Pointer Version Program257

Passing by Value 259

Passing a Constant Pointer 260

Returning Pointers261

Introducing the Inventory Pointer Program 262

Returning a Pointer 263

Using a Returned Pointer to Display a Value264

Assigning a Returned Pointer to a Pointer265

Assigning to a Variable the Value Pointed to by a Returned Pointer 265

Altering an Object through a Returned Pointer266

Understanding the Relationship between Pointers and Arrays267

Introducing the Array Passer Program 267

Using an Array Name as a Constant Pointer269

Passing and Returning Arrays 270

Introducing the Tic-Tac-Toe 2.0 Game271

Questions and Answers273

Discussion Questions275

Exercises 275

Chapter 8 Classes: Critter Caretaker278

Defining New Types 278

Introducing the Simple Critter Program279

Defining a Class 280

Defining Member Functions281

Instantiating Objects282

Accessing Data Members 282

Calling Member Functions283

Using Constructors283

Introducing the Constructor Critter Program284

Declaring and Defining a Constructor 285

Calling a Constructor Automatically 286

Setting Member Access Levels 287

Introducing the Private Critter Program287

Specifying Public and Private Access Levels 289

Defining Accessor Member Functions290

Defining Constant Member Functions 291

Using Static Data Members and Member Functions 292

Introducing the Static Critter Program 293

Declaring and Initializing Static Data Members295

Accessing Static Data Members295

Declaring and Defining Static Member Functions 296

Calling Static Member Functions 296

Introducing the Critter Caretaker Game297

Planning the Game 298

Planning the Pseudocode299

The Critter Class 300

The main() Function303

Summary 304

Questions and Answers306

Discussion Questions308

Exercises 308

Chapter 9 Advanced Classes and Dynamic Memory:Game Lobby 310

Using Aggregation310

Introducing the Critter Farm Program 311

Using Object Data Members313

Using Container Data Members314

Using Friend Functions and Operator Overloading315

Introducing the Friend Critter Program315

Creating Friend Functions318

Overloading Operators318

Dynamically Allocating Memory319

Introducing the Heap Program320

Using the new Operator 322

Using the delete Operator323

Avoiding Memory Leaks 324

Working with Data Members and the Heap 326

Introducing the Heap Data Member Program 326

Declaring Data Members that Point to Values on the Heap330

Declaring and Defining Destructors 331

Declaring and Defining Copy Constructors332

Overloading the Assignment Operator336

Introducing the Game Lobby Program 338

The Player Class 339

The Lobby Class 341

The Lobby::AddPlayer() Member Function343

The Lobby::RemovePlayer() Member Function 345

The Lobby::Clear() Member Function345

The operator<<() Member Function346

The main() Function347

Questions and Answers349

Discussion Questions351

Chapter 10 Inheritance and Polymorphism: Blackjack354

Introducing Inheritance354

Introducing the Simple Boss Program 356

Deriving from a Base Class 358

Instantiating Objects from a Derived Class359

Using Inherited Members360

Controlling Access under Inheritance360

Introducing the Simple Boss 2.0 Program 361

Using Access Modifiers with Class Members 362

Using Access Modifiers when Deriving Classes 363

Calling and Overriding Base Class Member Functions363

Introducing the Overriding Boss Program364

Calling Base Class Constructors366

Declaring Virtual Base Class Member Functions367

Overriding Virtual Base Class Member Functions 367

Calling Base Class Member Functions368

Using Overloaded Assignment Operators and Copy Constructors in Derived Classes369

Introducing Polymorphism370

Introducing the Polymorphic Bad Guy Program370

Using Base Class Pointers to Derived Class Objects373

Defining Virtual Destructors374

Using Abstract Classes 375

Introducing the Abstract Creature Program 375

Declaring Pure Virtual Functions 377

Deriving a Class from an Abstract Class378

Introducing the Blackjack Game379

Designing the Classes 379

Planning the Game Logic383

The Card Class384

The Hand Class386

The GenericPlayer Class389

The Player Class 391

The House Class 392

The Deck Class393

The Game Class396

The main() Function399

Overloading the operator<<() Function 400

Summary 402

Questions and Answers403

Discussion Questions405

Exercises 405

Appendix A Creating Your First C++ Program 406

Appendix B Operator Precedence.412

Appendix C Keywords 414

Appendix D ASCII Chart 416

Appendix E Escape Sequences.420

Index 422