class object

Hi! I have created a class named point, where I could get x and y coordinates of a point. Now I want to calculate the distance between two points. I've done this like that: pt1.distance(pt2), but now i would like to do the same like this, in order to implentate a lot of points: something like: distance(pt1, pt2); so, i don't want to access to the method distance by starting from a point. Is it possible? Thanks!
Of course, just make it a free function.
What is it? =)
What is what?
A free function =)
Can I put it into the class?
I don't want to make a function in the main...
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
Hi , Suppose you have the class like 
class CMilage 
{
      private:  
                 int distance1 , distance2 ;
       public:
                CMilage() ; 
               int Distance( int dist1 , int dist2); 
};
CMilage::CMilage()
{
      distance1 = 0 ; distance2 = 0 ; 
}

//This is your distance function 
int CMilage::Distance(int dist1 , int dist2)
{
     return ( dist1 + dist2 ) ; 
} 

int main()
{
   int x = 10 , int y = 20 ; 
  CMilage miles;
   cout <<"\n Total distance = "<< miles.Distance( x , y) ;
   return 0 ; 
}
A free function is one that isn't part of a class, e.g.

1
2
3
4
double distance(const point& p1,const point& p2)
{
  return p1.distance(p2);
}
This is my class:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef POINT_H
#define POINT_H

class point
{
public:
    point();
    point( float a, float b );
    float getX();
    float getY();
    float getPolarP();
    float getPolarAng();
    float dist( point b );
    void setCart( float a, float b );
    void setPolar( float a, float b );
private:
    float x;
    float y;
    float p;
    float ang;
};
#endif 


This is its cpp:

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
#include "point.h"
#include "cmath"

point::point() {  x = 0; y = 0; p = 0; ang = 0; }

point::point(float a, float b) {
    x = a; y = b;
    p = sqrt(pow(a,2)+pow(b,2));
    ang = atan2(b, a);
}

float point::getX() { return x; }

float point::getY() { return y; }

void point::setCart(float a, float b) {
    x = a; y = b;
    p = sqrt(pow(x,2)+pow(y,2));
    ang = atan2(y, x);
}

float point::dist( point b ) {
    return sqrt(pow(b.getX() - x, 2) + pow(b.getY() - y, 2));
}

float point::getPolarP() { return p; }

float point::getPolarAng() { return ang; };

void point::setPolar(float a, float b) {
    p = a; ang = b;
    x = p*cos(ang);
    y = p*sin(ang);
}


a piece of main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
point* pt[6];
    float x1, y1;
    bool exit = false;
    char e;

    cout << "x1 = ";
    cin >> x1;
    cout << "y1 = ";
    cin >> y1;
    pt[1] = new point(x1, y1);

    cout << "x2 = ";
    cin >> x1;
    cout << "y2 = ";
    cin >> y1;
    pt[2] = new point(x1, y1);


and why in main this doesn't work??

cout << pt[1]->dist(pt[2]);


???



Try

cout << pt[1]->dist(*pt[2]);

you are passing a pointer to dist.
It works! Thank you very much!! XD
The proper solution would have been not to allocate your point objects dynamically.
Care to explain why you're doing that?
Because I didn't think another solution to make a user able to choose how much object create... Do you know a better way?
(now i'm trying to use vectors to make it possible)
Topic archived. No new replies allowed.