As we begin the study of C++ and object oriented programming, a few commentsare in order to help you get started. Since the field of object oriented programming is probably new to you, you will find that there is a significant amount of new terminology for you to grasp. This is true of any new endeavor and you should
be warned not to be intimidated by all of the new concepts.
We will add a few new topics in each tutorial and you will slowly grasp the entire language.
Comments in C++
Examine the file named CONCOM.CPP for an example of several new things in C++. We will take the CONCOM.CPP new constructs one at a time beginning with the comments.
#include <iostream.h> /* This is the stream definition file */
void print_it(const int data_value);
const int START = 3; // The value of START cannot be changed
const int STOP = 9; // The value of STOP cannot be changed
volatile int CENTER = 6; /* The value of CENTER may be changed
by something external to this
int index; /* A normal C variable */
for (index = START ; index < STOP ; index++)
} /* End of program */
void print_it(const int data_value)
cout << "The value of the index is " << data_value << "\n";
// Result of execution
// The value of the index is 3
// The value of the index is 4
// The value of the index is 5
// The value of the index is 6
// The value of the index is 7
// The value of the index is 8
A comment begins with the double slash “//”, starts anywhere on a line, and runs to the end of that line where it is automatically terminated. The old method of comment definition used with ANSI- C can also be used with C++ as illustrated in lines 11 through 14, among other places in this program. The new method is the preferred method of comment definition because it is impossible to inadvertently comment out several lines of code. This can be done by forgetting to include the end of comment notation when using the older C method of comment notation. Good programming practice would be to use the new method for all comments and reserve the old method for use in commenting out a section of code during debugging since the two methods can be nested.
It would be well to caution you at this point however, that you should not use comments when the same sense of program definition can be obtained by using meaningful names for variables, constants, and functions. The careful selection of variable and function names can make nearly any code self documenting and you should strive to achieve this in your code.