Why Use a Module?

Modules let you compile pieces of your program separately. This is important for two reasons:

  1. When the program gets big, you only have to recompile the pieces you change; this saves time.

  2. You can share your modules with other programmers and they can leverage the code you've created in the same way that you use the iostream library.

What Is in a Header File?

Header files tell the compiler what is in the implementation module, such as the names and arguments of functions, and definitions of constants and data types.

The functions named in the header file are public, because any user of the module can see them. The implementation file, however, can contain functions not mentioned in the header file that cannot be used by anything other than the module implementation. Such functions are referred to as private.

Making a Header File

It's time to split a few functions from the example into separate modules. Let's start with the module that will hold the PauseForUserAcknowledgement() function—PromptModule.h (see Listing 8.1).

Listing 8.1. Header for PromptModule
1: #ifndef PromptModuleH
2: #define PromptModuleH
3:
4: void PauseForUserAcknowledgement(void);
5:
6: #endif

Line 1 contains a special preprocessor instruction. It means “if not defined.” When the name that follows #ifndef (that is, PromptModuleH) is not yet defined in the program, the lines from here to #endif will be passed on to the compiler.


Line 2 is what makes line 1 useful. With this, the symbol PromptModuleH is defined. If the header were included a second time in the same program, line 1 would cause the preprocessor to check its list of defined symbols, and it would see that PromptModuleH has been defined. Therefore, the code between line 1 and line 6 would not be passed to the compiler a second time.

(Note that lines 1, 2, and 6 never go to the compiler. They are “eaten” by the preprocessor.)

Why are lines 1, 2, and 6 so important? Imagine two library modules that use another library module such as iostream. Imagine if both of those modules were included in main.cpp. The header would be duplicated and the compiler would see two occurrences of the declaration of cout and would be unable to decide between them. Use #ifndef, #define, and #endif in every header you write.

Let's move to line 4, the core of the header file. This is a function prototype, which was discussed in the previous lesson. You can identify it as a prototype because it has no body and ends with a semicolon. It provides enough information for the compiler to ensure that a call to this function has been correctly set up with the right types for the arguments and the expected return value.

What Does an Implementation File Look Like?

Implementation files are a lot like main.cpp, except that they include their own header file as well as any others they need.

Standard C++ requires that there be something in an implementation file. There doesn't have to be anything more than a #include of its header file, but it usually contains substantial code.

Listing 8.2 is the implementation for the PromptModule.h.

Listing 8.2. Implementation for PromptModule
 1: #include <iostream>
 2:
*3: #include "PromptModule.h"
 4:
 5: using namespace std;
 6:
 7: void PauseForUserAcknowledgement(void)
 8: {
 9:    // Note: You must type something before Enter
10:    char StopCharacter;
11:    cout << endl << "Press a key and "Enter": ";
12:    cin >> StopCharacter;
13: }

Line 3 includes iostream and the module header file, line 5 uses the std namespace, and the function itself is identical to the one that was in main.cpp.


You use a different form for the #include of the PromptModule.h than you used for iostream. This is because iostream is a standard include file in a standard location, while PromptModule.h is in the current directory. The double quotes tell the preprocessor to look for a header in the current directory, and, if it is not found there, to look in any other directories provided to the compiler through its command line (see your compiler documentation for how to indicate additional directories to be searched). The .h extension must be specified at the end of the header filename in this form.

As you can see, you really don't need to change much to use separate compilation. But you might want to alter some names, just to be safe.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.156.122