Table of Contents....5
About the Author....16
About the Technical Reviewer....17
Acknowledgments....18
Introduction....20
Chapter 1: Getting Started....25
Initial setup....25
…in Unix....26
…in MinGW....27
…in Microsoft Visual Studio....29
A simple program....30
Spacing....32
Creating an SSDL project....34
…with g++ (Unix or MinGW)....34
The files you created....36
Cool command-line tricks....36
Antibugging....37
…in Microsoft Visual Studio....37
Compiling your program....38
The files you created....42
Reopening your project....45
Antibugging....45
How not to be miserable (whatever your platform)....47
Shapes and the functions that draw them....49
Antibugging....58
consts and colors....60
Text....63
sout, escape sequences, and fonts....63
SSDL_RenderText, SSDL_RenderTextCentered....67
Chapter 2: Images and Sound....71
Images and window characteristics....71
Antibugging....77
Multiple images together....79
Adding transparency with GIMP....81
Sound....86
Antibugging....89
Chapter 3: Numbers....90
Variables....90
Constants....93
When to use constants, not literal values....93
Math operators....95
Integer division....96
Assignment (=) operators....96
A diving board example....97
The no-worries list for math operators....100
Built-in functions and casting....101
Antibugging....106
Chapter 4: Mouse, and if....108
Mouse functions....108
Antibugging....111
if....112
Coercion and if conditions (if’s dirty little secret)....115
Combining conditions with &&, ||, and !....115
Antibugging....116
Boolean values and variables....119
A hidden object game....121
Chapter 5: Loops, Input, and char....129
Keyboard input....129
Antibugging....131
while and do-while....133
Loops with SSDL....134
break and continue....136
Antibugging....136
for loops....140
Increment operators....140
An example: Averaging numbers....141
Antibugging....143
chars and cctype....145
Antibugging....148
switch....150
Antibugging....152
Chapter 6: Algorithms and the Development Process....154
Adventures in robotic cooking....154
Writing a program, from start to finish....158
Requirements: What do we want to do?....158
Algorithm: How do we do it?....159
Trace the algorithm: Will it work?....161
Coding: Putting it all into C++ (plus: commenting the lazy way)....161
Chapter 7: Functions....167
Functions that return values....167
Functions that return nothing....174
Global variables....178
Antibugging....180
How to write a function in four easy steps (and call it in one)....182
Why have functions, anyway?....188
Recap....196
Chapter 8: Functions, Continued....198
Random numbers....198
Making a random number generator....198
Using the built-in random number generator....201
Antibugging....204
Boolean functions....206
& parameters....208
Antibugging....213
Identifier scope....214
A final note on algorithms....216
Chapter 9: Using the Debugger....217
Breakpoints and watched variables....221
ddd....222
gdb....223
Visual Studio....223
Fixing the stripes....225
Going into functions....225
ddd....225
gdb....226
Visual Studio....227
Fixing the stars....228
Wrap-up....229
Antibugging....229
Bottom-up testing....230
More on antibugging....231
Chapter 10: Arrays and enum....234
Arrays....234
Arrays’ dirty little secret: using memory addresses....237
Antibugging....238
Arrays as function parameters....239
Array parameters that change, or don’t....239
Array parameters and reusability....240
Antibugging....241
Enumeration types....242
Antibugging....245
Multidimensional arrays....246
Displaying the board....247
Arrays of more than two dimensions....251
Antibugging....252
Chapter 11: Animation with structs and Sprites....254
structs....254
Making a movie with struct and while....259
Sprites....266
Antibugging....272
Chapter 12: Making an Arcade Game: Input, Collisions, and Putting It All Together....273
Determining input states....273
Mouse....273
Keyboard....274
Antibugging....276
Events....276
Cooldowns and lifetimes....278
Collisions....282
The big game....283
Antibugging....297
Chapter 13: Standard I/O and File Operations....301
Standard I/O programs....301
Compiling standard I/O programs....302
Building a project from scratch (optional)....303
…in Microsoft Visual Studio....303
Antibugging....305
…with g++....306
Antibugging....307
File I/O (optional)....307
cin and cout as files....308
Setting up the command prompt in Windows....308
Redirecting I/O at the command prompt....309
while (cin)....310
Reading in characters, including whitespace....312
Antibugging....313
Using filenames....314
Chapter 14: Character Arrays and Dynamic Memory....321
Character arrays....321
Antibugging....325
Dynamic allocation of arrays....328
Antibugging....331
Using the * notation....334
Antibugging....338
Chapter 15: Classes....340
Constructors....343
Antibugging....347
const objects, const member functions…....349
Antibugging....350
…and const parameters....350
Multiple constructors....351
Copy constructors....351
Default constructors....352
Conversion constructors....353
Summary....353
Antibugging....354
Default parameters for code reuse....355
Date program (so far)....356
Chapter 16: Classes, Continued....360
inline functions for efficiency....360
Access functions....362
Separate compilation and include files....363
What happens in separate compilation....364
Writing your .h file....365
Including a .h file only once....366
Avoid using namespace std; in include files....368
Backing up a multi-file project....368
Antibugging....368
Multiple-file projects in Microsoft Visual Studio....370
Multiple-file projects in g++....372
Command line: more typing, less thinking....372
Makefiles: more thinking, less typing (optional)....372
A simple version....373
A better version....374
A complete version....375
Antibugging....377
Final Date program....377
Chapter 17: Operators....383
The basic string class....383
Destructors....385
Binary and unary operators: ==, !=, and !....386
All other comparison operators at once....387
Assignment operators and *this....388
Antibugging....391
Arithmetic operators....391
[] and ()....394
++ and --....396
>> and <<: operators that aren’t class members....397
static members....400
Explicit call to constructor....401
The final String program....402
#include ....408
Chapter 18: Exceptions, Move Constructors and =, Recursion, and O notation....409
Exceptions....409
Move constructors and move = (optional)....414
Recursion (optional; referenced in the next section)....416
Antibugging....420
Efficiency and O notation (optional)....421
Chapter 19: Inheritance....425
The basics of inheritance....425
Constructors and destructors for inheritance and member variables....430
Inheritance as a concept....431
Classes for card games....433
An inheritance hierarchy....436
private inheritance....439
Hiding an inherited member function....441
A game of Montana....442
Chapter 20: Templates....453
Function templates....453
Antibugging....455
Concepts for function templates (optional)....456
Antibugging....459
The Vector class....459
Efficiency and O notation (optional)....463
Making Vector a template....465
Antibugging....469
Concepts for class templates (optional)....469
pair....471
Non-type template arguments....472
#include ....474
Chapter 21: Virtual Functions and Multiple Inheritance....475
Virtual functions, plus: move functions with movable parents and class members....475
Behind the scenes....480
Pure virtual functions and abstract base classes....481
Why virtual functions often mean using pointers....481
Virtual destructors....486
Move functions with movable parents and class members (optional)....488
Antibugging....489
Multiple inheritance (optional)....492
Antibugging....493
Chapter 22: Linked Lists....496
What lists are and why have them....496
Efficiency and O notation (optional)....498
Starting the linked list template....499
List::List ()....501
void List::push_front (const T& newElement);....501
void List::pop_front ()....503
List::~List ()....505
->: a bit of syntactic sugar....506
More friendly syntax: pointers as conditions....506
The linked list template....507
Antibugging....510
#include ....512
Chapter 23: The Standard Template Library....513
Iterators....513
…with vector too....516
const and reverse iterators....517
Antibugging....518
Getting really lazy: range-based for and auto....520
Spans....521
initializer_lists (optional)....524
(optional)....526
Antibugging....527
Chapter 24: Building Bigger Projects....529
Namespaces....529
Conditional compilation....530
Libraries....531
g++....532
Compiling the library....532
Linking the library....532
Running programs that use dynamic libraries....533
Makefiles....533
Microsoft Visual Studio....534
Static libraries: the easy option....534
Dynamic Link Libraries (DLLs): the not so easy option....537
Antibugging....543
Chapter 25: Esoterica (Recommended)....544
sstream: using strings like cin/cout....544
Formatted output with format strings....548
Antibugging....553
Command-line arguments....554
Debugging with command-line arguments in Unix....557
Debugging with command-line arguments in Visual Studio....558
Bit manipulation: &, |, ~, and <>....560
Antibugging....566
Chapter 26: Esoterica (Recommended), Continued....567
Defaulted constructors and =....567
constexpr and static_assert: moving work to compile time....568
Structured bindings and tuples: returning multiple values at once....572
Smart pointers....576
unique_ptr....576
shared_ptr....582
Antibugging....583
static_cast et al.....583
User-defined literals: automatic conversion between systems of measurement....585
Lambda functions for one-time use....588
Lambda captures....590
An example with lambda functions....591
Chapter 27: Esoterica (Not So Recommended)....595
protected sections, protected inheritance....595
friends and why you shouldn’t have any....600
User-defined conversions (cast operators)....604
Modules....606
Coroutines....606
Chapter 28: C....610
Compiling C....611
I/O....612
printf....612
scanf and the address-of (&) operator....613
fprintf and fscanf, fopen and fclose....615
sprintf and sscanf; fgets, fputs, and puts....617
Summary of commands....620
Antibugging....622
Parameter passing with *....622
Antibugging....625
Dynamic memory....625
Chapter 29: Moving on with SDL....627
Writing code....630
Antibugging....633
Compiling....633
Further resources....634
Appendix A: Help with Setup....635
…for Unix users....635
Debian/Ubuntu....635
RedHat/Fedora....636
SSDL....636
Antibugging....636
…for MinGW users....637
Antibugging....637
…for Microsoft Visual Studio users....638
Antibugging....638
…for other platforms....639
Sound....639
Making your own projects....640
...in g++....640
…in Microsoft Visual Studio....640
Appendix B: Operators....642
Associativity....642
Precedence....642
Overloading....644
Appendix C: ASCII Codes....645
Appendix D: Fundamental Types....647
Appendix E: Escape Sequences....649
Appendix F: Basic C Standard Library....650
cmath....650
cctype....651
cstdlib....651
Appendix G: Common Debugger Commands....652
Microsoft Visual Studio....652
gdb/ddd....653
Appendix H: SSDL Reference....654
Updating the screen....654
Added types....654
Clearing the screen....655
Colors....655
Drawing....656
Images....657
Mouse, keyboard, and events....658
Music....658
Quit messages....660
Sounds....660
Sprites....662
Text....664
Time and synchronization....665
Window....665
References....666
Index....667
Surely there’s no shortage of C++ intro texts. Why write yet another?
I’m glad you asked.
Ever since moving from Pascal to C++ (back when dinosaurs roamed the Earth), I’ve been underwhelmed by available resources. I wanted something quirky and fun to read, with sufficient coverage and fun examples, like the old Oh! Pascal! text by Cooper and Clancy. Even a perfectly accurate text with broad coverage gives you nothing if you fall asleep when you read it. Well, nothing but a sore neck.
But the other reason, of course, is to promote laziness.
We all want our projects to be done more quickly, with less wailing and gnashing of teeth. Sometimes, it’s said, you have to put your nose to the grindstone. Maybe, but I like my nose too well for that. I’d rather do things the easy way.
But the easy way isn’t procrastinating and dragging my feet; it’s to find something I love doing and do it well enough that it feels relatively effortless. It’s producing something robust enough that when it does break down, it tells me exactly what the problem is, so I don’t have to spend a week pleading with it to explain itself. It’s writing code that I can use again and again, adapting it to a new use in hours instead of days.