CH 02

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

2.

Editing, Compiling and Executing a Simple Program

A simple C++ program

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.

// First Program – Welcoming message

#include <iostream>

using namespace std;

int main()

cout << "Hello there, I’m a C++ program!\n";

cout << "Welcome to the OOP world.";

return 0;

Overview of a C++ program

Every C++ program consists of a header and a main body and has the following structure.

// Comment statements which are ignored by computer but inform reader

#include < header file name>

void main()

/* A second type of comment

on more than one line

... ignored by the compiler */

main body statements;

Line 1: // First Program – Welcoming message


At the top of the program there’s a comment which will not be executed by the computer. On any line in
a program containing // indicate that the rest of the line (beginning with //) is a comment. Another form
of inserting comments in a C++ program is using the opening /* and closing */ symbols, possibly on
multiple lines. Comments are inserted by programmers to help people read and understand the
program. Here the comment states the purpose of the program.

Line 2: #include <iostream>

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).

Line 3: using namespace std;

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.

Line 5: int main()

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.

Line 7 & 8: cout << "Hello there, I’m a C++ program!\n";

cout << "Welcome to the OOP world.";

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.

The development cycle

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.

Figure 1 Compilation of a C++ Program

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.

You might also like