< previous page page_351 next page >

Page 351
Finally, on line 23, the program tests for WINDOWS_VERSION. Because you did not define WINDOWS_VERSION, the test fails and the message on line 26 is printed.
Inclusion and Inclusion Guards
You will create projects with many different files. You will probably organize your directories so that each class has its own header file (.HPP) with the class declaration, and its own implementation file (.CPP) with the source code for the class methods.
Your main() function will be in its own .CPP file, and all the .CPP files will be compiled into .OBJ files, which will then be linked together into a single program by the linker.
Because your programs will use methods from many classes, many header files will be included in each file. Also, header files often need to include one another. For example, the header file for a derived class's declaration must include the header file for its base class.
Imagine that the Animal class is declared in the file ANIMAL.HPP. The Dog class, which derives from Animal, must include the file ANIMAL.HPP in DOG.HPP, or Dog will not be able to derive from Animal. The Cat header also includes ANIMAL.HPP for the same reason. If you create a method that uses both a Cat and a Dog, you will be in danger of including ANIMAL.HPP twice.
This will generate a compile-time error, because it is not legal to declare a class (Animal) twice, even though the declarations are identical. You can solve this problem with inclusion guards. At the top of your ANIMAL header file, you will write these lines:
#ifndef ANIMAL_HPP
#define ANIMAL_HPP
                    // the whole file goes here
#endif
This says, If you haven't defined the term ANIMAL_HPP, go ahead and define it now. Between the #define statement and the closing #endif are the entire contents of the file.
The first time your program includes this file, it reads the first line, and the test evaluates to true; that is, you have not yet defined ANIMAL_HPP. Thus, this file goes ahead and defines ANIMAL_HPP and then includes the entire file.
The second time your program includes the ANIMAL.HPP file, it reads the first line, and the test evaluates false; ANIMAL_HPP has been defined. It therefore skips to the next #else (there isn't one) or the next #endif (at the end of the file). Thus, it skips the entire contents of the file, and the class is not declared twice.

 
< previous page page_351 next page >

If you like this book, buy it!