I don't get how and when you use header files. I've always been programming with everything in one big '.cpp' file. I'd be grateful if somebody would explain this to me.
iostream haven't the extension .h but it is a header file
Headers have the declarations for symbols compiled separately (other cpp files or libraries ) so you can use them.
an #include "something.h" is found, that line is replaced by the contents of something.h
In the example above iostream is used for std::cout
Example:
1 2
//MyFunc.h
void MyFunction(int);
1 2 3 4 5
//MyFunc.cpp
void MyFunction(int n)
{
// do something
}
1 2 3 4 5 6 7
//main.cpp
#include "MyFunc.h"
int main()
{
MyFunction(5);
}
In the main file MyFunction is called but without including the header it wouldn't have been recognised
1.) Break up and organize the code. When you're writing programs that are only a couple hundred to couple thousand lines of code, this isn't necessary. When you're working on a project that is several hundred thousand lines of code, it's absolutely essential.
2.) If properly organized, recompiling large projects doesn't require recompiling the entire project, only the code that has changed since the last compile. Again, with small programs that compile in less than a minute, this isn't necessary.
3.) When working with a team of programmers on a single large project, each member will be working on a seperate component, contained in a separate subset of files. If everything was contained in one large file, having multiple people working on that one file at the same time just adds another layer of impracticality to the scenario.
Bottom line, header files are for bigger projects. No need to worry about them till your projects start grouping to several thousand lines of code and larger. At that point, it will be useful to learn just how to properly organize your files to take advantage of their potential benefits. (for example, a poorly organized project might still require the bulk of the code get recompiled for just a small change in one file)
Header files are used also in small projects as is almost impossible creating a program without using any library. When using a library headers are required if you want to have the right declarations for that library.
Header files declare the interface to your classes, allow the separation of implementation, and serve as a quick reference to using your class.
Many times there is only 1 way to learn how to use a poorly documented class or library--the header files are the place to start figuring things out for yourself.
Or, you could implement some state-of-the-art, brand-new, useful, revolutionary library, coded privately; compile it, and distribute the binaries and header files for public use and never distribute your top-secret implementation code! It's like selling award-winning BBQ and not giving out the recipe. Mmmmmbbqq!
$ g++ ./test.cpp -o ./test.exe
./test.cpp: In function ‘int main()’:
./test.cpp:5: warning: deprecated conversion from string constant to ‘char*’
/tmp/ccOxaPgp.o: In function `main':
test.cpp:(.text+0x76): undefined reference to `writesomething(char*)'
collect2: ld returned 1 exit status
And If I include 'a.cpp' as well:
$ g++ ./test.cpp ./a.cpp -o ./test.exe
./test.cpp: In function ‘int main()’:
./test.cpp:5: warning: deprecated conversion from string constant to ‘char*’
./a.cpp: In function ‘void writesomething(char*)’:
./a.cpp:5: error: ‘cout’ was not declared in this scope