请问以下c++程序的问题在哪里?它的功能是计算两点之间的距离

来源:百度知道 编辑:UC知道 时间:2024/05/10 20:06:36
#include<iostream.h>
#include<cmath>
using namespace std;
class Point
{
public:
Friend class Distance;
Point(float a=0,float b=0)
{
X=a;
Y=b;
}
void Print()
{
cout<<"X="<<X<<endl;
cout<<"Y="<<Y<<endl;
}
private:
float X,Y;
};

class Distance
{
public:
float Dis(Point&p,Point&q);
};

float Distance::Dis(Point&p,Point&q)
{
float result;
result=sqrt((p.X-q.X)+(p.Y-q.Y));
cout<<result<<endl;
return result;
}
void main()
{
Point p(10,10),q(20,20);
Distance d;
d.Dis(p,q);
return ;
}

Friend class Distance
friend 的 f 要小写- -!
friend class Distance

而且距离应该是平方和..
result=sqrt((p.X-q.X)*(p.X-q.X)+(p.Y-q.Y)*(p.Y-q.Y))

参考:
#include<iostream>
#include<cmath>
using namespace std;
class Point
{
public:
friend class Distance;
Point(float a=0,float b=0)
{
X=a;
Y=b;
}
void Print()
{
cout<<"X="<<X<<endl;
cout<<"Y="<<Y<<endl;
}
private:
float X,Y;
};

class Distance
{
public:
float Dis(Point&p,Point&q);
};

float Distance::Dis(Point&p,Point&q)
{
float result;
result=sqrt((p.X-q.X)*(p.X-q.X)+(p.Y-q.Y)*(p.Y-q.Y));
cout<<result<<endl;
return result;
}
void main()
{
Point p(10,10),q(20,20);
Distance d;
d.Dis(p,q);
return ;
}