These commentary documents are intended to accompany your readings from the your textbook as you review C++ material. This is not a “web course”, and these commentaries are not trying to teach you the material.
They will include some notes on things that you might find strange or unintuitive if you come to C++ from Java (probably the most common case for students reading this website) or from Python.
These commentaries may also include some notes on things that, in my experience, some C++ students just find difficult in general, regardless of their background. I may also point out things that have changed in recent versions of C++.
Each section of this website includes chapter numbers from Malik’s C++ Programming: From Problem Analysis to Program Design, the textbook used for our CS150, CS250, and CS330 courses. If you are reading this website in preparation to take CS250, you might as well go ahead and get this book, as you will need it anyway.
If you prefer to wait, though, and use some other C++ text that you already have on hand, that’s OK. But then you will have to figure out the corresponding chapters. Use the list of What You Need to Know for each module to guide your search for the appropriate chapters.
1 Processing C++ Programs
All programs, whatever the language, start outs as plain text source code that a program writes using some kind of editor. The major steps involved in turning C++ source code into somethign you can execute are:
The source code is preprocessed to fetch differnet files that need to be included together.
Preprocessing modifies the text of the source code (though it does not change the original files supplied by the programmer). The result is still plain text and is still considered source code.
The preprocessed source code is compiled to produce object code.
Object code is close to executable machine code. However, most programs do not stand alone but make use of functions and other components provided by other code that will have been compiled separately.
The compiler does not know where these components reside within their own object code, so it includes a list of names of things that this code uses but that need to be located elsewhere. It also has a list of names of things that it provides and the locations where it has stored each of them.
The object code is linked to form the executable.
Linking is the process of taking multiple pieces of object code, each of which has a list of things it need and a list of things that it provides, and joining them together, so that all needed components are matched up against a component provided somewhere.
At the end of this process, you have an executable file, a file of machine code ready to load and run.
If you are a C programmer
Nothing new about this — it’s what you should be used to.
If you are a Java programmer
Java doesn’t have an equivalent to the preprocessing stage.
Compilation is very similar to C++.
The big difference is that, instead of compiling into object code that includes machine-language for the CPU you are running on, Java compiles into object code that includes machine language for an imaginary CPU called the Java Virtual Machine (JVM).
Thus, to execute a Java program, we need to fire up a simulator for this imaginary JVM machine.
Linking is done at a different time in Java than in C++. In C++, we link just after compiling and before executing.
In Java, the linker runs within the JVM simulator, and so is actually done while we are executing the Program.
If you are a Python programmer
This probably seems like overkill. You are used to simply telling the Python processor to take your source code and run it. It’s certainly simpler, but there’s price to be paid if you have large programs that are run many times in between any changes being made. Python translates the program just before running it, each time it is run. If the program is big enough for that translation to take a long time, you have to wait for it every time you run the program.
Once a C++ program is been compiled and linked, you can run it over and over without re-processing it.
2 Data Types
A data type is a collection of possible values, on which we can perform a certain set of operations.
All programming languages start with a certain set of pre-defined data types, such as characters, strings of characters, amd various kinds of numbers. Modern programming languages also allow programmers to define new data types to serve the needs of a particular program.
If you are a C programmer
The pre-defined data types in C++ have been deliberately chosen to be backwards-compatible with it’s elderly parent language, C.
The most notable change at this level is the introduction of the bool type. C programmers think of conditions like “x < y” as returning an integer, zero for false and non-zero for true. C++ instead has a true bool type with values true and false.
For backwards compatibility, C++ still allows you to treat zero/non-zero integers as equivalent to false/true, but it’s considered a bit gauche and we don’t talk about it in front of polite company.
If you are a Python programmer
One of the big conceptual changes in C++ is where types are applied.
In Python, values have type, but variables do not. Hence this code is perfectly acceptable in Python:
x = 23
⋮
➀ do some numeric operations on x
⋮
x = "hello"
⋮
➁ do some string operations on x
⋮
Data types establish what kinds of operations we can apply to a value or a variable that is holding a value.
If we know, as in ➀, that x holds an integer, then we can do “integer things” to it: add it to other integers, multiply it by other integers, etc.
If we know, as in ➁, that x holds a string, then we can do “string things” to it: ask how many characters it contains, extract characters from it, concatenate it with other strings, etc.
Trying to multiply a string by some number makes no sense. Neither does trying to extract characters from an integer. We can only apply string operations to strings, and integer operations to integers.
Trying to do otherwise is an error.
But a Python programmer can’t tell if the expressions “2 * x” is sensible unless the programmer knows where in the above sequence we’re thinking of putting it. If we put it in ➀, that’s fine. If we put it in ➁, that’s going to be a problem. But there’s nothing about x itself that tells us what kinds of data it can hold, because variables in Python can hold anything and can be switched from one kind of data to another as the program progresses.
In C++, on the other hand, both values and variables are typed.
The code
x = 23
⋮
x = "hello"
⋮
won’t be permitted by the compiler. If x has data type int, then the second assignment would be illegal. If x has the data type string, then the first assignment would have been illegal.
And that brings us to the next big shock for Pythoners: when we introduce a new variable in C++, we must declare what type of values we will be putting into it:
int x = 23; // x can only hold integers
⋮
string y = "hello"; // y can only hold strings
⋮
If you are a Java programmer
Most of what you will have read will seem familiar. int is int. double is double. char is char. No big deal.
One change to watch for, Java’s boolean is just bool in C++.
Java’s chars are 16 bits wide. C++ chars are only 8 bits.
But, all in all, this should feel pretty familiar.
C++ actually has more variations on the these of integers than Java does. Java has byte, int and long. C++ has char, short, int and long. char in C++ is actually considered an integer type, and you can do mathematical operations on it just like nay other int:
char c = 'A';
c = c + 4; // c now contains 'E'
In addition, C++ has both signed and unsigned versions of all of its integer types. An unsigned version can hold only non-negative values, but can go up to higher values on the positive side.
For example, a signed char can hold values in the range -128..127. An unsigned char can hold values in the range 0..255.
It’s the same number of possible values in each case, but the ranges are different