OOPortal OOPortal
Programming C++  «Prev 

C++ Comment style

Except for lengthy multi-line comments, the rest-of-line comment should be used; it is easier to use and less error prone. Comments themselves should be terse but written in clear prose style. Where possible, they should be aligned.
Excessive commenting, however, is distracting. Good choice of identifier names and clear use of structured flow of control do not need redundant comments such as "weight stores the weight of a person," or "this is a for loop."

/* C comment style */
// C++ comment style

Do not say in comments what can be clearly stated in code.

The central language feature of C++ is the class. A class is a user-defined type provided to represent a concept in the code of a program. Whenever our design for a program has a useful concept, idea or entity we try to represent it as a class in the program so that the idea is there in the code, rather than just in our head, in a design document, or in some comments. A program built out of a well chosen set of classes is far easier to understand and get right than one that builds everything directly in terms of the built-in types. In particular, classes are often what libraries offer.

Comments and Whitespace

You add comments that document your program code to make it easier for someone else to understand how it works. The compiler ignores everything that follows two successive forward slashes on a line so this kind of comment can follow code on a line. The first line is a comment that indicates the name of the file containing this code. This file is in the code download for the book. I will identify the file for each working example in the same way. The file extension, .cpp, indicates that this is a C++ source file.
Other extensions such as .cc are also used to identify a C++ source file. All the executable code for a program will be in one or more source files.
  1. an often reuse a function in many different programs, thus saving time and effort.
  2. Large programs are typically developed by a team of programmers. Each team member is responsible for programming a set of functions that are a well-defined subset of the whole program. Without a functional structure, this would be impractical.