Contents of each chapter



"Learn to Program Using C++ on the Raspberry Pi" Revised Edition


Phil Gardner





Here's a little more detail about what the book covers:


INTRODUCTION
About this book.
About the sample programs.
Conventions used throughout the book.
About the author.


CHAPTER 1: GETTING THINGS UP AND RUNNING
What is C++?
What do you need to get started?
The GNU g++ compiler.
An example of some really simple C++ code and what it means.
High-level and low-level code.
Typing high-level C++ code into the Nano text editor.
Compiling and running your first program.
Using the Geany text editor instead of Nano.
Finding out whether a program executes successfully.
Reporting an error by returning a code other than 0.
Chapter summary.
Problems and exercises.


CHAPTER 2: WRITING SOME SIMPLE CODE
Programs and statements.
Using a semi-colon ; to mark the end of a statement.
Compound statements – groups of statements enclosed within { } curly braces.
Putting comments in your code.
Expressions.
Operator symbols.
How to set out your C++ program code.
Adding spaces to make individual statements easier to read.
Chapter summary.
Problems and exercises.


CHAPTER 3: DATA-TYPES AND VALUES
Different types of data: int, float, bool, char and string.
Working with integer values: int.
Working with a smaller range of integer values: short.
Working with a larger range of integer values: long.
Overflow – values that are too big for a particular data-type.
Working with real numbers: float and double.
Working with character symbols: char.
Working with literal text: embedding text values in your code.
Working with named sequences of characters: string.
Named values: variables and constants.
Declaring variables in a program.
Declaring constants in a program.
Using descriptive names for constants and variables.
Rules for naming constants and variables.
Namespaces: avoiding clashes between identifiers with the same name.
Assigning a value to a constant or variable using the = assignment operator.
Chapter summary.
Problems and exercises.


CHAPTER 4: KEYBOARD INPUT AND SCREEN OUTPUT
Output: displaying data on the screen using "channel-out": cout.
Telling the computer to start a new line on the screen using endl.
Using identifiers from a namespace in your code.
Displaying several things at a time using the << output redirection operator.
Displaying the value of a stored variable on the screen.
Special characters that are denoted by \ backslash.
Controlling the cursor position on the screen.
Formatting values to change the way they are displayed.
Input: getting a value into the computer from the keyboard using cin.
Getting a whole line of text from the keyboard, including spaces, using getline.
Clearing unwanted characters from cin.
Validating numbers entered using the keyboard.
Chapter summary.
Problems and exercises.


CHAPTER 5: USING OPERATORS TO PROCESS DATA
Changing the value of variables with the ++ increment and -- decrement operators.
Different ways to use ++ increment and -- decrement.
The += "add amount" and -= "subtract amount" operators.
Multiplying numbers using the * operator.
Dividing numbers using the / operator.
The problem of attempting to divide a number by zero.
Mixing different data-types during division.
The results of floating point division.
Finding a remainder using the % modulus operator.
Using random numbers to produce unpredictable behaviour in a program.
Making the choices more random using a "seed".
Using % modulus to pick a random number within a certain range.
Exponents: Raising a number to a power using the pow() function.
Operator precedence – the order in which the computer carries out operations.
Using ( ) brackets in an expression to change how expressions are evaluated.
Finding the minimum or maximum of two values using min() and max().
Casts – converting between data-types. Using bit-wise operators to perform logical operations.
The bit-wise ~ NOT operator.
The bit-wise | OR operator.
The bit-wise & AND operator.
The bit-wise ^ XOR operator.
Logical shifts that "slide" patterns of bits to the left or right.
Chapter summary.
Problems and exercises.


CHAPTER 6: MAKING DECISIONS
Making simple decisions using if statements.
Simple decisions that use the  < <= > >= ==  relational operators.
Determining whether two values are the same with the == equivalence operator.
Comparing string values.
Checking that all parts of an expression are true using the logical and operator.
Determining whether two values are different using the != non-equivalence operator.
More complex decisions.
Checking that at least one part of an expression is true with the logical or operator.
Setting Boolean variables using the result of a relational expression.
Combining several if decisions together to deal with more than two outcomes.
Finding the opposite of a value or expression using the ! NOT operator.
Adding extra ( ) brackets around parts of expressions to ensure the correct result.
Using a switch statement when there are many possible outcomes for a decision.
Using the ? conditional operator to decide between two possible values.
Chapter summary.
Problems and exercises.


CHAPTER 7: REPETITION USING LOOPS
A repetitive sequence of instructions.
Repeatedly executing the same code one or more times using a do-while loop.
Using a do-while loop to make choices from a menu of options.
Repeatedly executing some code while an expression/condition is true.
A for loop that executes a statement several times.
Increasing or decreasing the value of a controlling variable.
Using the value of a for loop variable as part of a calculation.
Working directly with a range of characters in a loop.
Nesting: putting a loop within another loop.
Adding extra { } curly braces to make your code more readable.
Creating and using variables that are not local to a for statement.
Global variables.
Infinite loops – programs that go on forever without ever stopping.
Forcing a loop to terminate using break.
Counting things that happen.
Chapter summary.
Problems and exercises.


