Improved comments

Our first C++ program [CONCOM.CPP] includes examples of several

new things in C++. We will take the 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);

void main()

{

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 program */

int index; /* A normal C variable */

for (index = START ; index < STOP ; index++)

print_it(index);

} /* End of program */

void print_it(const int data_value)

{

cout << "The value of the index is " << data_value << "\n";

}

A comment in C++ 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++, but 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.

 

CONST and VOLATILE

There are two new keywords used which were not part of the original

Kernighan & Ritchie definition of C, but that are part of the ANSI-C

standard. The keyword const is used to define a constant. In line 6 the

constant is of type int, it is named START, and is initialised to the value

3. The compiler will not allow you to accidentally or purposefully

change the value of START because it has been declared a constant. If

you had another variable named STARTS, the system would not allow

you to slightly misspell STARTS as START and accidentally change it.

The compiler would give you an error message so you could fix the

error. Since it is not permissible to change the value of a constant, it is

imperative that you initialise it when it is declared so it will have a

useful value. The compiler does not require you to initialise it,

however, and will not issue an error message if you do not.

Note that const is also used in the function header in line 17 to indicate

that the formal parameter named data_value is a constant throughout

the function. Any attempt to assign a new value to this variable will

result in a compile error.

The keyword volatile is also part of the ANSI-C standard but was not

included in the original Kernighan & Ritchie definition of C. Even

though the value of a volatile variable can be changed by you, the

programmer, there may be another mechanism by which the value could

be changed, such as by an interrupt timer causing the value to be

incremented. The compiler needs to know that this value may be

changed by some external force when it optimises the code. Note that a

constant can also be volatile, which means that you cannot change it,

but the system can through some hardware function.

Line 3 illustrates prototyping and the modern method of function

definition as defined by the ANSI-C standard, which will be discussed

in greater detail in a later section of the Module. Prototyping is

absolutely required in C++; programs without prototyping will generate

linker errors.

 

The Scope Operator

The next program [SCOPEOP.CPP] illustrates another construct that is

unique to C++; there is no corresponding construct in either Kernighan

& Ritchie or ANSI-C. The scope operator allows access to the global

variable named index even though there is a local variable of the same

name within the main function. The use of the double colon in front of

the variable name (in lines 7, 8 and 10) instructs the system that we are

interested in using the global variable named index, defined in line 2,

rather than the local variable defined in line 5.

#include <iostream.h>

int index = 13;

void main()

{

float index = 3.1415;

cout << "The local index value is " << index << "\n";

cout << "The global index value is " << ::index << "\n";

::index = index + 7; // 3 + 7 should result in 10

cout << "The local index value is " << index << "\n";

cout << "The global index value is " << ::index << "\n";

}

The use of this technique allows access to the global variable for any

use. It could be used in calculations, as a function parameter, or for any

other purpose. It is not really good programming practice to abuse this

construct, because it could make the code difficult to read. It would be

best to use a different variable name instead of reusing this name, but

the construct is available to you if you find that you need it sometime.

The scope operator allows access to global variables even though

hidden by a local variable.

 

 I/O Stream Library.

 

Links:  Home C Programming Guide  C++ programming Guide

Contact Me