Class declaration

With regards to declaring and defining a class, I think it should be as follows:

Class declaration goes in a header file
Class definition and member function definitions go in a cpp file


Now, my questions are:

Do I declare access levels?

When I declare the default constructor, do I also give the default values or do I put these in the definition?

What do I do with the local variables?

And presumably the class definition and member function definitions can all go in the same cpp file?

Do I declare access levels?

Access levels go with the declaration. At least that's how I've always done it. Seems awkward/not possible to put them in the .cpp file.

When I declare the default constructor, do I also give the default values or do I put these in the definition?

Definition. The declaration is just there to declare names.

What do I do with the local variables?

Definition, same reason as above.

And presumably the class definition and member function definitions can all go in the same cpp file?

I don't understand this question.
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
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

some_class(): localVariable1, localVariable2 //etc. (so I'm actually putting the names of the variables rather than their type)

and in the definition the only thing I do differently is give them default values

some_class(): localVariable1(0), localVariable2(10)

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.
Last edited on
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?

some_class(): int, int {}
1
2
3
4
5
6
7
8
class MyClass
{
    int x;
    std::string y;
public:
    MyClass();
    //...
};
1
2
3
4
MyClass::MyClass() : x(7), y("Hello, Amnesiac!")
{
    //...
}
Last edited on
Topic archived. No new replies allowed.