//Header files! ! !

Im a beginner C++ programmer, and I want to know what are the advantages of using a header file. And how do I us my header files? Can someone please help me out, the information is very beneficial.

Comments

  • : Im a beginner C++ programmer, and I want to know what are the advantages of using a header file. And how do I us my header files? Can someone please help me out, the information is very beneficial.
    :

    Hi,

    Do you mean header files like , ,math.h> etc.? The advantage of using these header files is that it allows one to have access to the built in functions.

    One could use header files by using [b]#include[/b]. For example,
    [b]#include[/b] and [b]#include[/b].

    More information on header files can be found on the following website.

    www.cplusplus.com

    Hope this helps,


  • actually you are already using header files in some
    cases. #include is a header file.

    when you write your own you will call it from
    the a file with #include "myHeader.h"

    the stdlib .h files such as iostream which is a class
    do not use the quotes.
    bryce

    : Im a beginner C++ programmer, and I want to know what are the advantages of using a header file. And how do I us my header files? Can someone please help me out, the information is very beneficial.
    :

  • : actually you are already using header files in some
    : cases. #include is a header file.
    :
    : when you write your own you will call it from
    : the a file with #include "myHeader.h"
    :
    : the stdlib .h files such as iostream which is a class
    : do not use the quotes.
    : bryce
    :
    : : Im a beginner C++ programmer, and I want to know what are the advantages of using a header file. And how do I us my header files? Can someone please help me out, the information is very beneficial.
    : :
    :
    :


    Do you mean writing your own header files? It's really a software engineering matter. It decreases complexity. When you write a simple application there is often no need to use header files, but it is always considered good style to do so. A large application however, is always broken down into separate implementation files, each file implementing some logical unit of the program. This keeps you from having to recompile the entire program if only a small part of it changes (i.e. only one file is modified) and it also allows you to efficiently reuse parts of your program (by including the header files) without copying and pasting (which is a horrible idea you always strive to define things only once, in one location so that you do not have to search through code finding places where you have cut and pasted and modifying them if you find you made a mistake). Did that make since it seems a little rambling.

    Anyway, your question then becomes, which parts of the program do I want to reuse (i.e. what to include in header files.) you must understand the difference between internal linkage and external linkage.

    Internal linkage occurs when a name is local to a translation unit. Btw, a translation unit is an intermediate file that is created by taking your source file and actually physically including the code from all the header files into the same file (translation unit). This translation unit is then compiled into the object file (.o file).

    External linkage occurs when a name is not internal to a translation unit... i.e. the name occurs in multiple different source files.

    You will want to use header files to contain only those things with internal linkage (and not even all of those things with internal linkage). So, global variables (external linkage) do not go in header files. Non-inline member functions, including static members (external linkage) do not go in header files.

    Class definitions (internal linkage) however do go in header files. Enums (internal linkage) must be placed in the header file. Inline methods (internal linkage) are usually defined in header files as well.

    You are not allowed do define an external variable in a header file (the very name/keyword extern should clue you in to that).

    In general those things that go in a header file are as follows:
    Class definitions, including member function declaration, member data definition, static soncs member dec. static member declaration, etc. i.e. if Its a class, or in a class definition put it in a header file.
    Enums go in header files.
    Inline methods go in header files.
    Function prototypes go in header files.

    Some people say not to include static data definition or constant dat definition or static function definitions in header files because it pollutes the global namespace. I dont find much reason to disagree with them, but dont myself considering do so a sin the choice is up to you.

    Now, you know what goes in a header file.. how do you make one?

    Easy, open a file and save it with a .h prefix. Viola, you have a header file. Of course it is useless at this point. Thus you must put something in it. So define a class, throw in an enum and define a couple of inline methods or functions. Ok now the header file is getting somewhere. The final step is to protect the stuff in the header file from naming clashes. This is where the famous #if !defined comes in.

    i.e.

    #if !defined(my_header_file_24234)
    #define my_header_file_24234

    #end if

    the stuff in between will now be protected from link time naming clashes by being defined exactly once throughout the program. In C++ I have always heard to prefer the #if !defined(blah) over #ifndef . I dont have a valid reason for this, but I see no reason not to, and better programmers than me argue for it so I follow. I also choose to use a randomly typed sequence of numbers after the definition to keep naming clashes less likely if, god forbid, someone else should ever try to use one of my header files in a project. If it had the same name as an existing header file, there would be a naming conflict. Consider myString.h or something. Thus using #if !defined(myString_203947_h) would prevent this conflict.

    Well, I hope it is all as clear as mud now. Good luck!

    Ian

    P.S you include your own header files as mentioned by the others.
    #include
    #include "header_file_in_the_current_directory" // notice "" vs <>


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories