1. Technology

C++ Tutorial - Lesson One Learning How to Compile With Visual C++ 2005


8 of 8

Learn about C++ Comments
C++ has two ways to insert comments into source code. If you look at example 2, you can see the C++ single line comments.

Any line that starts with a // is a comment. Unlike the C type of comments that start /* and end */, the double forward slashes end with the end of line. To comment large swathes of code out, you would still use /* comments here */ as adding // to the start of each line is tedious.

Best Practices for Commenting

It is a very good idea to give variables meaningful names, not just A and B that I've used here. When you return to code you wrote a few months or years later, those variable names will get you back up to speed pronto.

If you saw this code

 float f= 128;
 float c= (f-32)*5/9; // Do conversion
 cout << f << c << endl;
You might guess it was converting fahrenheit to celsius.
 float fahrenheit= 128;
 float celsius= (fahrenheit-32)*5/9;
 cout << fahrenheit << celsius << endl;
But the second example is self explanatory.

Professional developers have to follow "house rules" for naming variables and even function names. Eg if your function does something, call it DoWhatever() (where whatever is what ever it does). If it fetches a value from a database then ReadValue or FetchValue might be appropriate. Additionally, comments should be used to document the values passed in and any information about how they should be used. Always imagine you are writing the code for someone else to read and maintain.

That completes this lesson.

Related Video
Upload Videos to Google Video

©2014 About.com. All rights reserved.