A class declaration consist of:
_ The name of the class
_ Its members, with access specifiers.
_ The declaration (prototype) of Its methods, with access specifiers.
A class definition is simply the definition of its methods.
¿what goes where?
A_ Put the class declaration on the header.
The problem is that everyone see the private details. That means that you need to recompile client code because of a change in the class members.
1_ Put only the public methods and a private pointer to the actual structure that holds the private data (pimpl)
So the client code knows nothing but the interface. However you need to manage the pointers.
About default values, put them in a header so the compiler (and the user) can see them.
Also inline or template functions should go in headers too
Another issue is how to define what should be a method and what a global function
and with declaring the local variables it would be something like
1 2 3
private:
int localVariable1;
int localVariable2;
or do I not need to specify the variable names?
And with the last question I got confused so ignore it, but in the definition of a class would you always define its member functions within it? Or is it sometimes better to define some outside it?
in the definition of a class would you always define its member functions within it? Or is it sometimes better to define some outside it?
If you declare it inside the class, the compiler will try to inline the function. If you have some small functions that you'd want inlined then you can just stick the definition in there also. I would only do it for very small functions though as it can get hard to read if you mix definitions and declarations.
So when I declare the default constructor (let's say the class type is called some_class and the two variables are both integers), is it just
Stick the declaration of the variable in with the definition of the function. Think of it like this:
In the class declaration, all you're saying is what variables and functions the whole class will have access to. How those functions are implemented is entirely up to the function and should not matter to the main declaration file. Think of when you read documentation on some classes, do they say what variables are being used inside each function? No, just what variables are passed as arguments.
EDIT:
I should note that this an issue of preference really. Each way has it's pros and cons as ne555 has stated above, but there really isn't a "wrong" way to do this.
Sorry I meant when you come to write the definition of the class and its member functions (in a cpp file) does it make a difference whether you define the member functions inside the curly braces of the class or outside it?
And I'm still confused about the default constructor, would the declaration be something like this then?