I am trying understand what I am supposed to put in a header file. It seems like the same thing as an interface.
Interface -> define functions required for a class and their signatures
Header File -> I was taught not to put the functions here, but just their input and output so that if someone wanted to learn about my code they wouldn't have to look through my cpp code
Can I ask you a favour. I'm started learning c++ less than a week ago. The concept including, creating, and implimenting interfaces is still foggy for me.
#include "helloWorld.h"
int main()
{
helloWorld();
return 0;
}
In this example, the interface to the helloWorld function is found in the header file helloWorld.h. The helloWorld.cpp file implements the function. The main program only needs to see the interface to be able to compile.
Both .cpp files are compiled into object files (most likely .o or .obj depending on your system) independently, and neither depends on the other--each .cpp file depends only on helloWorld.h (helloWorld.cpp also depends on iostream). After both object files are compiled, the linker links them together to create a fully functioning program.
Notice that the helloWorld.cpp file only needs to see the iostream header (interface) file, not the entire implementation of iostreams. The linker will pull in the required libraries to provide the iostream capabilities.
Is this the type of explanation you are looking for?
I was in France asking what to put on my feet, and they said "chaussures", but when I looked at one of these "chaussures" it was the same thing as a shoe!
It's just a matter of organization.
Also, having different interfaces can speed up compiling the program: files that are not changed are not compiled again.
That was helpful, but I am more confused on the implementation with classes.
Do you mind creating an examle (with 3 files like above) but with an interface class, the class itself, and then how it would all come together in the main.
There are no "interface" or "abstract class" keywords in C++.
A class is abstract if one of more of its methods is pure virtual (undefined in the base class). Like so:
1 2 3 4
class Abstract
{
virtualvoid func(int a) = 0;
};
The function "func" must be defined in a derived class.
You can create an "interface" base class similar to Java (I believe - my Java was never great and is a bit rusty now) by merely by creating a class with only pure virtual functions in it. The above class Abstract is essentially an interface class.
C++ allows for multiple inheritance, so a class can inherit from one or more "non-interface" classes as well as one or more "interface" classes. Because of multiple inheritance, C++ does not distinguish between "interface" classes and "non-interface" classes.
No, that woiuld be double-defining. To do so, HelloWorld.h should contain just class HelloWorld;
That is an instance of forward declaration. This tells the compiler that there is a class called HelloWorld that will be defined later. The compiler then knows about it and can create pointers and references to object of that type because it doesn't need to know how much memory the class takes up, or what it can do.
This will work just fine for compiling your version of HelloWorld.cpp (although trivially so). However, you would not be able to compile main.cpp. The main function needs to create an object of type HelloWorld (line 5 in my code example). Because the compiler only has a forward declaration available in the header file, it will not know how to allocate memory for hw, nor whether hw.printGreeting() is legal in line 6.
If this is confusing, go ahead and ignore it. If you understand the concept behind what to put in .h and .cpp files, you should be fine.
So if you made the change to HelloWorld.h that @viliml suggested:
1 2 3 4 5 6
#ifndef HELLOWORLD
#define HELLOWORLD
class HelloWorld;
#endif
and moved the declaration and implementation of the class into HelloWorld.cpp as you suggested, main.cpp would not compile. When trying to create hw in line 5, the compiler would not know what to create, because the included file (HelloWorld.h) would only contain a forward reference.
This is a subtle point and hinges on the recognition of the forward declaration that @viliml mentioned. If it's the only thing you don't understand, you can move on. You'll eventually understand it as you get more experience.