header files and code files..

This is a discussion on header files and code files.. within the C++ Programming forums, part of the General Programming Boards category; I'm a Java guy where all you need is a .java file. What's the deal on the .h and .cpp ...

  1. #1
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438

    header files and code files..

    I'm a Java guy where all you need is a .java file. What's the deal on the .h and .cpp files? Must you have both in a program (simple stuff mind you)? I know you need .cpp but what about the .h file?

  2. #2
    Registered User skyline's Avatar
    Join Date
    Dec 2001
    Posts
    49
    i don't think you actually need .h (but i would suggest to use .h). i think you could really just have a bunch of .cpp files and making use of the keyword "extern" a lot.

  3. #3
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    So why is the .h file there and what purpose does it serve?

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    A header file is best used to declare class and function prototypes. When you #include them, the preprocessor adds them to the code, so therefore you dont have to replicate every object and function to use it within your source.

    They can be used with .cpp files or precompiled libraries....

    For precompiled libraries, they can be usefull for you to give an indication of what your class does without having to give away the full source code and implementation.

    Have a look in any C++ book for information on this.

  5. #5
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Ah ok. So something like the iostream could be brought in as a header file instead of #include iostream in your .cpp file? Would there be any advantage do doing it like that or should you stick with the include?

    -Edit: Just to explain, I'm not ready to start learning C/C++ yet. I've still got so much to learn in Java before I'd feel confortable starting something new. I'm trying to understand some of the mindset and logic of C/C++ before I do get started, whenever that may be.
    Last edited by CompiledMonkey; 02-15-2002 at 08:37 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help using Header Files
    By d34n in forum C Programming
    Replies: 8
    Last Post: 04-21-2008, 11:06 PM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  3. Code Header Files
    By strickey in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2005, 10:07 AM
  4. I keep seeing some advice.. (Header file related)
    By Lithorien in forum C++ Programming
    Replies: 10
    Last Post: 01-14-2005, 03:31 AM
  5. Merging files (code included)
    By TankCDR in forum C Programming
    Replies: 3
    Last Post: 10-28-2001, 10:06 AM

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