Thread: Need help understanding Header Files

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    19

    Need help understanding Header Files

    Hey guys, I just got back into learning C++, and I'm going through the C++ Primer book. I understand the part about creating classes and stuff, but what is the point of a Header File? What do you need to include in a header file? And also, what is the difference/connection between the header file, the main project, and the class? Thanks for any explanations.

    -Kai

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    19
    Thanks, but I've already tried that site, and I don't know why, I'm in China, and it seems like they have something against Wikipedia. Because no matter where I go in wikipedia, I can't connect...

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The point is that the compiler needs to see a class definition or function prototype before using the class/calling the function to make sure it generates correct code and catch errors for you.
    So for every .cpp file that needs to use the class/function, it needs the class definition or function prototype. So to avoid copying it and pasting it everywhere, you put that in a header file and include it in all .cpp files.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Kaidao View Post
    Thanks, but I've already tried that site, and I don't know why, I'm in China, and it seems like they have something against Wikipedia. Because no matter where I go in wikipedia, I can't connect...
    Well, since I've already given the attribution, I hope they don't mind a copy-and-paste.
    In computer programming, particularly in the C and C++ programming languages, a header file or include file is a file, usually in the form of source code, that is automatically included in another source file by the compiler. Typically, header files are included via compiler directives at the beginning (or head) of the other source file.

    A header file commonly contains forward declarations of subroutines, variables, and other identifiers. Identifiers that need to be declared in more than one source file can be placed in one header file, which is then included whenever its contents are required.

    In the C and C++ programming languages, standard library functions are traditionally declared in header files; see C standard library and C++ standard library for examples.

    Motivation

    In most modern computer programming languages, programs can be broken up into smaller components such as subroutines, and those components can be distributed among many physical source files, which are compiled separately. Once a subroutine needs to be used somewhere other than where it's defined, the concept of forward declarations or function prototypes must be introduced. For example, a function defined in this way in one source file:

    Code:
    int add(int a, int b)
    {
        return a + b;
    }
    may be declared (with a function prototype) and then referred to in a second source file, thus:

    Code:
    extern int add(int, int);
     
    int triple(int x)
    {
        return add(x, add(x, x));
    }
    However, this simplistic approach requires that the programmer maintain the function declaration for add in two places — in the file containing its implementation and in the file where it's used. If the definition of the function ever changes, the programmer must remember to update all the prototypes scattered across the program, as well.

    Header files provide the solution. A module's header file declares each function, object, and data type that is part of the public interface of the module — for example, in this case the header file would include only the declaration of add. Each source file that refers to add uses the #include directive to bring in the header file:

    Code:
    /* File add.h */
    #ifndef ADD_H
    #define ADD_H
     
    int add(int, int);
     
    #endif /* ADD_H */
    
    /* File triple.c */
    #include "add.h"
     
    int triple(int x)
    {
        return add(x, add(x, x));
    }
    This reduces the maintenance burden: when a definition is changed, only a single copy of the declaration must be updated (the one in the header file). The header file may also be included in the source file that contains the corresponding definitions, giving the compiler an opportunity to check the declaration and the definition for consistency.

    Code:
    /* File add.c */
    #include "add.h"
     
    int add(int a, int b)
    {
        return a + b;
    }
    Typically, header files are used to specify only interfaces, and usually provide at least a small amount of documentation explaining how to use the components declared in the file. As in this example, the implementations of subroutines are left in a separate source file, which continues to be compiled separately. (One common exception in C and C++ is inline functions, which are often included in header files because most implementations cannot properly expand inline functions without seeing their definitions at compile time.)

    Alternatives

    Header files are not the only solution to the problem of accessing identifiers declared in different files. They have the disadvantage that it may still be necessary to make changes in two places (a source file and a header file) whenever a definition changes. Some newer languages (such as Java) dispense with header files and instead use a naming scheme that allows the compiler to locate the source files associated with interfaces and class implementations.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    19
    Hmm..I kinda get it, so it's kinda like a interface, right? Basically for maintenence issues. So how do you discern between what you should define/declare in the class and what you should define/declare in the header file?

    -Kai

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Class definitions (how the class looks, members, functions, etc) and function prototypes (how the function looks, what arguments it takes, etc) is placed inside headers.
    All other source code is placed inside .cpp files (the only exception to this rule is templates - they, too, go into headers).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You declare things that are used in multiple places [or likely to be so in the future] in header files. If it's not needing to be shared between different places, then it doesn't need to be (and thus shouldn't be) in a header file.

    What you declare inside a class follows the same rule. You usually declare classes in header files, but a class can be declared anywhere, as long as it's declared before its use.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    19
    Hm..alright thanks guys. I understand it a little better now, but not completely. At least now I know what header files are for..now I just have to understand better what I need to put in header, and what I need to put in class files. Thanks for your help.

    -Kai

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Kaidao View Post
    ....and what I need to put in class files. Thanks for your help.
    Your code! Declarations and class definitions only shows the layout of classes and how what arguments functions takes.
    The actual source is done in .cpp files.

    Function prototype (declarations) (example):
    Code:
    void foo(int a, int b);
    Class definition (example):
    Code:
    class myclass
    {
    public:
       void foo();
    private:
       int myint;
    };
    Templates:
    Code:
    template<typename T> void foo(T arg)
    {
        // My code
    }
    All these go into headers. Rest go into .cpp files.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    19
    OHHHHHH damn I get it now. Thanks Elysia...You're a programming master! How the hell do you know every thing? . Serious, thanks. I get it a lot more now. It's a lot different from Java...

    -Kai

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confusion on header and source files
    By dnguyen1022 in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2009, 03:42 AM
  2. C Header Files
    By devarishi in forum C Programming
    Replies: 8
    Last Post: 12-10-2008, 04:53 PM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. user created header files
    By MedicKth in forum C++ Programming
    Replies: 6
    Last Post: 09-20-2003, 12:36 PM
  5. more header files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-28-2001, 01:56 PM