Multiple files in Programs with Structures


It is recommended that the definition of structures used in a program (as with classes that will be discussed later) be placed in their own header file. For example the program: studentgrades.cpp has a structure called: Quizzes. Notice that studentgrades2.cpp is the same program except that the definition of the structure: Quizzes is placed into the header: quizzes.h which is included into the program. In viewing the header: quizzes.h, notice the following preprocessor code:

image from book

 #ifndef QUIZZES #define QUIZZES ..... #endif 

image from book

In large programs it may be possible that a header like quizzes.h could be included in several different source files of the project. This will cause problems if the compiler attempts to define the structure more than once. The preprocessor code above tells the compiler to define the structure Quizzes if and only if the word: QUIZZES has never been defined before.

Create an empty console project called: studentgrades2 and copy each of these two files into the project as a source file and as a header file respectively. Compile and execute the program. Notice that this program works the same as studentgrades.cpp did previously. Although there are several functions in the program in addition to main(), a header file was not created for them as in an earlier example on header files. A separate header file for these functions could have been done if the functions could be used in other programs. In this case, they could have been placed in an additional header for example: functions.h.

When structures have member functions (as with classes that will be demonstrated later), the functions that belong to the structure are frequently placed into their own source file. In the example: account.cpp discussed above, the program has a structure: Account and the structure has member functions. The program: account2.cpp contains an include for the header: theAccount.h. In addition to these two files the project must also have the source file: theAccount_h.cpp. Notice that at the top of the file: theAccount_h.cpp the following line:

image from book

 #include "theAccount.h" 

image from book

The #include is required so that the member functions have access to the structure's definition in the file theAccount.h.

Create an empty console project: account2. Copy both account2.cpp and theAccount_h.cpp as source files into the project. Next copy theAccount.h into the project as a header file. Compile and executed the program. Notice that account2.cpp works the same as the program account.cpp that was discussed above. For programs that have more than one structure, each of the structures should have their own header file and the member functions for each structure should be stored into their own source file as well. Don't forget to have a #include for the structure's header file in the member function's file as well as in the main program file.

Here is another example of a program that has a header file: thedate.h that contains the definition of a structure: date which has member functions as well as data members. This header file is called by the program: theclock.cpp. This program simulates a real clock. It uses the values: __DATE__ and __TIME__ that time stamps the program when it compiles. If you run this program at a different time without recompiling, then the date and time are not current.

 Note:  putch() and kbhit() had to be changed to _putch() and _kbhit() in this example as well in Visual Studio .NET 2005 because of security reasons. This was done is other examples as well.




Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
EAN: N/A
Year: 2007
Pages: 142

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