-   Programming (
-   -   for c++ programmers, header files? (

michaelinux 01-17-2012 04:05 PM

for c++ programmers, header files?
for all c++ programmer out there, i am trying to learn c++ and since i know some java and c#, it hasn't that difficult (yet). One thing that i really don't get yet is the use of header files, i mean having to write a .h file with the definitions and make sure all the right headers are included and then the .cpp file for the implementation, i find that a long process compared to java or c# where you just start writing your classes and functions and that's it.
i remember reading that some programmers don't use .h files anymore and that it could be ignored. I guess this is because they were talking about personal projects but idk.
so what is the main use of header files and why would i bother in writing one everytime i need a new class.
sorry if it's a really noob question. :(

keirey 01-17-2012 05:01 PM

Header files are used to separate the interface (function declarations and public data types) from implementation (how the functions are constructed and how private data types are used). This makes it possible to "hide" the implementation from the end user.
Suppose you have a working program and you find a better algorithm or way to implement a function. You could then change the implementation of the function in the .cpp file, as long as the function parameters remain the same. Then you can simply recompile the program using the new .cpp file. That way there is no need to change the header and main files.
For simple programs and exercises, it is not always necessary to have separate header files, however, for larger programs with many classes or programs with classes to be used by other end users, I find it a good habit to use separate header and definition files.

For a more extended discussion check out:

You should also read "Thinking in C++, Vol.1" by Bruce Eckel, which you can download from several sources online.

Hope this helps.

All times are GMT -5. The time now is 07:33 AM.