Example programmes used in the text and elsewhere; bold numbers refer to the text, code marked bad code doesn't compile and is posted to illustrate some interesting error. Of course some code compiles but illustrates a run time error, this isn't marked bad code, but that doesn't mean it isn't bad.
1. A quick example
1.1 HelloWorld.cpp
1.3 HelloWorld_1.3.cpp
1.4 HelloWorld_1.4.cpp
2. Variables
2.1 Add.cpp
2.1a Assign.cpp
2.1c DataTypes.cpp
Bad code involving char: BadChar.cpp
Example involving c++ and ++c: Increment.cpp
Example which test if an input is an integer: IntTest.cpp
2.3 UIntAdd.cpp
2.4 Cast.cpp
2.6 String.cpp
2.8 Scope.cpp
2.9 Scope_2.9.cpp
Ex 2.1 Declaring without assigning. Exercise_2.1.cpp
Ex 2.2 Answered above as Increment.cpp
Ex 2.3 Dividing integers. Exercise_2.3.cpp
Ex 2.4 Adding two chars. Exercise_2.4.cpp
Ex 2.5 inf and nan. Exercise_2.5.cpp
3. Flow control: if
3.1 If.cpp
3.2 Bigger.cpp
3.4 Relational.cpp
3.6 Negation.cpp
3.7 Logic.cpp
3.8 LazyAnd.cpp
Failed example of lazy evaluation: BadLazyAnd.cpp
Ex 3.4 The switch statement. Exercise_3.4.cpp
Example Switch works for char to: SwitchChar.cpp
Bad code involving switch with a variable case
condition BadSwitch.cpp,
corrected
at BadSwitchCorrected.cpp
4. Flow control: for and while
4.1 ForAdd.cpp
4.2 ForAdd_4.2.cpp
4.3 Growth.cpp
4.6 EraseString.cpp
Ex 4.1 Chars and ints Exercise_4.1.cpp
Example: this is Ex 4.1 adjusted to give a numbered table, useful for doing casts. Exercise_4.1.table.cpp
Ex 4.2 Incrementing chars Exercise_4.2.cpp
Ex 4.3 Scoping of the for loop variable. Exercise_4.3.cpp
Ex 4.4 while example with cin. Exercise_4.4.cpp
Ex 4.5 do/while example. Exercise_4.5.cpp
5. Functions
5.1 3nPlus1.cpp
5.2 3nPlus1_withHeader.cpp
5.3 3nPlus1_recursive.cpp
Example The traditional recursive example: the factorial, 1PlusX.cpp
5.4 Cursing.cpp
Example The rule 30 cellular automaton: CellularAutomaton.cpp
5.6 Adding.cpp
5.7 BadSwap.cpp
5.9 Swap.cpp
Ex 5.1 High or low example. Exercise_5.1.cpp
Enum example EnumExample.cpp
6. The Standard Template Library
6.1 Vectors.cpp
6.1 With at(i) replaced by [i] Vectors_noAt.cpp
Bad code showing you can't overload already defined operators for the core datatypes SillyExample.cpp
Example showing lots of overloading OverloadORama.cpp
6.2 MatrixVectorMult.cpp
6.2 With at(i) replaced by [i] MatrixVectorMult_noAt.cpp
Example The matrix multiplication with a switch to make the matrices simpler: MatrixVectorMult_withIsBinary.cpp
Example using the function in 6.3: VectorMatrixMult.cpp
6.3 Pointer_Short.cpp
Example Pass-by-pointer version of Swap: Swap_Pointers.cpp
Example An example with new. NewPointer.cpp
6.4 Iterator.cpp
Example An example which passes iterators to a function. PassingIterator.cpp
Ex 6.1 Does push_back return? No is the simple answer and trying to cout it gives terrible errors: Bad code BadExercise_6.1.cpp
Ex 6.2 Answered by the Bad code SillyExample.cpp above.
Ex 6.3 atoi() Exercise_6.3.cpp
Ex 6.6 Craps Exercise_6.6.cpp
Ex 6.7 Dangling pointer Exercise_6.7.cpp
Ex 6.10 Sorting chars Exercise_6.10.cpp
7. Classes
7.1 Bop.cpp
7.2 DoBop.cpp
7.3 Polynomial.cpp
7.4 DoPolynomial.cpp
7.5 fCoeffs
Examples of inputting chars CharReadIn.cpp CharReadIn_2.cpp Example
|