Comments are simply text ignored by
the compiler. They help to inform the reader of what you are doing at
any particular point in your program.
Including comments in your code helps
to clarify your intentions. Believe me, comments are essential.
There are two ways of including comments
in C++. Using // and using
/*...*/
- The double-slash comment //
is used to write a comment on a single line.
- The slash-star comment /*
tells the compiler to ignore any text until it finds the opposite slash-star
*/. You must match every /*
with it's pair */
Many C++ programmers use the double-slash
style comment most of the time and reserve slash-star style comments
for ignoring large sections of a program.
As a general rule, your program should
have comments at the beginning, explaining what the program does. Each
function should also have comments explaining what it does and what values
it returns. Finally, any statements in your program that are obscure
or less than obvious should be commented as well. You don't have to write
comments on every line, especially if the line is obviously understandable.
Here is an example of commented code:-
/************************************************************
Program: Hello World
Files: HelloWorld.cpp
Author: ADR
Environment: Visual C++ 6 Enterprise, Windows
XP
Notes: Contracted by Southwark College
Revisions: 1.00 07/08/03 First release
************************************************************/
#include <iostream.h>
int main()
{
/*this is a comment to describe the purpose of the function. It
extends until the closing slash-star */
cout << "Hello World!\n";
//this is a comment at the end of a line
return 0; //This comment
line is unnecessary - the code is obvious
}
|
You will be expected to
add comments to your own programs.