Thread: differance between headers and CPP files

  1. #1
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163

    differance between headers and CPP files

    Does anyone know the differance and what should be used when?

    If someone has a 'rule of thumb' I would love to hear it.

    James
    My site to register for all my other websites!
    'Clifton Bazaar'

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I don't know - but it may have something to do with the way they're compiled, and .h files may be compiled in C syntax and not C++. I use hpp to be on the safe side.

  3. #3
    You should know that in C++ you must declare functions before you use them. You must 'declare' all functions before the main() fxn otherwise you will get an error.
    //////////////////////////////////////////////////////////////////
    int myfxn(int k, int y); //This is considered a 'declaration' since it doesn't have any '{}' blocks and thus does nothing yet.

    void main() { //Here's our main fxn that starts our program
    ...;
    }

    int myfxn(int k, int y) //Down here is where we 'define' our function and tell it what to do
    {
    ...;
    }

    //////////////////////////////////////////////////////////////////////////
    The Header files (.h) simply hold the declarations, that way you don't have to put them in your .CPP file. Instead you just #include "name.h" them into your .cpp file. The header files hold the declarations for the functions you use in the .cpp file. However, their real value becomes evident when you get into Classes and other advanced C++ topics.

    For now, the answer to your question is the header files hold the 'declarations' of the functions or Classes you use so you could put our declaration of myfxn(int k, int y); into a file and give it a .h extension and that is our header file, then #include it above the main() fxn.

    .cpp files hold the Definitions of our functions and Classes.

    //////////////////////////////////////// Here's our new code//////////

    #include "somename.h"

    void main() { //Here's our main fxn that starts our program
    ...;
    }

    int myfxn(int k, int y) //Down here is where we 'define' our function and tell it what to do
    {
    ...;
    }
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  4. #4
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163
    At the moment I am putting everything into header files, but after reading what you've written I think I might change a few around and see what happens
    My site to register for all my other websites!
    'Clifton Bazaar'

  5. #5
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Just put the class declarations in the header files. Than define the methods of these classes in separate .cpp files. In the main program include the header files but not the .cpp files because the cpp's are compiled along with main as long as you are using a project workspace. I have no idea what to do in Linux, I think you need a make file or some god aweful thing.

  6. #6
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163
    Now to ask a question that some people may find is a bit simple:
    Because I have been using headers and only a main.cpp I have forgotten how to use a second .cpp file. Can someone please remind me of what I need to do in the .cpp?

    James
    My site to register for all my other websites!
    'Clifton Bazaar'

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    32
    You include it in the same way as a header file. In the main.cpp file add #include "yourfile.cpp" .

    In that .cpp file usually goes the implementation of whatever you've declared in the header, like e.g. the definitions of member functions.

    Seron

  8. #8
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189
    I did programming work this summer (and over the holidays) on a major software project being designed in C++, and this was their convention:

    All class declarations (including declarations of member variables and functions) and global functions were to be located in header files, along with the documentation. Each class in most cases was to have its own individual header file. Each header file then had its own .cc file of the same name, which contained the implementations of all the functions declared in the header file.

    Only .h files were #included by other files. The .cc files were compiled individually by the person who created the code and stored as makefiles, which were automatically added to the final executable when included by a .cc file with a main() function. However, different compilers and environments deal with multiple .cc files in different ways.

  9. #9
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163
    Thanks for all the comments people, I'm rearranging my program as we all talk
    My site to register for all my other websites!
    'Clifton Bazaar'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. statics in headers
    By KIBO in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2009, 09:49 AM
  2. Unexpected end of file, splitting headers into .h/.cpp
    By Shamino in forum C++ Programming
    Replies: 19
    Last Post: 12-11-2007, 01:08 PM
  3. [HOWTO] write classes headers
    By gibbofresco in forum C++ Programming
    Replies: 9
    Last Post: 10-26-2007, 12:32 PM
  4. Understanding Headers and Inclusions
    By jdm in forum C++ Programming
    Replies: 11
    Last Post: 04-21-2004, 10:11 PM
  5. Headers, Libraries and the rest
    By nickname_changed in forum C++ Programming
    Replies: 8
    Last Post: 06-23-2003, 08:17 PM