Please explain very slowly in a loud voice.... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Please explain very slowly in a loud voice....

Can anybody tell me what a Header File actually is and why they are used? What sort of thing goea in them? I have been watxhing a few youtube videos, but a loy of them say "We'll just set up a header file" with no explanation. I don't really know what they are doing! Thanks.

4th Aug 2017, 2:22 AM
Richard Appleton
Richard Appleton - avatar
12 Answers
+ 7
😆 c++ is a vast beast.
4th Aug 2017, 2:49 AM
jay
jay - avatar
+ 6
lol wait till you start playing with template metaprogramming 😀
4th Aug 2017, 2:51 AM
jay
jay - avatar
+ 2
Hi James. I have been watching the Cavestory link you sent me and that's what has triggered my question. I just wanted tk understand what he was doing and why in a bit more detail. :-D
4th Aug 2017, 2:39 AM
Richard Appleton
Richard Appleton - avatar
+ 2
Ah. So it is like creating a library of routines you can call on and add into other programmes as required?? That makes sense!! OK. so how is that different to setting up another .cpp file or a class?
4th Aug 2017, 2:44 AM
Richard Appleton
Richard Appleton - avatar
+ 2
Some times you don't want to show your code to a customer, but he wants to be able to use the code. Then, you create a header for the file, with the interface (usually .h), and another file for the implementation (usually .cc or cpp). You then compile the implementation and provide the .o file to your customer, as well as the header, so he can use it without seeing the implementation. This is just an example of why headers can be separetade from implementation.
4th Aug 2017, 1:20 PM
Denis Felipe
Denis Felipe - avatar
+ 1
Header can be considered as a tool kit, it contains many tools like cout, cin in iostream. When you want to use the tools you have take the kit similarly you must include the header file when you want to use the methods that are predefined in them.
4th Aug 2017, 2:29 AM
G.S.N.V. Suraj
G.S.N.V. Suraj - avatar
+ 1
Dam!! I thought I had this sussed! I thought a class was a piece of code attached to a specific routine or object and that it was written as an additional .cpp file.
4th Aug 2017, 2:48 AM
Richard Appleton
Richard Appleton - avatar
+ 1
I'm having a stroke!
4th Aug 2017, 2:49 AM
Richard Appleton
Richard Appleton - avatar
+ 1
I have an example of a fully working programme here from my tutor. It has loads of header files... one for each thing on the game from the sound to the object to the scrolling. Each header also has a .cpp code file. I am guessing the headers are classes as they have constructors and deconstructors and private variables. So I am guessong that oncluding the header automatically includes the relevent .cpp code? Even though I can find no mention of it anywhere in the main.cpp?
4th Aug 2017, 3:01 AM
Richard Appleton
Richard Appleton - avatar
+ 1
Ok. so a couple more questions:. what is #pragma? how do you actually use these classes. I can't find any reference to them in the main code.
4th Aug 2017, 3:19 AM
Richard Appleton
Richard Appleton - avatar
+ 1
It's an introduction to C++ with no previous knowledge required. Apparently! I downloaded this app because I had no idea what was going on. I have learnt more from this basic intro and the people on this forum than on the course!
4th Aug 2017, 3:39 AM
Richard Appleton
Richard Appleton - avatar