Reputation: 33
I have 2 questions to make. Why people implement some methods in the header of the class? Like:
public:
point2D():x(0),y(0){};
point2D(int xP, int yP):x(xP),y(yP){};
What is the real difference between this and implement in a different file(performance, memory)?
And my second question is about the use of the constructor in a inheritance.
1. point2D::point2D(){
2. x = 10;
3. y = 10;
4. }
5. point2D::point2D(int xP, int yP){
6. x = xP;
7. y = yP;
8. }
9. point3D::point3D(){
10. point2D();
11. z = 0;
12. }
13. point3D::point3D(int xP, int yP, int zP){
14. point2D(xP, yP);
15. z = zP;
16. }
When I try to create my constructor in a separate CPP and the constructor for the point3D that receive 3 parameters(line 13) try to use the point2D constructor with 2 integer parameters(line 14), the constructor that is actually called is the point2D with no parameters. Why? We have 2 parameters inside the call of the constructor.
Thank you guys and I hope I was clear in my doubts.
Upvotes: 0
Views: 212
Reputation: 311156
The both constructors
9. point3D::point3D(){
10. point2D();
11. z = 0;
12. }
13. point3D::point3D(int xP, int yP, int zP){
14. point2D(xP, yP);
15. z = zP;
16. }
are wrong.
Statements
10. point2D();
and
14. point2D(xP, yP);
do not make sense because they simply create temporary objects that will be deleted after execution of the statements.
I think you mean the following
9. point3D::point3D() : point2D(){
11. z = 0;
12. }
or simply
9. point3D::point3D(){
11. z = 0;
12. }
13. point3D::point3D(int xP, int yP, int zP) : point2D(xP, yP) {
15. z = zP;
16. }
As for the first question then these constructor implementations
public:
point2D():x(0),y(0){};
point2D(int xP, int yP):x(xP),y(yP){};
are enough simple and readable so they can be placed in a header. By default constructors defined in a class have function specifier inline
. And inline functions must be defined in each translation unit where they are used. Thus it is natural to have their definitions in the class definition though they also can be defined inlined in the same header after the class definition.
Upvotes: 0
Reputation: 167
Question 1:
I hope this is what you are asking.
People tend to implement constructors in header files and right in the class if it does not have much work to do and can be written on 1 line such as above. This is just style and varies person to person. Another reason is because it is inlined, for performance.
Else if you are talking about the constructor initializer list(CONSTR(...) : <constructor initializer list> { })
then the reason for this is that it initializes the data members, when you assign to them in the body of the constructor(like you are doing in the second piece of code) then that is not initializing it, that is assigning to them and is less efficient. Though it does have a use, sometimes you need to do other work before you get the data member/s value/s. Such as getting input.
Question 2:
You again have to use the constructor initializer list not the body so here is an example
point3D::point3D(int xP, int yP, int zP) : point2D(xP, yP), z(zP) { }
Upvotes: 1
Reputation: 9617
Both answers are tight together. The syntax at the top is member initializer list. This is used to initialize members, ie. set their initial values. It does not need to be in header but it can be inlined if it is there. Anything in the body of the constructor (including assignment to member variables) is done after this initialization.
Parent constructors can be called only from member initializer list. Your code at lines 10 and 14 are temporaries (just like point2D name_i_will_never_use();
), not calls to constructor of parent class.
Upvotes: 0
Reputation: 3506
Implementing methods in the header has the advantage that the method can be inlined by the compiler. This can bring a significant speed-up.
Upvotes: 0