This course is taught using the C++ programming language. C++ is an extension of an earlier language, C, and for the most part, we will use the C subset of C++ in this course because it provides the tools that we need to explore physical data structures. A few of the language features that we will use are part of C++ but not of C. These notes make no attempt to offer a complete introduction to C++.
Java was designed to share many of its basic features with C++. The original intent was to make Java easy for C++ programmers to learn. But it also works the other way. If you already know Java, you will see familiar things in C++.
Even though Java was designed to look like C++, Java and C++ are different languages. C++ is much closer to the actual computer. Java is based on an earlier language, Smalltalk and, as a rough approximation, Java is Smalltalk with C++ syntax. Even though much of a Java program looks like C++, under the hood, it is much more like Smalltalk.
Like all programming languages, C++ has a rigid form. Only use features that you have learned. Do not try to make up the language as you go and hope that your guesses are right. Guessing will take you into the swamp.
C++ is a free-form language. That means
in most places, an end-of-line is treated like a blank;
in most places, any sequence of blanks, tabs or end-of-line characters is treated the same as a single blank.
Notice that the free-form rule means you do not need to put a long statement all on one line. The course standards require you to limit lines to about 80 characters. But indent to show that some lines belong to statements started in previous lines. For example, we will see printf later, which prints items under control of a format. Here is a statement that prints some information.
printf("This graph has %d vertices and %d edges\n", numberOfVertices, numberOfEdges);
Notice the color. Anything shown in that color is C++.
The free-form rule does not apply to string constants, such as "blanks matter here". A string constant must be entirely on one line. If you want to write a multiline string constant, write \n for a line break.
Comments in C++ have the same form as comments in Java. They are ignored by the compiler.
All text from // to the end of a line is a comment. (This is another place where the free-form rule does not apply.)
Text between /* and */ is a comment. That allows you to write a multi-line comment without beginning each line with //.
Do not embed one comment inside another. For example, do not write /* Here is an /* embedded comment */ */. The compiler will end the entire comment at the first occurrence of */.
Some students have been taught to use margin comments, such as the following.
x = x + 1; // add 1 to x
Silly comments like that just clutter the program. Don't explain C++ to the reader. Margin comments tend to make lines long, and that is clumsy. You are better off writing a paragraph of comment followed by a paragraph of comment-free code.
There is one more feature of C++ where the free-form rule is violated. A C++ compiler goes through two stages: the preprocessor and the compiler-proper.
C++ program | → | preprocessor | → | compiler-proper | → | compiled program |
The preprocessor makes some changes to the program. For example, the preprocessor replaces line
#include <cstdio>
by the entire contents of the standard system file cstdio.
Any line that starts with # is a preprocessor directives, and it is removed by the preprocessor. The preprocessor is line-oriented, not free-form; a preprocessor directive ends at the end of a line.
Why does C++ look a lot like Java? Answer
What does it mean when we say that C++ is free-form? Answer
#include <cmath>do? Answer