.cpp and .h

This is a discussion on .cpp and .h within the C++ Programming forums, part of the General Programming Boards category; What is the difference between a .cpp file and a .h file?...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    127

    .cpp and .h

    What is the difference between a .cpp file and a .h file?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,435
    .cpp files contain your source code.
    .h files contains declarations and definitions and are thusly included into .cpp files via #include.
    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.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Nothing, except for how you use them.
    cpp files you compile
    h files you include
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by herWter View Post
    What is the difference between a .cpp file and a .h file?
    cpp are the guts of the code. h files are headers, which usually only contain declarations and macros. You shoudl not put functions in h files because there willa rise a conflict if you include the h file from more than one cpp file. The compiler will complain about redefinition.


    the cpp file woudl contain
    Code:
    #include <stdio.h>
    
    void HelloWorld(){
       printf("Hello World!\n);
       return;
       }
    the .h file would contain
    Code:
    void HelloWorld(void);
    any other cpp fiels that needed to use the HelloWorld function would simply include the .h file.
    Last edited by abachler; 07-02-2008 at 09:13 AM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Note that in C++, () and (void) both specify empty parameter lists. So your example is okay. Still, I like to have the prototypes match the implementations . . . .

    Plus, your example is in C, where () and (void) are not the same thing. (void) means, "this function takes no parameters." () means, "this function could take any number of parameters -- no information about its parameters is available."

    For example:
    Code:
    #include <stdio.h>
    
    void func1() {
    
    }
    
    void func2(void) {
    
    }
    
    int main() {
        func1(1);  /* this does not generate an error */
        func2(2);  /* this does generate an error */
        return 0;
    }
    In C, one should use (void) whenever possible. The behaviour of () is a holdover from K&R C.

    In C++, as I mentioned, () and (void) are the same thing.
    Last edited by dwks; 07-02-2008 at 12:20 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,435
    Ya, so do I.
    Although in C, it is actually only required to put (void) in the prototype, and not the actual definition.
    It's possible, but not required.
    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.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    [edit] Never mind, it seems you're right.

    Though there is one possible pitfall that I can see: if you leave out the (void) from a function definition, and don't have a prototype, then functions calling that function from later in the file will evoke the allow-any-number-of-arguments behaviour. Rather unlikely, but there you have it [/edit]
    Last edited by dwks; 07-02-2008 at 12:35 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,435
    But it depends on the whole implicit function call behavior which is to be avoided at all costs.
    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.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Of course. It's even deprecated.

    BTW, "implicit function declaration" is not the same thing as we're discussing here. Implicit function declaration is the behaviour that assumes a prototype of int func() for an unprototyped function. It's deprecated too.

    This might be better termed . . . I dunno, empty parameter list behaviour or something.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    Also, it's a good idea to make your header-files this way:

    Code:
    #ifndef SOMENAMEFORTHEHEADER
    #define SOMENAMEFORTHEHEADER
    ...code goes here...
    #endif
    This will make sure that if you use multiple header files, it will only be included once.
    It reads like:
    -If SOMENAMEFORTHEHEADER isnt defined, then define SOMENAMEFORTHEHEADER, and process all code untill #endif


    Before compilation, the preprocessor-commandoes like this come into work, and so, if this have allready been included, SOMENAMEFORTHEHEADER will allready be defined, and therefore, nothing happens when included multiple times.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    A good name to use for the header guard is HEADER_H, where that's the name of the file. For example, for "core.h", you might use CORE_H.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    A good name to use for the header guard is HEADER_H, where that's the name of the file. For example, for "core.h", you might use CORE_H.
    I'm not that experienced with C++... but is it to prevent your names from accidently messing with reserved words or other other defined constants, while using consistent names for the defines?
    Last edited by Drogin; 07-02-2008 at 03:49 PM.

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by dwks View Post
    A good name to use for the header guard is HEADER_H, where that's the name of the file. For example, for "core.h", you might use CORE_H.
    But then what happens if you have two include files with the same name in different directories?
    I use FILENAME_H_MONTH_DAY_YEAR or something like that, just to make if even more unlikely to have the same name as another header.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,435
    Then you would be better of using some DIRECTORY pragma so separate files with the same name but from different dirs.
    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.

  15. #15
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    Then you would be better of using some DIRECTORY pragma so separate files with the same name but from different dirs.
    I've never heard of a "directory" pragma? Besides, most pragmas are compiler specific.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Get .cpp and .h files to communicate...
    By yaya in forum C++ Programming
    Replies: 6
    Last Post: 11-24-2008, 11:45 PM
  2. .cpp and .h files - organization
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 01-12-2006, 10:40 PM
  3. Why Declare in .H but Define in .CPP ?
    By Krak in forum C++ Programming
    Replies: 10
    Last Post: 07-30-2005, 12:36 AM
  4. placement of (.h) and (.cpp) files..!?
    By matheo917 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 05:37 PM
  5. Class files (.h and .cpp
    By Todd in forum C++ Programming
    Replies: 7
    Last Post: 02-14-2002, 02:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21