Next: Variables
Up: Introduction to C++ Programming
Previous: Exercises
Before looking at how to write C++ programs consider the following simple example program.
// Sample program // IEA September 1995 // Reads values for the length and width of a rectangle // and returns the perimeter and area of the rectangle. #include <iostream.h> void main() { int length, width; int perimeter, area; // declarations cout << "Length = "; // prompt user cin >> length; // enter length cout << "Width = "; // prompt user cin >> width; // input width perimeter = 2*(length+width); // compute perimeter area = length*width; // compute area cout << endl << "Perimeter is " << perimeter; cout << endl << "Area is " << area << endl; // output results } // end of main program
Download program.The following points should be noted in the above program:
//
until the end of the line is
ignored by the compiler. This facility allows the programmer to
insert Comments in the program. Every program should at
least have a comment indicating the programmer's name, when it was
written and what the program actually does. Any program that is not
very simple should also have further comments indicating the major
steps carried out and explaining any particularly complex piece of
programming. This is essential if the program has to be amended or
corrected at a later date.#include <iostream.h>must start in column one. It causes the compiler to include the text of the named file (in this case
iostream.h
) in the program at
this point. The file iostream.h
is a system supplied file
which has definitions in it which are required if the program is going
to use stream input or output. All your programs will include this
file. This statement is a
compiler directive -- that is it gives information to the
compiler but does not cause any executable code to be produced.
main
which
commences at the line
void main()
All programs must have a function main
. Note that the opening
brace ({
) marks the beginning of the body of the function,
while the closing brace (}
) indicates the end of the body of
the function. The word void
indicates that main
does
not return a value. Running the program consists of obeying the
statements in the body of the function main
.
main
contains the actual code
which is executed by the computer and is enclosed, as noted
above, in braces {}
.
main()
,
{
}
etc. are not instructions to do something and
hence are not followed by a semi-colon.
cout << "Length = "
send the quoted characters to the output stream cout
. The
special identifier endl
when sent to an output stream will
cause a newline to be taken on output.
int
, i.e. whole numbers. Thus the statement
int length, width;
declares to the compiler that integer variables length
and
width
are going to be used by the program. The compiler
reserves space in memory for these variables.
area = length*width;
evaluates the expression on the right-hand side of the equals sign
using the current values of length
and width
and
assigns the resulting value to the variable area
.main
, cout
, in variable names or in strings
(unless you actually want them printed).