Part 1: Jumping into C++ ............................................................................................................................. 11 Contents Chapter 1: Introduction and Developer Environment Setup ................................................................ 13 What is a programming language? ......................................................................................................... 13 I've heard of a language called C, what’s the difference between C and C++? ...................................... 13 Do I need to know C to learn C++? ......................................................................................................... 13 Do I need to know math to be a programmer? ...................................................................................... 13 Terminology ............................................................................................................................................ 14 Programming ...................................................................................................................................... 14 Executable ........................................................................................................................................... 14 Editing and compiling source files .......................................................................................................... 14 A note about sample source code .......................................................................................................... 14 Windows ................................................................................................................................................. 15 Step 1: Download Code::Blocks .......................................................................................................... 15 Step 2: Install Code::Blocks ................................................................................................................. 15 Step 3: Running Code::Blocks ............................................................................................................. 15 Troubleshooting .................................................................................................................................. 21 What exactly is Code::Blocks?............................................................................................................. 23 Macintosh ............................................................................................................................................... 23 XCode .................................................................................................................................................. 24 Installing XCode 3 ................................................................................................................................ 24 Running XCode .................................................................................................................................... 24 Creating your first C++ program in XCode .......................................................................................... 24 Installing XCode 4 ................................................................................................................................ 29 Running XCode .................................................................................................................................... 29 Creating your first C++ program in XCode .......................................................................................... 30 Troubleshooting .................................................................................................................................. 35 Linux ........................................................................................................................................................ 37 Step 1: Installing g++ ........................................................................................................................... 38 Step 2: Running g++ ............................................................................................................................ 38 Step 3: Running your program ............................................................................................................ 38 Step 4: Setting up a text editor ........................................................................................................... 39 Configuring Nano ................................................................................................................................ 39 Using Nano .......................................................................................................................................... 40 Chapter 2: The Basics of C++ ................................................................................................................. 43 Intro to the C++ language ....................................................................................................................... 43 The simplest C++ program .................................................................................................................. 43 What happens if you don't see your program? .................................................................................. 45 The basic structure of a C++ program ................................................................................................. 45 Commenting your programs ................................................................................................................... 46 Thinking like a programmer and creating reusable code ....................................................................... 47 A few words on the joys and pain of practice ......................................................................................... 47 Quiz yourself ........................................................................................................................................... 48 Practice problems ................................................................................................................................... 49 Chapter 3: User Interaction and Saving Information with Variables .................................................... 50 Declaring variables in C++ ................................................................................................................... 50 Using variables .................................................................................................................................... 50 What if your program exits immediately? .......................................................................................... 51 Changing, using and comparing variables........................................................................................... 52 Shorthand for adding and subtracting one ......................................................................................... 52 The use and misuse of variables ............................................................................................................. 54 Common errors when declaring variables in C++ ............................................................................... 54 Case sensitivity .................................................................................................................................... 55 Naming variables ................................................................................................................................. 55 Storing strings ......................................................................................................................................... 56 Okay, I get strings—but why all those other types? ............................................................................... 58 Quiz yourself ........................................................................................................................................... 60 Practice problems ................................................................................................................................... 61 Chapter 4: If Statements ....................................................................................................................... 62 Basic syntax for if .................................................................................................................................... 62 Expressions.............................................................................................................................................. 63 What is truth? ..................................................................................................................................... 63 The bool type ...................................................................................................................................... 64 Else statements ....................................................................................................................................... 65 Else-if ....................................................................................................................................................... 65 String comparisons .................................................................................................................................. 66 More interesting conditions using Boolean operators ........................................................................... 66 Boolean not ......................................................................................................................................... 67 Boolean and ........................................................................................................................................ 67 © Alex Allain (www.cprogramming.com) 1 Boolean or ........................................................................................................................................... 68 Combining expressions ....................................................................................................................... 68 Example Boolean expressions ............................................................................................................. 70 Quiz yourself ........................................................................................................................................... 70 Practice problems ................................................................................................................................... 71 Chapter 5: Loops ................................................................................................................................... 72 While loops ............................................................................................................................................. 72 A common mistake ............................................................................................................................. 72 For loops ................................................................................................................................................. 73 Variable initialization .......................................................................................................................... 74 Loop condition .................................................................................................................................... 74 Variable update ................................................................................................................................... 74 Do-while loops ........................................................................................................................................ 75 Controlling the flow of loops .................................................................................................................. 76 Nested loops ........................................................................................................................................... 77 Choosing the right kind of loop ............................................................................................................... 78 For loop ............................................................................................................................................... 79 While loops ......................................................................................................................................... 79 Do-while loops .................................................................................................................................... 79 Quiz yourself ........................................................................................................................................... 80 Practice problems ................................................................................................................................... 81 Chapter 6: Functions ............................................................................................................................. 82 Function syntax ....................................................................................................................................... 82 Local variables and global variables ........................................................................................................ 83 Local variables ..................................................................................................................................... 83 Global variables ................................................................................................................................... 85 A warning about global variables ........................................................................................................ 86 Making functions available for use ......................................................................................................... 86 Function definitions and declarations ................................................................................................ 87 An example of using a function prototype ......................................................................................... 87 Breaking down a program into functions ............................................................................................... 88 When you’re repeating code again and again .................................................................................... 88 When you want to make code easier to read ..................................................................................... 88 Naming and overloading functions ......................................................................................................... 89 Summary of functions ............................................................................................................................. 90 © Alex Allain (www.cprogramming.com) 2 Quiz yourself ........................................................................................................................................... 90 Practice problems ................................................................................................................................... 90 Chapter 7: What If You Can’t Figure Out What to Do? ......................................................................... 92 All we need to do is check if the number has no remainder when divided by the divisor:.................... 94 A brief aside about efficiency and security ............................................................................................. 95 What if you don’t know the algorithm? .................................................................................................. 96 Practice Problems ................................................................................................................................... 98 Chapter 8: Switch Case and Enums ....................................................................................................... 99 Comparison of switch case with if-else ................................................................................................. 101 Creating simple types using enumerations ........................................................................................... 101 Quiz yourself ......................................................................................................................................... 103 Practice problems ................................................................................................................................. 104 Chapter 9: Randomizing Your Programs ............................................................................................. 105 Getting random numbers in C++ ........................................................................................................... 105 Bugs and randomness ........................................................................................................................... 108 Quiz yourself ......................................................................................................................................... 108 Practice problems ................................................................................................................................. 109 Part 2: Working with Data......................................................................................................................... 110 Chapter 10: Arrays ................................................................................................................................ 111 Some basic array syntax ........................................................................................................................ 111 Example uses for arrays ........................................................................................................................ 112 Using arrays to store orderings ......................................................................................................... 112 Representing grids with multi-dimensional array............................................................................. 112 Using arrays ........................................................................................................................................... 113 Arrays and for loops .......................................................................................................................... 113 Passing arrays to functions ............................................................................................................... 114 Writing off the end of an array ......................................................................................................... 115 Sorting arrays ........................................................................................................................................ 116 Quiz yourself ......................................................................................................................................... 120 Practice problems ................................................................................................................................. 121 Chapter 11: Structures .......................................................................................................................... 122 Associating multiple values together .................................................................................................... 122 Syntax ................................................................................................................................................ 122 Passing structures around ................................................................................................................. 124 Quiz yourself ......................................................................................................................................... 126 © Alex Allain (www.cprogramming.com) 3 Practice problems ................................................................................................................................. 127 Chapter 12: Introduction to Pointers .................................................................................................... 128 Forget everything you’ve ever heard .................................................................................................... 128 Ok, then—what are pointers? Why should you care? .......................................................................... 128 What is memory? .................................................................................................................................. 129 Variables vs. addresses ..................................................................................................................... 129 Memory layout.................................................................................................................................. 130 Other advantages (and disadvantages) of pointers .............................................................................. 132 Quiz yourself ......................................................................................................................................... 133 Practice problems ................................................................................................................................. 133 Chapter 13: Using Pointers ................................................................................................................... 135 Pointer syntax ....................................................................................................................................... 135 Declaring a pointer ............................................................................................................................ 135 Pointing to something: getting the address of a variable ..................................................................... 135 Using a pointer .................................................................................................................................. 136 Uninitialized pointers and NULL ........................................................................................................... 139 Pointers and functions .......................................................................................................................... 140 References ............................................................................................................................................ 142 References vs. pointers ..................................................................................................................... 143 Quiz yourself ......................................................................................................................................... 144 Practice problems ................................................................................................................................. 144 Chapter 14: Dynamic Memory Allocation ............................................................................................. 146 Getting more memory with new .......................................................................................................... 146 Running out of memory .................................................................................................................... 146 References and dynamic allocation .................................................................................................. 147 Pointers and arrays ............................................................................................................................... 147 Multidimensional arrays ....................................................................................................................... 149 Pointer arithmetic ................................................................................................................................. 149 Understanding two dimensional arrays ............................................................................................ 150 Pointers to pointers .......................................................................................................................... 151 Pointers to pointers and two dimensional arrays ............................................................................. 153 Taking stock of pointers ........................................................................................................................ 154 Quiz yourself ......................................................................................................................................... 154 Practice problems ................................................................................................................................. 155 Chapter 15: Introduction to Data Structures with Linked Lists ............................................................ 157 © Alex Allain (www.cprogramming.com) 4 Pointers and structures ......................................................................................................................... 159 Creating a linked list .............................................................................................................................. 160 First time through ............................................................................................................................. 161 Second time through ........................................................................................................................ 161 Traversing a linked list .......................................................................................................................... 163 Taking stock of linked lists .................................................................................................................... 164 Arrays vs linked lists .......................................................................................................................... 165 Quiz yourself ......................................................................................................................................... 167 Practice problems ................................................................................................................................. 168 Chapter 16: Recursion ........................................................................................................................... 169 How to think about recursion ............................................................................................................... 169 Recursion and data structures .............................................................................................................. 171 Loops and recursion .............................................................................................................................. 173 The stack ............................................................................................................................................... 175 The power of the stack ..................................................................................................................... 177 Downsides of recursion..................................................................................................................... 177 Debugging stack overflows ............................................................................................................... 178 Performance ..................................................................................................................................... 179 Taking stock of recursion ...................................................................................................................... 180 Quiz yourself ......................................................................................................................................... 180 Practice problems ................................................................................................................................. 181 Chapter 17: Binary Trees ...................................................................................................................... 182 Talking about trees ........................................................................................................................... 184 Implementing binary trees ................................................................................................................ 184 Inserting into the tree ....................................................................................................................... 185 Searching the tree ............................................................................................................................. 188 Destroying the tree .......................................................................................................................... 188 Removing from a tree ....................................................................................................................... 190 Real world use of binary trees .............................................................................................................. 197 Cost of building trees and maps ....................................................................................................... 199 Quiz yourself ......................................................................................................................................... 199 Practice problems ................................................................................................................................. 200 Chapter 18: The Standard Template Library ......................................................................................... 201 Vectors, a resizable array ...................................................................................................................... 201 Calling methods on vectors ............................................................................................................... 202 © Alex Allain (www.cprogramming.com) 5 Other features of vectors .................................................................................................................. 203 Maps ..................................................................................................................................................... 204 Iterators ................................................................................................................................................ 205 Checking if a value is in a map .......................................................................................................... 207 Taking stock of the STL .......................................................................................................................... 208 Learning more about the STL ................................................................................................................ 209 Quiz yourself ......................................................................................................................................... 209 Practice problems ................................................................................................................................. 210 Chapter 19: More about Strings ........................................................................................................... 211 Reading in strings .................................................................................................................................. 211 String length and accessing individual elements .................................................................................. 212 Searching and substrings ...................................................................................................................... 213 Passing by reference ............................................................................................................................. 214 Const propagation............................................................................................................................. 216 Const and the STL .............................................................................................................................. 217 Quiz yourself ......................................................................................................................................... 218 Practice problems ................................................................................................................................. 218 Chapter 20: Debugging with Code::Blocks ............................................................................................ 220 Starting out ........................................................................................................................................... 221 Breaking in ............................................................................................................................................ 222 Debugging crashes ............................................................................................................................ 228 Breaking into a hung program .......................................................................................................... 231 Modifying variables ........................................................................................................................... 235 Summary ........................................................................................................................................... 235 Practice problems ................................................................................................................................. 235 Problem 1: Issues with exponents .................................................................................................... 235 Problem 2: Trouble adding numbers ................................................................................................ 236 Problem 3: Bugs with Fibonacci ........................................................................................................ 236 Problem 4: Misreading and misreplaying a list ................................................................................. 237 Part 3: Writing Larger Programs ............................................................................................................... 238 Chapter 21: Breaking Programs Up Into Smaller Pieces ....................................................................... 239 Understanding the C++ build process ................................................................................................... 239 Preprocessing .................................................................................................................................... 239 Compilation ....................................................................................................................................... 241 Linking ............................................................................................................................................... 241 © Alex Allain (www.cprogramming.com) 6 Why separate compiling and linking? ............................................................................................... 241 How to split your program across multiple files ................................................................................... 242 Step 1: Splitting our declarations and definitions ............................................................................. 242 Step 2: Figure out which functions need to be shared ..................................................................... 242 Step 3: Move shared functions into their new files .......................................................................... 243 Going through an example ............................................................................................................... 243 Other dos and don'ts of header files ................................................................................................ 247 Handling multiple source files in your development environment .................................................. 247 Quiz yourself ......................................................................................................................................... 250 Practice problems ................................................................................................................................. 251 Chapter 22: Introduction to Program Design ....................................................................................... 252 Redundant code .................................................................................................................................... 252 Assumptions about how data is stored ................................................................................................ 253 Design and comments ........................................................................................................................... 254 Quiz yourself ......................................................................................................................................... 255 Chapter 23: Hiding the Representation of Structured Data ................................................................. 257 Using functions to hide the layout of a structure ............................................................................. 257 Method declaration and call syntax .................................................................................................. 258 Quiz yourself ......................................................................................................................................... 260 Practice problems ................................................................................................................................. 261 Chapter 24: The Class ............................................................................................................................ 262 Hiding how data is stored ..................................................................................................................... 262 Declaring an instance of a class ............................................................................................................ 264 The responsibilities of a class ................................................................................................................ 264 What does private really mean? ....................................................................................................... 265 Summary ............................................................................................................................................... 266 Quiz yourself ......................................................................................................................................... 266 Practice problems ................................................................................................................................. 266 Chapter 25: The Lifecycle of a Class ...................................................................................................... 267 Object construction .............................................................................................................................. 267 What happens if you don't create a constructor? ............................................................................ 269 Initializing members of the class ....................................................................................................... 270 Using the initialization list for const fields ........................................................................................ 271 Object destruction ................................................................................................................................ 271 Destruction on delete ....................................................................................................................... 273 © Alex Allain (www.cprogramming.com) 7 Destruction when going out of scope ............................................................................................... 273 Destruction due to another destructor ............................................................................................ 274 Copying classes ..................................................................................................................................... 275 The assignment operator .................................................................................................................. 276 The copy constructor ........................................................................................................................ 278 The full list of compiler generated methods ..................................................................................... 280 Preventing copying entirely .............................................................................................................. 280 Quiz yourself ......................................................................................................................................... 281 Practice problems ................................................................................................................................. 282 Chapter 26: Inheritance and Polymorphism ......................................................................................... 283 Inheritance in C++ ................................................................................................................................. 284 Other uses and misuses of inheritance ............................................................................................. 287 Inheritance, object construction and object destruction ................................................................. 288 Polymorphism and object destruction.............................................................................................. 290 The slicing problem ........................................................................................................................... 291 Sharing code with subclasses ............................................................................................................ 293 Protected data .................................................................................................................................. 293 Class-wide data ................................................................................................................................. 293 How is polymorphism implemented? ............................................................................................... 295 Quiz yourself ......................................................................................................................................... 297 Practice problems ................................................................................................................................. 298 Chapter 27: Namespaces ...................................................................................................................... 299 When to write "using namespace" ................................................................................................... 301 When should you create a namespace? ........................................................................................... 301 Quiz yourself ......................................................................................................................................... 302 Practice problems ................................................................................................................................. 302 Chapter 28: File I/O ............................................................................................................................... 303 File I/O basics ........................................................................................................................................ 303 Reading from files ............................................................................................................................. 303 File formats ........................................................................................................................................... 305 End of file .......................................................................................................................................... 306 Writing files ........................................................................................................................................... 307 Creating new files.............................................................................................................................. 308 File position ........................................................................................................................................... 308 Accepting command line arguments .................................................................................................... 311 © Alex Allain (www.cprogramming.com) 8
Description: