next up previous
Next: Variables Up: Introduction to C++ Programming Previous: Exercises

A simple C++ program

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:
  1. Any text from the symbols // 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.
  2. The line
    #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.

  3. The actual program consists of the function 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.

  4. The body of the function main contains the actual code which is executed by the computer and is enclosed, as noted above, in braces {}.

  5. Every statement which instructs the computer to do something is terminated by a semi-colon. Symbols such as main(), { } etc. are not instructions to do something and hence are not followed by a semi-colon.

  6. Sequences of characters enclosed in double quotes are literal strings. Thus instructions such as
    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.

  7. All variables that are used in a program must be declared and given a type. In this case all the variables are of type 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.
  8. Values can be given to variables by the assignment statement, e.g. the statement
    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.
  9. Layout of the program is quite arbitrary, i.e. new lines, spaces etc. can be inserted wherever desired and will be ignored by the compiler. The prime aim of additional spaces, new lines, etc. is to make the program more readable. However superfluous spaces or new lines must not be inserted in words like main, cout, in variable names or in strings (unless you actually want them printed).

next up previous
Next: Variables Up: Introduction to C++ Programming Previous: Exercises
Peter JB King