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. Every library has a name and is referred to by a header file. For example, the descriptions of the functions needed to perform input/output (I/O) are contained in the header file iostream.

Similarly, the descriptions of some very useful mathematical functions, such as power, absolute, and sine, are contained in the header file cmath. If you want to use I/O or math functions, you need to tell the computer where to find the necessary code.

You use 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. Preprocessor directives are commands supplied to the preprocessor that cause the preprocessor to modify the text of a C++ program before it is compiled. All preprocessor commands begin with #. There are no semicolons at the end of preprocessor commands because they are not C++ statements. To use a header file in a C++ program, use the preprocessor directive include. The general syntax to include a header file (provided by the IDE) in a C++ program is:

#include <headerFileName>

Note that the preprocessor commands are processed by the preprocessor before the program goes through the compiler

Namespaces

Earlier, you learned that both cin and cout are predefined identifiers. In ANSI/ISO Standard C++, these identifiers are declared in the header file iostream, but within a namespace. The name of this namespace is std. (The namespace mechanism will be formally defined and discussed in detail in Chapter 8. For now, you need to know only how to use cin and cout and, in fact, any other identifier from the header file iostream.) There are several ways you can use an identifier declared in the namespace std. One way to use cin and cout is to refer to them as std::cin and std::cout throughout the program. Another option is to include the following statement in your program: using namespace std; This statement appears after the statement #include . You can then refer to cin and cout without using the prefix std::. To simplify the use of cin and cout, this book uses the second form


tags:#programming-languages/cpp