C++  .NET  C#  Java  C  ASP  HTML  Prolog  Visual Basic  Javascript  JScript  VBScript  MFC  Perl  PHP  Basic  Python  C  Assembler  VB  Ada  Ruby  XML  ASP.NET  C++  Java
[warebiz] :: Programming Bizness

Specializing in programming tutorials that make sense!

 Programming Tutorials

 Resources     » Articles
    » Projects
    » CS Dictionary
    » References
    » Books

 Site     » Home
    » About
    » Support
    » Make Donation
binary digits
C++ Ripped Apart Tutorial
Section 8: User-Defined Types, Header Files, Graphics 

You are in Section 8 of 9, Article 8.4 of 8.4

8 .. User-Defined Types, Header Files, and Graphics Intro
       8.4 .. Creating User-Defined Header Files


Creating User-Defined Header Files

What is a header file?

Every time a program is written, certain #include statements must be specified at the beginning of the program. The #include statement is actually a link to a header file containing pre-defined elements that need to be used for successful execution of the program.

Header files are created so that the code contained in them can be used in multiple programs without having to write the code every time. It is all pre-defined in a separate header file so all you have to do is "include" it.

Consider the iostream.h header file. Load your compiler and open iostream.h. What you see is actually code used to define the standard input/output streams. By using this header file in your programs, you have access to the input/output streams. Without this convenient header file, if you wanted to have access to the input/output streams, you would first have to define them. Very tedious and time consuming not to mention the extra effort of learning how to define them.

What is a user-defined header file?

A user-defined header file is a header file that you, as the programmer, created. iostream.h and all other header files which are included using angle brackets (<>) are pre-defined.

For example, the following header files are pre-defined:



When you create a header file, which is described below, and "include" it in a program, you will use quotes instead of brackets.

For example, suppose guess.h is a header file that you created for a program. You can include this header file in your program by using:



User-defined header files are commonly used to define constants, user-defined types, and to list function prototypes to be used in a main file.

Header files do not normally include function definitions. Definitions will normally be placed in a separate "auxiliary" source file that will have a (.cpp) extension. Also note that you shouldn't compile header files individually. The header files will be compiled when you compile the actual program that tries to link to it.

Also, when you compile the actual program, the only elements in the header file that will be compiled are the elements in which the program will need to use.

The following explains how to create a header file using the Dev-C++ V 4.9.9.2 compiler:

First, open a new document and clear it. Next, enter the following information (the elements are described further below):



Notice the (#) symbol preceding commands in the above file. Anytime the (#) symbol is used, it is associated with the compiler directories. It tells the compiler to do something as in turning on/off some compiler setting.

Any statement preceded with the (#) symbol is not an executable statement. It only functions during compilation. In the above code, NAMEOFHEADER is the name of the header file. This name can be anything you would like it to be, but it should be descriptive and reflect its contents.

The     #ifndef     command can be read as "if not defined". #ifndef means it checks to see if a given symbol is defined or not. If the symbol is not defined, compilation will move into the body of the header file and include necessary definitions.

If the symbol is defined, the header fill will not need to include the definitions. The     #define     is only performed if     #ifndef     returns a value of true, which means the symbol is not defined.     #define     literally does what it reads: it defines the elements contained in the header file for use in the program which called upon it.

Essentially, these two commands (#ifndef and #define), guard against re-declaration errors if the header file is included in the calling program more than once. In a single application, you may be using multiple source files, and it is possible that more than one of the source files may try to #include the same header file.

Generally speaking, when you create a header file that contains the constants and prototypes for a program, you will also want to create an auxiliary file (will have a .cpp extension) that holds all of the function definitions to be used in the program.

This means that the only function in the main program will be the main() function. The question may arise as to how the compiler will know how to link the auxiliary file to the actual main program file. This can be done through the use of a project file.

It is important to note that the main program file, which contains the main() function, and the auxiliary file, which contains the function definitions, both have to include the user-defined header file and also any other compiler directives they use.

As mentioned earlier, when you include a user-defined header file, you have to use double quotes instead of using brackets. The brackets tell the compiler that it is dealing with pre-defined include files and the quotes tell the compiler it is dealing with a user-defined header file.

Creating Project Files

In order to successfully link all of your source files (main file and auxiliary file), you will have to create a project and place the files in it. Project files are used to "tie together" multiple source files into a single executable application or program. Creating a project is extremely easy. The following explains how to create a project using the Dev C/C++ V 4.9.9.2 compiler:

  1. if the individual source files already exist, which means you have already created them, click the file menu and select new project
  2. for DOS level programs (probably what you want), at the next prompt select console application and make sure C++ project is selected
  3. at the dialog box, enter a descriptive name for the project, which will be given an extension of .dev
  4. go to the project menu and select add to project
  5. at the open prompt, select all of the source files to be included in the program (NOTE: do not include the header file itself)
  6. go to the options menu and select Compiler Options. Select directories and enter the full path to the user-defined header file that you created in the text box (only needs to be done if you didn't place your user-defined header file in the default "include" directory under the Dev-C++ directories)
  7. go to the execute menu and select rebuild all

It is important to note that none of the source files can have the same base name as the project file. The base name of project will be used as the name of the executable file that is created after successful compilation. Assuming there are no errors, once the program is built, the executable file should be created (.exe extension with the base project name).

If you have not created any project files, simply choose to create a "New Unit for Project..." instead of "Add unit to project...".

Note: If all source files are located in the current working directory, then there is no need to create a project file.

The following are two source files and one user-defined header file that can be used as an example of setting up a project. The program is written for Dev C/C++ V 4.9.9.2 compiler.

Here is the header file:

Source Code

stuheader.cpp


Here is the main source file:

Source Code

stuheader1.cpp


Here is the auxiliary file:

Source Code

stuheaderaux.cpp


In the next section, we'll cover object oriented programming and classes.

Read on for more...

Next: What is OOP?

You are in Section 8 of 9, Article 8.4 of 8.4

[Back to Top]

Google
 
Web warebizprogramming.com

.: Report Bug : Error Log : About Webmaster :.

Search Warebiz!
binary digits binary digits

 Random Insight ::

binary digits

 Book References ::

binary digits
 C++  .NET  C#  Java  C  ASP  HTML  Prolog  Visual Basic  Javascript  JScript  VBScript  MFC  Perl  PHP  Basic  Python  C  Assembler  VB  Ada  Ruby  XML  ASP.NET  C++  Java