Thread: Headers?

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    34

    Headers?

    I dont understand the point of headers? do they have a real purpose? ok say i make a class that creates a scene and then i want to make a character in the scene in java i would create a character class and then import that class at the top of my main class and call its constructor. Do headers have anything to do with this?

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    What does your C++ book say?

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Header files specify the interface of classes & functions, while .cpp files contain the actual implementation of those classes & functions.
    Think of it like the difference between an Interface and a Concrete class in Java (sort of).

    Why would you want to use header files? Because C/C++ is very different than Java, since it creates binary executable files instead of bytecode interpreted files. So if you include the same implementation in multiple places, the linker will yell at you for including the same thing multiple times; but if you just include header files, it gives the compiler a hint of what is to come later, and the linker then only has one implementation of everything to deal with.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The difference has nothing much to do with creating binary executable rather than bytecode interpreted files. Bytecode files are just a specific executable format.

    In software (or any system) design, a critical concern is separating interface from implementation. Interfaces (i.e. the defined mechanism for interacting with something) need to be as stable and well defined as possible, as changing interfaces usually means code (i.e. implementation) on both sides of the interface must be changed. With a well-defined interface, however, code can be updated without breaking things (eg when changing the implementation of a function, it is not necessary to change code that calls the function - unless the interface changes).

    How's this relevant to header files? Header files are, by convention, normally where interfaces (APIs, class types, etc) are specified. They are #included for use by any code that needs to either use that interface (eg call functions in the API) or to implement the functionality. This removes dependency between the code that uses functionality, and the code that implements the functionality (i.e. if the implementation changes, usage doesn't).

    Header files, incidentally, are not the only way to achieve separation of interface and implementation. They just happen to be the means that is best known (and arguably) easiest to employ in C/C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-18-2005, 02:26 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM