Académique Documents
Professionnel Documents
Culture Documents
C++ is a very popular structured programming language. It has evolved from the C
programming language. Programs can be split into modules, each comprising its own code, and
are linked together by the main function - main().
In the first phase, the program’s source code (a series of statements used to instruct the computer
to perform a particular task) is created. The following line of source code instructs the computer
to print Hello World! on the screen:
The source code is typed and manipulated in a built-in program provided with C++, referred to
as the editor (displayed below).
In the second phase, a preprocessor program processes the code by including the C++ header
files in the source code to be compiled.
In the third phase, the source code is compiled (by a program called a compiler), resulting in the
source code being converted to machine language (object code). The Ctrl + F9 keys are used to
compile a program.
In the fourth phase (linking), the compiled source code is combined with the object code from
the function library to produce an executable image. This process is performed by a program
called the linker. The Ctrl + F10 keys are used to link a program.
CaFSET (Antigua) Office Workbook. Written by Richard and Jessie Lewis © 2009 by CaFSET (Antigua) 287
Introduction to Programming Lesson 3: Introduction to C++ Programming
In the fifth phase (called loading), the executable image is taken from the disk and transfers it to
memory. This task is performed by the loader.
In the final phase, the computer executes the program one instruction at a time.
A compiler translates the source code into a form that you can run at a later date. Programs that
are produced by a compiler run very fast. The compiler does not have to be loaded on your
computer in order for the executable program to run.
A variable name can contain letters of the alphabet, decimal digits, and the underscore character
(_) but must begin with a letter. C++ is case-sensitive, therefore the variable name Gender is
different from the variable gender. C++ has a number of Reserved words (words that are
reserved for, or are a part of, the language), which cannot be used as variable names. For
example, main and int. It is useful to use variable names which give an indication of the data
they represent.
Variable Scope: A local variable is one defined within the body of a function or module
and cannot be “seen” outside that module. A global variable is one
defined outside the body of the program modules and can be “seen” by all
modules within the program.
Header Files
#include <iostream.h>
This header file contains information and declarations used by the compiler when compiling
standard input/output library functions such as cout and cin. This header file must be in every
C++ program.
#include <string.h>
This header file contains prototypes for string processing functions.
288 CaFSET (Antigua) Office Workbook. Written by Richard and Jessie Lewis © 2009 by CaFSET (Antigua)
Introduction to Programming Lesson 3: Introduction to C++ Programming
cin
The cin object is responsible for input. The overloaded extraction operator >> is used to put data
in your program variables. Let us examine the following cin syntax:
int x;
cout << “Enter a number: ”
cin >> x;
The int x indicates that the data input by the user should be an integer named x. The statement
cin >> x is storing the number in the variable x. If the user enters 6, that value is stored in x.
endl
endl (pronounced end-ell) is short for end line. endl writes a new line to screen.
return 0
The statement return 0 passes control (by passing the value 0) to the operating system
environment in which the program is being executed. This indicates to the operating system that
the program executed successfully. This statement is usually the last statement in main().
Functions in C++
Main ()
Every C++ program has a main() function. Whenever a C++ program is executed, the statements
in main() are executed.
Other functions
A function in C++ must have a name relating to its task. A function to add two numbers would
probably be given the name add(). A { is used to begin the function and a } to end it. Functions
may have arguments (a data type used as an input to a function) and return a value.
Functions must be declared and later defined. The function to add two variables can be declared
as: int add(int a, int b);
CaFSET (Antigua) Office Workbook. Written by Richard and Jessie Lewis © 2009 by CaFSET (Antigua) 289