0

I'm really new to c++. I have a simple console application with an header.h that holds my class

class MyClass
{
public:
    float x, y, z;

    MyClass(float x, float y, float z);
};

I have a implement.cpp with all my implemented methods and I have

MyClass::MyClass(float x, float y, float z) {};

Then in main.cpp I try to simply print the values

int main()
{
  MyClass One(-3.0f, 0.0f, 4.0f);

  cout <<  "Firsth Object: " << One.x << ", " << One.y << ", " << One.z << endl;
}

But in console values are printed like :

-1.07374e+08, -1.07374e+08, -1.07374e+08

What am I doing wrong?

VP.
  • 15,509
  • 17
  • 91
  • 161
Koosshh56
  • 317
  • 3
  • 17

2 Answers2

5

Your constructor did not initialize any of the members: MyClass::x, MyClass::y nor MyClass::z.

You must do:

MyClass::MyClass(float x, float y, float z)
{
    this->x = x;
    this->y = y;
    this->z = z;
};

or better yet (more idiomatic, and possibly faster):

MyClass::MyClass(float x, float y, float z) : 
    x( x ), y( y ), z( z )
{
};

Without that, you're printing the values of uninitialized members of the MyClass object One. In general, you must always initialize members of a class before you can use them.

Community
  • 1
  • 1
jpo38
  • 20,821
  • 10
  • 70
  • 151
  • 4
    Turn MyClass::MyClass(float x, float y, float z) {}; into MyClass::MyClass(float x, float y, float z) : x(x), y(y), z(z) {}; – SenselessCoder Jan 23 '16 at 15:12
  • Really thank you. As I said, I'm really a beginner. What is the meaning of this-> ? – Koosshh56 Jan 23 '16 at 15:14
  • 1
    Just FYI, you don't need to use different names for the parameters in an initializer list and variable beginning with `_` are reserved by the standards spec. – Galik Jan 23 '16 at 15:17
  • It is not necessary to rename arguments with a `_` prefix, compiler is smart enough in this case to distinct class members and function arguments. – VP. Jan 23 '16 at 15:18
  • @Galik: I know, but I'm always scared about using the same names.....I prefer to avoid any kind of ambiguity. – jpo38 Jan 23 '16 at 15:18
  • 1
    Hmm, sorry, I totally just imposed the same variable names on your answer then. I agree with Galik and Victor, it's completely unnecessary and hurts readability. But if you insist on mangling names, why not mangle the private members, instead of the formal arguments? – Cody Gray - on strike Jan 23 '16 at 15:21
  • @CodyGray: No problem and thanks for the help and the added link! – jpo38 Jan 23 '16 at 15:23
  • @jpo38 If you think about it it is actually always safer to use the same names, less chance you get the variables in the wrong order because they always look visually identical - mistakes look more obvious. That's how I see it :) – Galik Jan 23 '16 at 15:24
  • @Koosshh56: In case you're also a StackOverflow beginner, don't forget to accept an answer on this page! ;-) – jpo38 Jan 23 '16 at 15:24
2

Your current constructor does nothing.

You have to initialize your objects variable. For this you can use

MyClass::MyClass(float x, float y, float z) : x(x), y(y), z(z) 
{
}

This type of initialization in constructors is called initializer list, you can read it up here.

This has the same effect as the constructor pointed out by @jpo38.

Markus Weninger
  • 11,931
  • 7
  • 64
  • 137
  • This answer duplicates another with just a little correction. It is worth just to add a comment to the `jpo38`'s answer. – VP. Jan 23 '16 at 15:14
  • Okay, so should I delete the answer? I'm pretty new to writing ansers on SO. – Markus Weninger Jan 23 '16 at 15:16
  • I think yes, because people should usually add answers which have a significant difference. If you just have a correction of another answer - just add a comment to it, there is no need in duplicating answers. – VP. Jan 23 '16 at 15:18
  • But I think that this _has_ a significant difference, because most other languages don't have a construct like _initializer list_. I came from C# and Java and was suprised that something like this is possible. – Markus Weninger Jan 23 '16 at 15:19
  • 1
    @VictorPolevoy: It _is_ a significant difference. – Lightness Races in Orbit Jan 23 '16 at 16:57