Sams Teach Yourself C++ in 21 Days

(singke) #1
LISTING2.5 HELP.cppDemonstrates Comments
1: #include <iostream>
2:
3: int main()
4: {
5: using std::cout;
6:
7: /* this is a comment
8: and it extends until the closing
9: star-slash comment mark */
10: cout << “Hello World!\n”;
11: // this comment ends at the end of the line
12: cout << “That comment ended!\n”;
13:
14: // double-slash comments can be alone on a line
15: /* as can slash-star comments */
16: return 0;
17: }

Hello World!
That comment ended!
The comment on lines 7–9 is completely ignored by the compiler, as are the
comments on lines 11, 14, and 15. The comment on line 11 ended with the end
of the line. The comments on lines 7 and 15 required a closing comment mark.

OUTPUT


34 Day 2


ANALYSIS

There is a third style of comment that is supported by some C++ compilers.
These comments are referred to as document comments and are indicated
using three forward slashes (///). The compilers that support this style of
comment allow you to generate documentation about the program from
these comments. Because these are not currently a part of the C++ standard,
they are not covered here.

NOTE

A Final Word of Caution About Comments ....................................................


Comments that state the obvious are less than useful. In fact, they can be counterproduc-
tive because the code might change and the programmer might neglect to update the
comment. What is obvious to one person might be obscure to another, however, so judg-
ment is required when adding comments.
The bottom line is that comments should not say whatis happening, they should say why
it is happening.
Free download pdf