CH 02
CH 02
CH 02
The following is an example of a simple, short but complete program (source code) written in the C++
programming language. The program displays some message on the screen.
#include <iostream>
int main()
return 0;
Every C++ program consists of a header and a main body and has the following structure.
void main()
Lines beginning with # are instructions to the compiler's preprocessor. The include instruction says
"what follows is a file name, find that file and insert its contents right here". It is used to include the
contents of a file of definitions which will be used in the program. Here the file iostream contains the
definitions of some of the symbols used later in the program (e.g. cin, cout).
This is an advanced feature of C++. It is used to specify that names used in the program (such as cout)
are defined in the standard C and C++ libraries. This is used to avoid problems with other libraries which
may also use these names.
This defines a function called main. When a program is run, the instructions will be executed in the
order they appear in the main body of the program. The main body is delimited by main() and the
opening and closing braces (curly brackets). The int at the beginning of this line specifies that main() will
return an integer value on its completion.
Line 6: {
The opening (left) brace marks the beginning of the main body of the program. The main body consists
of instructions which defining the data or statements on how the data should be processed. All C++
declarations and statements must end with a semicolon.
These statements instruct the computer to display the string of characters within the quotation marks.
Line 9: }
The closing (right) brace marks the end of the main body of the program.
Blank lines
Blank lines (like in Lines 4) could be introduced to make the program more readable. They will be
ignored by the compiler. Whitespace (spaces, tabs and newlines) are also ignored (unless they are part
of a string of characters contained between quotation marks). They too can be used to enhance the
visual appearance of a program.
Indentation
It does not matter where you place statements, either on the same line or on separate lines. A common
and accepted style is that you indent after each opening brace and move back at each closing brace.
C++ programs go through 3 main phases during development: editing (writing the program), compiling
(i.e. translating the program to executable code and detecting syntax errors) and running the program
and checking for logical errors (called debugging).
i. Edit
The first phase consists of editing a file by typing in the C++ program with a text editor and making
corrections if necessary. The program is stored as a text file on the disk, usually with the file extension
.cc, .cpp, .C or .cxx to indicate that it is a C++ program (e.g. FirstProgram.cc ).
ii. Compile
A compiler translates the C++ program into machine language code (object code) which it stores on the
disk as a file with the extension .o ( e.g. FirstProgram.o). A linker then links the object code with
standard library routines that the program may use and creates an executable image which is also saved
on disk, usually as a file with the file name extension .exe (e.g. FirstProgram.exe), see Figure 1.
iii. Execute
The executable code is loaded from the disk to memory and the computer's processing unit (Central
Processing Unit) executes the program one instruction at a time.