I also understand what header files
and classes do in general. What I am
not getting is the implementation when
do I use a header or a class? Do I
need to create classes for everything
I do? Do my actual work functions need
to be in header files or in CPP files?
I'm lost on the proper uses of these
and could use some real world guidance
from more experienced programmers.
Re-wind a few years and I'd have had the same question - it isn't something that is covered very well I don't think. So let's break down that part which I see as the meat of your problem.
Do I
need to create classes for everything
I do?
That's subjective, but I'd say no. You need to create a class for things you want to model, where it makes sense to do so. An example might be an item of data that is more complicated than C's types like int
and char
. You should also use the String
class and STL
's containers (look at vector). In short, what this means is you will probably be using a class for most things you do, but that doesn't mean to say every last little part of your code must somehow fall into a class, somewhere.
I also understand what header files
and classes do in general.
Let me just repeat what you get then. As you are probably aware, C
and consequently C++
requires prototype definitions for functions that do not precede it in the compilation unit (complete source code after all the headers replace their includes). As such:
#include <iostream>
using namespace std;
int testadd()
{
int a = 4; int b = 2; int c = add(a, b);
return c;
}
int add(int x, int y)
{
return x + y;
}
int main(int argc, char** argv)
{
cout << "result is: " << c << endl;
return 0;
}
(Contrived example) won't work, because testadd
can't find add
but main
can find add
.
So your program ideally ought to begin with a list of all the functions, classes et al you want to use. Somebody decided it would be a good idea if these were in a separate file from the implementation as it makes things a lot easier to read and saves programmers using your work (in the library scenario) from re-writing prototypes to your functions. Makes using any other library a lot easier.
So, when you implement your classes, you have a choice:
- Declare the classes in prototype form (functions end as prototypes) and implement them in a cpp unit.
- Implement the whole lot in a header file.
Which you do is honestly up to you. I stick to 1, because I think it's nicer, but nobody says you must and I have seen both techniques used.
Incidentally, just worth mentioning, if you choose 1, let's say you have main.cpp, myclass.h, myclass.cpp and you call myclass methods implemented in myclass.cpp from main.cpp, what happens in compilation is that these two units (*.cpp) are compiled to machine code form (main.obj and myclass.obj in the case of vc). A linker is then responsible for bringing these two objects into an exe
, ensuring all the symbols functions) called in each compilation unit can be found in one of the referenced other objects or libraries. If that happens, it puts together an exe. If not, you get an error. That's the other consequence (link errors) which provoke such questions in learning users.