What goes into the header file? C++

Discussion in 'OT Technology' started by AbortionSurvivor, Mar 1, 2003.

  1. AbortionSurvivor

    AbortionSurvivor Active Member

    Joined:
    Jun 5, 2002
    Messages:
    3,016
    Likes Received:
    0
    Location:
    Nor. Cal
    what do i write in the header file? Is it just the class definition?
    I have another implementation file for my function definitions, but how do i link my main.cpp file with my implementation.cpp with my header.h?
     
  2. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    You can put function prototypes in the header file as well.

    Actually, you can put anything in a header file, but in general it's good style to just put classes and prototypes in them.
     
  3. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Constant definitions can also go into a header file.

    Think of your implementation.cpp as a third-party library. You want to publish by way of your header file whatever information a user might need to make use of your library. Generally, those things are function prototypes, type definitions and constant definitions.

    From a code maintenance point of view, if you have a project with many .cpp files, and you find yourself putting something like #define MY_CONSTANT 0x0000FFFF in a bunch of them, you're better off defining that once in a header file so that any change to MY_CONSTANT is easily propagated and less prone to cause a bug.

    As far as your linking issue goes, all you need to do is #include "header.h" in your main.cpp file. Then, compile your object files, then feed them to your linker to create an executable.
     
  4. BaZ

    BaZ 2004 ACC Champions

    Joined:
    Jun 12, 2001
    Messages:
    2,005
    Likes Received:
    0
    Location:
    Hokieville, USA
    as one of my software engineering professors told me:

    you put anything in the header that you dont care about people seeing. everything else goes into the .cpp. all you need for a program to link is the headers and the object files, thus for many libraries, thats all youll get.
     

Share This Page