10.3 include Files

I l @ ve RuBoard

10.3 #include Files

The #include directive allows the program to use source code from another file.

For example, you have been using the following directive in your programs:

 #include <iostream> 

This tells the preprocessor to take the file iostream and insert it in the current program. Files that are included in other programs are called header files . (Most #include directives come at the head of a program.) The angle brackets indicate that the file is a standard header file. In Unix, these files are usually located in /usr/include . In MS-DOS/Windows, they are located in an installation-dependent directory.

Standard include files are used for defining data structures and macros used by library routines. For example, std::cout is a standard object that (as you know by now) prints data on the standard output. The std:: ostream class definition used by std::cout and its related routines is defined in iostream . [1]

[1] Actually, the ostream class is defined in the header ostream . However, this file is included by the iostream header. The result is that by including this single header, you get the definitions of standard objects such as std::cin and standard classes such as std::ostream .

Sometimes you may want to write your own set of include files. Local include files are particularly useful for storing constants and data structures when a program spans several files, which can be helpful for information sharing when a team of programmers is working on a single project. (See Chapter 23.)

Local include files may be specified by using double quotation marks (") around the filename.

 #include "defs.h" 

The filename (" defs.h ") can be any valid filename. By convention, local C++ headers end in .h . The file specified by the #include can be a simple file, " defs.h "; a relative path , " ../../data.h "; or an absolute path, " /root/include/const.h ". (In MS-DOS/Windows, you should use backslash (\) instead of slash (/) as a directory separator. For some reason though, you can still use slash (/) and things work.)

Include files may be nested, but this can cause problems. Suppose you define several useful constants in the file const.h . If the files data.h and io.h both include const.h, and you put the following in your program:

 #include "data.h"  #include "io.h" 

you generate errors because the preprocessor sets the definitions in const.h twice. Defining a constant twice is not a fatal error; however, defining a data structure or union twice is an error and must be avoided.

One way around this problem is to have const.h check to see whether it has already been included and not define any symbols that have already been defined.

Look at the following code:

 #ifndef _CONST_H_INCLUDED_  /* Define constants */  #define _CONST_H_INCLUDED_  #endif  /* _CONST_H_INCLUDED_ */ 

When const.h is included, it defines the symbol _CONST_H_INCLUDED_ . If that symbol is already defined (because the file was included earlier), the #ifdef conditional hides all the other defines so they don't cause trouble.

It is possible to put code in a header file, but this is considered poor programming practice. By convention, code goes in .cpp files and definitions, declarations, macros, and inline functions go in the .h files. You could include a .cpp file in another .cpp file, but this is considered bad practice.

I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net