Broad Network


C++ Header File

Writing a C++ Application – Part 2

Forward: In this part of the series I talk about C++ header file.

By: Chrysanthus Date Published: 7 Feb 2013

Introduction

This is part 2 of my series, Writing a C++ Application. In this part of the series I talk about C++ header file. This tutorial is a continuation from the previous tutorial. You should be reading the parts of this series in the order given. A typical C++ application is made up of more than one file. An application is a large program. In order for these files to work together, you need another file called a header file, to join them together. I show you how the header file is produced in this part of the series. The header file is not necessarily a namespace. The example I gave at the end of the previous part of the series is in the global scope (or global namespace). I talk about the header file here with the same global scope assumption. Well, the header can be related to a namespace; that I explain later.

Entities
A C++ entity is a value, macro, instantiated object, fundamental object, class, class member, array, array element, function, enumerator, template, or namespace. With the exception of value, a header file consists of declarations of one or more of these entities. In practice, some of the entities, not only value, will not be accepted into headers. Some of the entities accepted are declared in a special way. For the ones that are accepted, I show you the way in which they are declared in this tutorial.

As mentioned above, a header file is used to join C++ files together to form an application (large program). In a C++ program or large program (application) an entity is not “defined” more than once. If an entity is to be used in more than one file for an application, the entity has to be declared in a header file. Again, not all entities can be declared in a header file. Each of the C++ files (.cpp files) that uses these common entities need to have the header file included at its top, with the #include preprocessing directive. You can join C++ files using the extern specifier for entity names, but that is not the official way to join files; besides, to keep typing the extern specifier in special places of files, would be cumbersome.

The content of a header file can simply be called the header. You saw a header in the previous part of the series. You saw the one header in a number of .cpp files. The header was not yet in a file of its own. To form a header file, simple cut off that header from one of the .cpp files and place in one special file called the header file; I say more on this below. The recommended extension for a C++ header file is .hh. I spend much of this tutorial explaining how to code the declarations of the above entities that a header would accept.

Macro
An object macro for a number can be declared as follows:

    #define objMcr 25.3

where 25.3 is a number example. That would go fine into a header.

An object macro for a character can be declared as follows:

    #define objMcr 'A'

where A is a character example. That would go fine into a header.

An object macro for a string can be declared as follows:

    #define objMcr "some txt"

where "some txt" is a character example. That would go fine into a header.

A function-like macro can be declared as follows:

    #define max(a, b) ((a) > (b) ? (a) : (b))

where max is an example function name; (a, b) are an example parameter set and “((a) > (b) ? (a) : (b))” is an example function-like macro body. That too would go fine into a header.

Remember, at the end of a macro (object or function-like) you do not type the semicolon, but you press the keyboard Enter Key, to implicitly have the ‘\n’ character.

Fundamental Object
The declaration of a fundamental object in a header is different from the declaration of an object macro. With a fundamental object you have to specify the type; you end the declaration with a semicolon; the object has to be constant and be assigned a value. I use the double object (data) type to illustrate this. The declaration can be something like,

    const double pi = 3.141592;

This is accepted by a C++ header.

Function
An example of a function declaration is:

    int fn (int theInt, char theChar);

So, you declare a function in the header using the function prototype. It ends with a semicolon. The definition would be in one of the .cpp files.

Enumerator
An example of an enumeration declaration is:

    enum DayNumber {today, tomorrow, afterTomorrow, theDayAfterTomorrow};

An enumeration is a collection of constant integers identified by names (idebtifiers), e.g. today and tomorrow in this example. The collection has a name e.g. DayNumber, which is like an object (data) type. The enumerator is a kind of class/struct construct with its own special properties. The above syntax (type of declaration) is accepted in a header.

Namespace
The namespace does not really have a declaration that is accepted by a header. However, the declarations of entities accepted by a header can all be placed in a (local) namespace; more on this later.

Class
A synopsis is a summary of a class description (definition). It has the properties of the class and the prototypes of the member functions.  An example of a class synopsis is:

    class Calculator
        {
            public:
            int num1;
            int num2;

            Calculator(int ident1, int ident2);

            int add();
        };

You should have seen this in the previous part of the series. You use a class synopsis as declaration for the class in a header – this too, is accepted by a header.

For the synopsis, the implementation (full definitions) of the member functions has to be below the header, in one of the .cpp files.