CHAPTER 8: CHARACTERS AND TEXT STRINGS
Working with single characters.
Converting between ASCII character codes and character symbols.
Arithmetic operations on char values.
Choosing character symbols at random.
Examining and processing individual characters that are part of a string value.
Finding whether a character is a numeric digit, letter of the alphabet, or other symbol.
Finding the number of characters in a string value using the length() function.
Detecting different kinds of characters in a string value.
Replacing individual characters in a string value.
Determining whether two string values are the same.
Appending – joining string values together using + or the append() function.
Detecting a pattern of characters - finding whether a string contains a sub-string.
Finding more than one occurrence of a sub‐string in a string value.
Adding text characters inside a string value using the insert() function.
Manually copying characters from one string to another.
Copying part of a string value using the substr() function.
Erasing and replacing parts of a string value.
Replacing part of a string using the replace() function.
Finding characters in a string that are part of a certain set.
Finding characters in a string that are not in a particular set.
Splitting a string value into individual tokens.
Swapping the values of two string variables around.
Chapter summary.
Problems and exercises.


CHAPTER 9: ARRAYS OF DATA
Working with several data values that have the same data-type.
What is an array?
Creating a simple array of values.
Using index numbers to refer to the values in an array.
Accessing an individual value from an array.
Using a loop to efficiently access more than one value in an array.
Going too far: attempting to use an index that is out-of-bounds.
Recording raw data from user inputs using arrays.
Categorising or counting up user inputs using arrays.
Searching through an array of data values to find what you want: a linear search.
Searching through an array of data values to find a partial match.
How to pick something at random from an array of values.
Matching related items from more than one array.
Inserting items of data into an array at a particular point.
Multi-dimensional arrays.
Creating and accessing a two-dimensional array.
Chapter summary.
Problems and exercises.


CHAPTER 10: FUNCTIONS
What are functions?
Creating a function in C++.
Function names.
Arguments – input values for a function.
Functions that do not take any arguments as input.
The body and return value of a function.
Calling a function.
How a call to a function affects the flow-of-execution within your program.
Making use of a result that gets returned from a call to a function.
A function that accepts more than one argument as input.
Functions that do not return any result: void functions.
More about function arguments – copies of values.
Functions that operate directly on their input data using & references to values.
Passing an array as an input to a function.
Deciding where to put functions in your code.
Declaring a function: telling the compiler that a function will exist later in your code.
Creating your own re-usable library of useful functions.
Inline functions.
Recursive functions: functions that call themselves.
Chapter summary.
Problems and exercises.


CHAPTER 11: FILES OF DATA
Why do we need to use files?
Using file streams to read and write data.
Using an output stream to create a file of text: ofstream.
Appending data on to the end of an existing file.
Detecting whether a file exists.
Using an input stream to read text in from a file: ifstream.
Processing data as it is read in from an input stream.
Using an array to store and process the data from a file.
Reading from files that contain columns of tab-delimited data.
Reading from files that contain comma‐separated data values.
Choosing files: typing in the filename for the file that you want to use.
Working with more than one file at the same time.
Converting between text and numeric values using stringstream.
Chapter summary.
Problems and exercises.


CHAPTER 12: STRUCTURES, POINTERS AND MEMORY
Creating structures to group data values together: struct.
Using a structure to create your own data-type.
Arrays of structures.
Static and dynamic data.
Memory locations and addresses.
References: obtaining the memory address of a data value.
Pointers.
Accessing data from a pointer using the * de-reference operator.
Null pointers.
Using pointers to create and access dynamic data stored on the heap.
Building your own data-structures.
Recycling memory once you have finished using your dynamic data.
Chapter summary.
Problems and exercises.


CHAPTER 13: OBJECTS AND CLASSES
Packaging up related data items.
Packaging data and functions together in a class.
Declaring the data and member functions that a class contains.
The difference between private and public members of a class.
Defining the body of a member function for a class.
Constructors: functions that are called when an instance of a class is created.
The need for an empty constructor function.
Creating objects dynamically on the heap.
Overloading operators.
Inheritance – creating new classes that are based on other existing classes.
Declaring your classes using .h header files.
Chapter summary.
Problems and exercises.


APPENDICES: USEFUL INFORMATION
The ASCII character set and character codes.
C++ reserved words.
Special symbols used in C++ and what they mean.
Operators and their precedence.
Data-types and ranges of values.
Header files for C++ and C library functions that you can include in your programs.
Escape-sequences to control the colour of text on the screen.
Finding and correcting common mistakes in your C++ source-code.
Stages that g++ goes through when translating C++ source-code into an executable.
Useful compiler options for g++.
Making it easier to run your executables at the command-line.
Viewing assembly language that the compiler produces.
Viewing the machine code instructions of your executable.
Useful Linux commands.
Glossary of useful terms.
How to download the software that you need (Raspbian and Ubuntu).
Useful web-links.


ANSWERS TO EXERCISES


INDEX