|
|
|
|
|
|
|
To fight the onset of confusion and to help others to understand your code, you'll want to use comments. Comments are simply text that is ignored by the compiler, but that may inform the reader of what you are doing at any particular point in your program. |
|
|
|
|
|
|
|
|
New Term: There are two types of comments in C++. The double-slash (//) comment, which will be referred to as a C++-style comment, tells the compiler to ignore everything that follows the slashes until the end of the line. |
|
|
|
|
|
|
|
|
The slash-star (/*) comment mark tells the compiler to ignore everything that follows until it finds a star-slash (*/) comment mark. These marks will be referred to as C-style comments because C++ inherited them from C. Remember, every /* must be matched with a closing */. |
|
|
|
|
|
|
|
|
Many C++ programmers use the C++-style comment most of the time, and reserve C-style comments for blocking out large blocks of a program. You can include C++-style comments within a block commented out by C-style comments; everything, including the C++-style comments, is ignored between the C-style comment marks. |
|
|
|
|
|
|
|
|
Comments are free; they don't cost anything in performance, and they are ignored by the compiler. Listing 2.2 illustrates this. |
|
|
|
|
|
|
|
|
LISTING 2.2HELLO.CPP DEMONSTRATES COMMENTS |
|
|
|
 |
|
|
|
|
1: #include <iostream.h>
2:
3: int main()
4: {
5: /* this is a comment
6: and it extends until the closing
7: star-slash comment mark */
8: cout << Hello World!\n;
9: // this comment ends at the end of the line
10: cout << That comment ended!;
11:
12: // double slash comments can be alone on a line
13: /* as can slash-star comments */
14: return 0;
15: } |
|
|
|
|
|
|
|
|
Hello World!
That comment ended! |
|
|
|
|
|
|
|
|
Analysis: The comments on lines 5 through 7 are completely ignored by the compiler, as are the comments on lines 9, 12, and 13. The comment on line 9 ends with the end of the line; however, the comments on 5 and 13 require a closing comment mark. |
|
|
|
|
|