### Writing a class but having trouble

So the problem is the value of m in the magnitude function. The value is not transferring out to be used at any of the argument function.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889`` ``````#include #include #include const double pi=3.141592654; using namespace std; class Point{ public: Point(){} Point(float, float, float); //double getx() const { return x;} //double gety() const { return y;} //double getz() const { return z;} float magnitude() const; //determine each points magnitude to origin float argumentx() const; //Angle of the vector in degrees float argumenty() const; //Angle of the vector in degrees float argumentz() const; //Angle of the vector in degrees float m; private: float x,y,z; }; //This is the point object, initialize the data members to parameter values //Constructor definition Point::Point(float xval, float yval, float zval){ x=xval; y=yval; z=zval; } //This is the magnitude //Member function definition float Point::magnitude() const { float m=sqrt(x*x+y*y+z*z); return m; } //This is the angle member function definition float Point::argumentx() const { float angX; //Angle of vector to X-axis angX=acos(x/m)*(180/pi); cout<"; cin>>Xpoint>>Ypoint>>Zpoint; Point point1(Xpoint, Ypoint, Zpoint); cout<<"The magnitude of the point is "<
I see...

You calculate m, however it is a local variable only. It isn't a private member and therefore cannot be used by the other functions in the class.

To solve this, m should be a private member. Replace line 23 with: `float x,y,z,m;` and replace line 40 with: `m = sqrt(x*x+y*y+z*z);`
Thank you, I am beginning to understand the private use now. It works.
It sure does!

Private is great if you are doing internal calculations and you don't want "others" to be able to access intermediate calculations.
Topic archived. No new replies allowed.