Hey guys,
New to the forums, and new to c++ but just need a hand distinguishing header files and source (.cpp) files apart. I come from java, where header files are never needed, so I am a little confused. I know that header files are used while compiling the program to speed things up, and I know that it is possible to do things without header files, but if I am to learn c++ I want to do it properly.
It seems that there is no difference between source files and header files. From what I have seen, the main() function has a source file and everything else can be done in header files by just #include 'ing them. What is even the point in having more classes???
Is there a tutorial that you guys can point me to that makes a simple application (console or opengl, opengl is what I want to learn) with multiple class files, and multiple header files?
I know that header files are used while compiling the program to speed things up
That's not accurate. Header files are used to declare classes.
.cpp files are used to implement your classes. Anyone that wants to use your class only needs the header and the object file of the compiled .cpp file. They don't need to see how the class is implemented.
Header files in C++ are different from cpp files in it's execution.
Basically what you do in your header file is lay out the markup of the object. It's the blueprint if you will.
The source file, or .cpp is used for implementing that class.
This is important in terms of data hiding. In many cases, it's not necessary for other that use your code to actually know how it is implemented. They just need to use the functions and variables.
By using a header file, you give any other user the opportunity to know how your object will function, but you do not reveal how it works internally.
The tutorial here one OOP should give you enough insight in why this is
The tutorial here one OOP should give you enough insight in why this is
I think I understand the point now, but am still unclear as to what exactly goes in the header files. Do you know if there are any small, simple projects (maybe 2 or 3 classes and header files) which are available for download? This will let me have a look to see how they have structured it all.
In C++ functions has to be declared and classes has to be defined before you can use them. If you don't use header files you would have to declare all the functions you are going to use and define all the classes you are going to use in that .cpp file. So if you want to make a change to a class you would have to edit all the files that define the class. If you use header files you only have to change one file.
Normally only .cpp files are compiled. #include is just an automatic copy paste. You would get the exact same result if you replace the #include with the content of the file.
Things that are often put in header files are class definitions, function declarations, constants, ... things that you are going to need in other files. Note that some things can't be defined more than once so you can't put it in the header file. A non-inline, non-template function definition is such a thing that you should not put in the header. If you do, you would get an error if you try to include that file in more than one .cpp file.
#include <iostream>
#include "Name.h"
int main ()
{
std::cout << "this program is an example of an OOP \n";
aName objectName;
objectName.aFunction(3);
return 0;
}