Example Header File
The following is an example header file content:

    const double pi = 3.141592;
    #define myConstStr "I love you."
    enum DayNumber {today, tomorrow, afterTomorrow, theDayAfterTomorrow};

    class Calculator
        {
            public:
            int num1;
            int num2;

            Calculator(int ident1, int ident2);

            int add();
        };

    static Calculator::Calculator myObject(2,3);

    void fn();

This is the header for the application in the previous part of the series. It begins with the declaration of a const double. You then have the definition of a string object macro. After that you have the declaration of an enumeration. Then you have the class synopsis. After that a static instantiated object declaration born of the class synopsis. Then you have a function declaration. Function declaration is the typical entity declaration of a header.

This header is from the previous part of the series. This header was typed in the two files, sampleEntities.cpp and mainFile.cpp . The application we are dealing with consisted of these two files in the previous part of the series. For the file sampleEntities.cpp, you had the following two lines above the header:

    #include <iostream>
    using namespace std;

These two lines are part of the header of the sampleEntities.cpp file alone. They are not part of the header of the mainFile.cpp file. These two lines are in the sampleEntities.cpp file because this file uses the cout object of the C++ iostream header file. The iostream header file is of the C++ std namespace – more on header in namespace later. For this application, which we continue to develop in this series, these two lines will not be part of our header of interest. In my computer, the mainFile.cpp file is in the working directory, MinGW.

The header of interest can be cut off from the two files, sampleEntities.cpp and mainFile.cpp and placed in a different file. Do this; cut off the header of interest from both files (actually delete one and cut off one); open or start a blank text editor document; paste the header of interest in the blank document; save the resulting header file in the c:\dir1 directory with the name, demo.hh.

Now, go to the two files (sampleEntities.cpp and mainFile.cpp), where the same header was cut off in both files and type,

    #include "c:\dir1\demo.hh"

and press the Enter key. There should be no text on the left of the line before #. During compile time, this line will be replaced by the header content from the header file, demo.hh of c:\dir1.

You should now have three files for the application: sampleEntities.cpp and demo.hh in the c:\dir1 directory and mainFile.cpp in the working directory. The content of the demo.hh file is just the header.

In the compilation command line you still have the two file names and the name of the executable file. The demo.hh file name does not feature (is not typed) in the command line. Its content is included into the two files by the #include directive in the two files.

Now compile the application with the following command and note that it compiles without any error massage:

    g++ c:\dir1\sampleEntities.cpp mainFile.cpp -o mainFile.exe

Execute the application with the following command and note that the output of the function fn() in mainFile.cpp would be the same as it was in the previous part of the series:

    mainFile.exe

Note: the header file is included in all the .cpp files that use the entities. However, only one of the .cpp files should have the definitions of the entities.

Short Name for Long Declaration Name
Imagine that the name of a class is very long. You can use the typedef specifier to have a short name alias in the header (file). You use the alias in the same way that you would use the long name. Assume that “Calculator” is a long name. You can use the typedef specifier as follows, in the header, below the synopsis (above):

    typedef Calculator Calc;

In the body of a file, you can have something like:

            Calc obj(5,6);
            cout << obj.add();

Name of Application
At this point we should think of the name of the application. The name is mainFile.exe, which is the executable file. That is not a suitable name. So let us call it, Demo. Demo consists of three files: sampleEntities.cpp, mainFile.cpp and demo.hh. demo.hh is not typed at the compiler command line. Nothing stops you from giving the executable file, the name, demo.exe, and type at the compiler command line.

Guide line for Files
Apart from the header file, the C++ files should be produced in an almost independent way. Apart from the header file these same files should be typed at the compiler command line (for compilation).

Syntax for Including a File
You can include a header file or any (.cpp) file to a file using one of two possible syntaxes. My compiler for example comes with two directories called the “include” directory (the two directories have the same name, but are of different paths). If you place the file to be included in any of the “include” directories, then you would use the syntax:

    #include <filename.ext>

alone on one line, pressing the keyboard Enter key at the end. Your compiler should have a similar default directory. If the file to be included is in a different directory, then you would use the syntax:

    #include "pathToFile"

alone on one line, pressing the keyboard Enter key at the end. One syntax uses angle brackets; the other uses double quotes.

We are at the end of this part of the series. Some of you are now wondering how the template (container) fits into the header concept. For that I say, “do not worry”, I have a whole part of the series, dedicated to template container.

For now, we take a break and continue in the next part of the series.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message