Thread: Header Files

  1. #1
    Registered User
    Join Date
    Aug 2006

    Header Files


    As a beginner, I am finding it difficult to understand the function of header files, why they are used, and what you should include in them. I don't really know the difference between a .h file and a .cpp file.

    Suppose I have a class MyClass, with a member variable, int MyVariable, and a member function MyFunction. I could write this in a .h file:

    class MyClass
    int MyVariable;
    void MyFunction()
    // Do something
    And if I #include this .h file at the beginning of my source code, then I can use this class with no problems.

    However, I have noticed that many projects will only put the decleration in the .h file, and the definition will appear in a .cpp file. What is the point in this? As far as I'm concerned, it just makes everything more dispersed and harder to follow.

    Other times, the definition is included in the .h file, as above, and this seems like the sensible way of going about it.

    Perhaps it is something to do with how the compiler interprets .h and .cpp files, but I don't really know what the difference is between them.

    Please help

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    when you want to use std::string are you interested to know how the c_str() function implemented?

    no - enogh to know that it is present and its prototype.

    headers contain info required to use the class from the external code - class definition and function prototypes.

    all the code that could be hidden from the user of the class and (for exaple) compiled in the lib-file provided with the header file to use on linkage stage - goes into cpp
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    Ė David J. Wheeler

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    While it will work with classes, it also does two things:
    It implies that all functions are inline.
    By storing the definition along with the declaration, you make it harder to browse the contents of the class, what members it exposes, etc.

    Aside from that, headers are used to keep declarations that the compiler needs to see when you want to use something - ie a class declaration. So that when you use the class, you simply include the file instead of re-writing the class declaration.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  3. classes and header files
    By Drake in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2006, 07:12 PM
  4. Request for comments
    By Prelude in forum A Brief History of
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM