# Thread: Need help(3D point class)

1. ## Need help(3D point class)

Hello ,, there is some error with member funtion distance can anyone help me with it.

Code:
```#include<iostream.h>
#include<conio.h>
#include<math.h>
class point
{
int x;
int y;
int z;
public:
void setPoint()
{
cin>>x>>y>>z;
}
void printPoint()
{
cout<<"("<<x<<","<<y<<","<<z<<")"<<"\n";
}
void negatePoint()
{
cout<<"Negate of point is:("<<-x<<","<<-y<<","<<-z<<")\n";
}
void distancePoint(float)
{ point dis;
dis=pow 1(x,2)+ pow(y,2) +pow(z,2);
dis=sqrt(dis);
}
};
int main()
{
point a;
clrscr();
a.setPoint();
a.printPoint();
a.negatePoint();
a.distancePoint();
getch();
return 0;
}```

2. Code:
```include<iostream>
#include<math.h>
class point
{
int x;
int y;
int z;
public:
void setPoint()
{
std::cin>>x>>y>>z;
}
void printPoint()
{
std::cout<<"("<<x<<","<<y<<","<<z<<")"<<"\n";
}
void negatePoint()
{
std::cout<<"Negate of point is:("<<-x<<","<<-y<<","<<-z<<")\n";
}
#if 0
// This peice of code is blocked coz i cant understand what
// u want to do in distance point if u want to knw the distance
// of two points and return the diff
void distancePoint(const float distance)
{
point dis;
dis.x = pow 1(x,2)+ pow(y,2) +pow(z,2);
dis.y = sqrt(dis.x);
}
#else
point Distance(const point point_a,
const point point_b) {
point result;
result.x = point_a.x - point_b.x;
result.y = point_a.y - point_b.y;
result.z = point_a.z - point_b.z;
return result;
}

#endif
};
int main()
{
point a;
#if 0
clrscr(); // Sorry i m compiling with g++
#endif
a.setPoint();
a.printPoint();
a.negatePoint();
#if 0
a.distancePoint(); // Changed the signature u can use however u want :)
#endif
#if 0
getch(); // window's specific
#endif
return 0;
}```

Check this out......
And i compiled this with g++ 4.3.2 on linux kernel 2.6.2
so microsoft specific function has been commented and abt distancepoint i dont understand what u want to do with that

3. What error are you having?

4. distance function calculates the distance from the origin, sqrt((x-0)^2,(y-0)^2,(z-0)^2)

5. K then write the code and u will find any problem tell me, sorry i was not aware of that

6. And by the way what is the origin in your sample code ?????

7. Originally Posted by Fatima Rizwan
distance function calculates the distance from the origin, sqrt((x-0)^2,(y-0)^2,(z-0)^2)
Why u are having the forumla like above that can be written like
sqrt((x)^2,(y)^2,(z)^2)

8. origin is 0

9. Code:
```#include<iostream.h>
#include<conio.h>
#include<math.h>
class point
{
int x;
int y;
int z;
public:
void setPoint()
{
cin>>x>>y>>z;
}
void printPoint()
{
cout<<"("<<x<<","<<y<<","<<z<<")"<<"\n";
}
void negatePoint()
{
cout<<"Negate of point is:("<<-x<<","<<-y<<","<<-z<<")\n";
}
void distancePoint()
{ double dis;

dis=sqrt(pow(x,2)+pow(y,2)+pow(z,2));

}
};
int main()
{
point a;
clrscr();
a.setPoint();
a.printPoint();
a.negatePoint();
a.distancePoint();
cout<<a.distancePoint();    //ILLEGAL STRUCTURE OPERATION
getch();
return 0;
}```
hOW TO PRINT IT??

10. distancepoint returns void.

11. To answer the last question, the distance function should return the result.

Similarly it would more sense for negate to return a point with negated values, and for setPoint not to ask for input but to accept three values through arguments.

12. Fatima is ur problem solved or not ??? or just stuck in distance caluclation ????

13. ya its solved,,,=))