Thread: problem with Class Point.

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    8

    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.
    Last edited by utoots; 03-28-2003 at 02:46 PM.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 09:42 PM
  3. Class problem
    By w274v in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2006, 06:53 PM
  4. problem with sending files to a class method..
    By utoots in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2003, 01:38 AM
  5. static class problem.
    By Sebastiani in forum C++ Programming
    Replies: 3
    Last Post: 10-16-2002, 03:27 PM