Basic Elements of C++ (III)



Basic Elements of C++ (III)

Preprocessor Directives

• Only a small number of operations, such as arithmetic and assignment operations, are explicitly defined in C++.

• Many of the functions and symbols needed to run a C++ program are provided as a collection of libraries. For example, in order to perform input/output, the header file iostream must be included.

• Preprocessor directives and the names of header files to tell the computer the locations of the code provided in libraries.

• Preprocessor directives are processed by a program called a preprocessor.

• All preprocessor commands begin with #. There are no semicolons at the end of the preprocessor commands and they appear as the first line of a program.

• The general syntax is # include

• The preprocessor commands are processed by the preprocessor before the program goes through the compiler.

Commonly used header files are

| |input/output |

| |math functions |

| |string functions |

| |formatting manipulations for input/output |

Creating a C++ Program

• Using the proper structure for a C++ program makes it easier to understand and subsequently modify the program.

• Every C++ program is divided into two parts: the preprocessor directives and the program. Taken together, the preprocessor directives and program statements constitute the C++ source code.

• To be useful, this source code must be saved in a file that has the file extension .cpp.

• Compiler generates the object code

− Saved in a file with file extension .obj

• Executable code is produced and saved in a file with the file extension .exe.

Program Style and Form

• Every C++ program has a function main

• Basic parts of function main are:

− The heading

− The body of the function

1) The heading part

The heading part has the following form:

typeOfFunction main(argument list)

2) The body of the function

➢ The body of the function is enclosed between { and }

➢ Has two types of statements

• Declaration statements

✓ int a, b, c;

✓ double x, y;

• Executable statements. Three forms:

✓ a = 4; //assignment statement

✓ cin>>b; //input statement

✓ cout ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download