|
|
|
|
|
|
|
C++ comments come in two forms. The first is any sequence of characters enclosed by the /* */ pair. The compiler ignores anything within the pair. Here's an example: |
|
|
|
|
|
|
|
|
float fuelLoad; /* The amount of fuel, entered in pounds */ |
|
|
|
|
|
|
|
|
The second, and more common, form begins with two slashes (//) and extends to the end of that line of the program: |
|
|
|
|
|
|
|
|
float fuelLoad; // The amount of fuel, entered in pounds |
|
|
|
|
|
|
|
|
The compiler ignores anything after the two slashes. |
|
|
|
|
|
|
|
|
It is good programming style to write fully commented programs. A comment should appear at the beginning of a program to explain what the program does: |
|
|
|
|
|
|
|
|
// This program computes the weight and balance of a Beechcraft
// Starship-1 airplane, given the amount of fuel, number of
// passengers, and weight of luggage in fore and aft storage.
// It assumes that there are two pilots and a standard complement
// of equipment, and that passengers weigh 170 pounds each |
|
|
|
|
|
|
|
|
Another good place for comments is in constant and variable declarations, where the comments explain how each identifier is used. In addition, comments should introduce each major step in a long program and should explain anything that is unusual or difficult to read (for example, a lengthy formula). |
|
|
|
|
|
|
|
|
It is important to make your comments concise and to arrange them in the program so that they are easy to see and it is clear what they refer to. If comments are too long or crowd the statements of the program, they make the program more difficult to readjust the opposite of what you intended! |
|
|
|
|
|
|
|
|
We have looked at basic elements of C++ programs: identifiers, declarations, variables, constants, expressions, statements, and comments. Now let's see how to collect these elements into a program. As you saw earlier, C++ programs are made up of functions, one of which must be named main. A program also can have declarations that lie outside of any function. The syntax template for a program looks like this: |
|
|
|
|
|