Introduction 1
About This Book 1
Foolish Assumptions 2
How This Book Is Organized 3
Part I: Getting Started with C++ Programming 3
Part II: Writing a Program: Decisions, Decisions 4
Part III: Becoming a Procedural Programmer 4
Part IV: Data Structures 4
Part V: Object-Oriented Programming 4
Part VI: Advanced Strokes 5
Part VII: The Part of Tens 5
Icons Used in This Book 5
Beyond the Book 6
Where to Go from Here 6
Part I: Getting Started with C++ Programming 7
Chapter 1: What Is a Program? 9
How Does My Son Differ from a Computer? 9
Programming a Human Computer 11
Creating the algorithm 11
Setting the tire-changing language 12
Constructing the program 13
Computer processors 17
Computer Languages 17
High-level languages 19
The C++ language 20
Chapter 2: Installing Code::Blocks 21
Reviewing the Compilation Process 21
Installing Code::Blocks 23
Windows installation 23
Ubuntu Linux installation 26
Mac OS installation 27
Setting up Code::Blocks 31
Testing the Code::Blocks Installation 33
Creating the project 34
Testing your default project 37
Chapter 3: Writing Your First Program 41
Creating a New Project 41
Filename extensions 43
Entering Your Program 44
Building the Program 46
Finding What Could Go Wrong 47
Misspelled commands 47
Missing semicolon 49
Using the Online Material 50
Running the Program 51
How the Program Works 51
The template 51
The Conversion program 53
Part II: Writing a Program: Decisions, Decisions 55
Chapter 4: Integer Expressions 57
Declaring Variables 57
Variable names 58
Assigning a value to a variable 59
Initializing a variable at declaration 60
Integer Constants 61
Expressions 62
Binary operators 62
Unraveling compound expressions 63
Unary Operators 65
The Special Assignment Operators 67
Chapter 5: Character Expressions 69
Defining Character Variables 69
Encoding characters 70
Example of character encoding 73
Encoding Strings of Characters 75
Special Character Constants 75
Chapter 6: if I Could Make My Own Decisions 79
The if Statement 79
Comparison operators 80
Say No to No braces 83
What Else Is There? 84
Nesting if Statements 86
Compound Conditional Expressions 89
Chapter 7: Switching Paths 93
Controlling Flow with the switch Statement 93
Control Fell Through: Did I break It? 96
Implementing an Example Calculator with the switch Statement 97
Chapter 8: Debugging Your Programs, Part I 101
Identifying Types of Errors 101
Avoiding Introducing Errors 102
Coding with style 102
Establishing variable naming conventions 103
Finding the First Error with a Little Help 104
Finding the Run-Time Error 105
Formulating test data 106
Executing the test cases 106
Seeing whats going on in your program 107
Part III: Becoming a Procedural Programmer 109
Chapter 9: while Running in Circles 111
Creating a while Loop 111
Breaking out of the Middle of a Loop 114
Nested Loops 117
Chapter 10: Looping for the Fun of It 121
The for Parts of Every Loop 121
Looking at an Example 123
Getting More Done with the Comma Operator 125
Chapter 11: Functions, I Declare! 129
Breaking Your Problem Down into Functions 129
Understanding How Functions Are Useful 130
Writing and Using a Function 131
Returning things 132
Reviewing an example 133
Passing Arguments to Functions 135
Function with arguments 136
Functions with multiple arguments 137
Exposing main() 137
Defining Function Prototype Declarations 139
Chapter 12: Dividing Programs into Modules 141
Breaking Programs Apart 141
Breaking Up Isnt That Hard to Do 142
Creating Factorialcpp 143
Creating an #include file 145
Including #include files 146
Creating maincpp 148
Building the result 149
Using the Standard C++ Library 149
Variable Scope 150
Chapter 13: Debugging Your Programs, Par t 2 151
Debugging a Dys-Functional Program 151
Performing unit level testing 153
Outfitting a function for testing 155
Returning to unit test 159
Part IV: Data Structures 163
Chapter 14: Other Numerical Variable Types 165
The Limitations of Integers in C++ 165
Integer round-off 166
Limited range 166
A Type That doubles as a Real Number 167
Solving the truncation problem 168
When an integer is not an integer 168
Discovering the limits of double 169
Variable Size the long and short of It 172
How far do numbers range? 174
Types of Constants 175
Passing Different Types to Functions 176
Overloading function names 177
Mixed-mode overloading 177
Chapter 15: Arrays 181
What Is an Array? 181
Declaring an Array 182
Indexing into an Array 183
Looking at an Example 184
Initializing an Array 187
Chapter 16: Arrays with Character 189
The ASCII-Zero Character Array 189
Declaring and Initializing an ASCIIZ Array 190
Looking at an Example 191
Looking at a More Detailed Example 193
Foiling hackers 197
Do I Really Have to Do All That Work? 198
Chapter 17: Pointing the Way to C++ Pointers 203
Whats a Pointer? 203
Declaring a Pointer 204
Passing Arguments to a Function 206
Passing arguments by value 206
Passing arguments by reference 209
Putting it together 211
Reference argument types 213
Playing with Heaps of Memory 214
Do you really need a new keyword? 214
Dont forget to clean up after yourself 215
Looking at an example 216
Chapter 18: Taking a Second Look at C++ Pointers 221
Pointers and Arrays 221
Operations on pointers 222
Pointer addition versus indexing into an array 224
Using the pointer increment operator 227
Why bother with array pointers? 230
Operations on Different Pointer Types 231
Constant Nags 231
Differences Between Pointers and Arrays 233
My main() Arguments 233
Arrays of pointers 234
Arrays of arguments 235
Chapter 19: Programming with Class 241
Grouping Data 241
The Class 242
The Object 243
Arrays of Objects 244
Looking at an Example 246
Chapter 20: Debugging Your Programs, Par t 3 253
A New Approach to Debugging 253
The solution 254
Entomology for Dummies 255
Starting the debugger 257
Fixing the (first) bug 264
Finding and fixing the second bug 265
Part V: Object-Oriented Programming 269
Chapter 21: What Is Object-Oriented Programming? 271
Abstraction and Microwave Ovens 271
Procedural nachos 273
Object-oriented nachos 273
Classification and Microwave Ovens 274
Why Build Objects This Way? 275
Self-Contained Classes 276
Chapter 22: Structured Play: Making Classes Do Things 277
Activating Our Objects 277
Creating a Member Function 278
Defining a member function 279
Naming class members 280
Calling a member function 281
Accessing other members from within a member function 282
Keeping a Member Function after Class 284
Overloading Member Functions 285
Chapter 23: Pointers to Objects 287
Pointers to Objects 287
Arrow syntax 288
Calling all member functions 288
Passing Objects to Functions 289
Calling a function with an object value 289
Calling a function with an object pointer 290
Looking at an example 292
Allocating Objects off the Heap 296
Chapter 24: Do Not Disturb: Protected Members 299
Protecting Members 299
Why you need protected members 300
Making members protected 301
So what? 303
Who Needs Friends, Anyway? 304
Chapter 25: Getting Objects Off to a Good Start 307
The Constructor 308
Limitations on constructors 309
Can I see an example? 310
Constructing data members 312
Destructors 315
Looking at an example 316
Destructing data members 318
Chapter 26: Making Constructive Arguments 321
Constructors with Arguments 321
Looking at an example 322
Overloading the Constructor 326
The Default default Constructor 330
Constructing Data Members 332
Initializing data members with the default constructor 332
Initializing data members with a different constructor 334
Looking at an example 337
New with C++ 2011 340
Chapter 27: Coping with the Copy Constructor 341
Copying an Object 341
The default copy constructor 342
Looking at an example 344
Creating a Copy Constructor 346
Avoiding Copies 349
Part VI: Advanced Strokes 351
Chapter 28: Inheriting a Class 353
Advantages of Inheritance 354
Learning the lingo 355
Implementing Inheritance in C++ 355
Looking at an example 356
Having a HAS_A Relationship 360
Chapter 29: Are Virtual Functions for Real? 361
Overriding Member Functions 361
Early binding 362
Ambiguous case 364
Enter late binding 366
When Is Virtual Not? 369
Virtual Considerations 371
Chapter 30: Overloading Assignment Operators 373
Overloading an Operator 374
Overloading the Assignment Operator Is Critical 374
Looking at an Example 376
Writing Your Own (or Not) 379
Chapter 31: Performing Streaming I/O 381
How Stream I/O Works 381
Stream Input/Output 383
Creating an input object 383
Creating an output object 385
Open modes 386
What is binary mode? 386
Hey, file, what state are you in? 387
Other Member Functions of the fstream Classes 391
Reading and writing streams directly 393
Controlling format 396
Whats up with endl? 399
Manipulating Manipulators 399
Using the stringstream Classes 400
Chapter 32: I Take Exception! 405
The Exception Mechanism 406
Examining the exception mechanism in detail 408
Special considerations for throwing 409
Creating a Custom Exception Class 410
Restrictions on exception classes 414
Part VII: The Part of Tens 415
Chapter 33: Ten Ways to Avoid Bugs 417
Enable All Warnings and Error Messages 417
Adopt a Clear and Consistent Coding Style 418
Comment the Code While You Write It 419
Single-Step Every Path in the Debugger at Least Once 419
Limit the Visibility 420
Keep Track of Heap Memory 420
Zero Out Pointers after Deleting What They Point To 421
Use Exceptions to Handle Errors 421
Declare Destructors Virtual 422
Provide a Copy Constructor and Overloaded Assignment Operator 422
Chapter 34: Ten Features Not Covered in This Book 423
The goto Command 423
The Ternary Operator 424
Binary Logic 425
Enumerated Types 425
Namespaces 425
Pure Virtual Functions 426
The string Class 426
Multiple Inheritance 427
Templates and the Standard Template Library 427
Lambda Functions 428
Index 429