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.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <cmath>
#include <cstdlib>
#include <iostream>
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<<angX<<" Degrees in respect to X-axis.\n";
                 
                 return 0;
                 }
float Point::argumenty() const
{
                 float  angY; //Angle of vector to y-axis 
                 angY=acos(y/m)*(180/pi);
                 cout<<angY<<" Degrees in respect to Y-axis.\n";
                 return 0;
                 }
float Point::argumentz() const
{
                 float angZ; //Angle of vector to z-axis
                 cout<<z<<endl<<m<<endl<<pi<<" Z point";
                 angZ=acos(z/m)*(180/pi);
                 cout<<angZ<<" Degrees in respect to Z-axis.\n";
                 return 0;
                 }


int main(int argc, char *argv[])
{
    //Point point1;
    float Xpoint, Ypoint, Zpoint; //This is the user input
    
    cout<<"Describe the point by entering the x-axis, y-axis,"
        <<"z-axis"<<endl<<"-->";
    cin>>Xpoint>>Ypoint>>Zpoint;
    
    Point point1(Xpoint, Ypoint, Zpoint);
    
    cout<<"The magnitude of the point is "<<point1.magnitude()<<endl;
    cout<<point1.argumentx();
   cout<<point1.argumenty();
   cout<<point1.argumentz();
   
    system("PAUSE");
    return EXIT_SUCCESS;
}
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.