Vous êtes sur la page 1sur 3

Introduction to Programming Lesson 3: Introduction to C++ Programming

LESSON 3: INTRODUCTION TO C++ PROGRAMMING

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

The Programming Phases


A C++ program typically goes through six phases: editing, preprocessing, compiling, linking,
loading, and executing.

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:

e.g., cout << “Hello World!\n”;

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.

Interpreter versus Compiler


An interpreter is a program that reads a high-level language program statement, determines the
operation to be performed, and executes the operation immediately. An interpreter, though
easier to work with (than a compiler), must be loaded on your computer in order for the program
to run.

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.

Variables and Data Declarations


A variable represents a data storage location in the computer's memory in which a value can be
stored. Every reference to that name is actually a reference to the contents of that memory
location. All variables being used in a program must be declared. A variable must be declared
as a specific data type. A variable declaration is the process of setting aside memory for the
variable. The data types of concern to us, with sample declarations are:

int (for whole numbers) e.g. int age;


float (for floating-point numbers – numbers with an integer portion and a fractional
portion) e.g. float salary;
char (for a single character, such as M or F for Gender) e.g. char gender;

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

The Output and Input Objects


cout
The cout object is used to print a value to the screen. The insertion operator << is used to
redirect the output to screen. For example, to display This is my first program!, you code:

cout << “This is my first program!”;

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

The function may be defined as:


int add(int a, int b)
{
int sum;
sum = a + b;
return sum;
}

CaFSET (Antigua) Office Workbook. Written by Richard and Jessie Lewis © 2009 by CaFSET (Antigua) 289

Vous aimerez peut-être aussi