# problem with Class Point.

• 03-28-2003
utoots
problem with Class Point.
We are starting "classes" now in our introductory programming class. I'm having a problem sending information from int main() to the point class here's my code. This is just a simple lab where one sets coordinates and then finds the distance between the coordinates using the distance formula.

Code:

``` #include <iostream> #include <cmath> class Point  {  public:    Point();    Point(int xval, int yval);  int getX();  int getY();  void setX(int xval);  void setY(int yval);  void setXY(int xval, int yval);  void printPoint(void);  private:  int x;  int y;  }; Point::Point() {  x = 0;  y = 0; } Point::Point(int xval, int yval) {  x = xval;  y = yval; } int Point::getX() {  return x; } int Point::getY() {  return y; } void Point::setX(int xval) {  x = xval; } void Point::setY(int yval) {  y = yval; } void Point::setXY(int xval, int yval) {  x = xval;  y = yval; } void Point::printPoint() {  cout << "(" << x << ",";  cout << y << ")"; } //THIS IS WHERE IM CONFUSED.  GO DOWN FOR THE OTHER COMMENT float Point::distanceFrom(Point coordinate) {    distance = sqrt(pow((coordinate.getX() - x1), 2) + pow  ((coordinate.getY() - y1), 2);  return distance; } int main() {  Point pt1, pt2, pt3, origin;  pt1.setX(5);  pt1.setY(7);  pt2.setXY(-2, -4);  pt3.setXY(-8, -9);  origin.setXY(0,0); // THIS IS WHERE MY PROBLEM LIES AS WELL  cout << "The distance between pt1 and pt2 is " << pt1.distanceFrom(pt2) // HOW WOULD I SEND pt1??       << endl;  cout << "The distance between pt2 and pt1 is " << pt2.distanceFrom(pt1) // HOW WOULD I SEND pt2??       << endl;   }```
I don't know how to send the coordinates from pt1 to my distance formula in class point...please help.
• 03-28-2003
Magos
Why would you send Pt1 as a parameter to Pt1:s method?
The class has it's own data, so you only need to pass another point to the method so it can calculate the distance between itself and that point. A method can always access its own members (variables) which, I think, you're not realizing.

As you can see in the DistanceFrom method, one point is accessed though GetX and GetY methods. That is the point you pass, and you need methods (so called "getters") to access that data since it's private.
The other point is its own data, which is accessed directly through the member variables (x, y).
• 03-28-2003
change this:
distance = sqrt(pow((coordinate.getX() - x1), 2) + pow
((coordinate.getY() - y1), 2);

to this:
distance = sqrt(pow((coordinate.getX() - x), 2) + pow
((coordinate.getY() - y), 2));

Note that x and y are for the point that called the method and don't need other identifiers, whereas you have to use the getX() and getY() methods to get the respective values for the point passed in. Also note that I think you were missing a ) at the very end of the line.