C++友元函数求两点间距离,改错!
#include<math.h>#include<iostream>usingnamespacestd;classCPoint{private:doublex,y;pub...
#include<math.h>
#include<iostream>
using namespace std;
class CPoint
{
private:
double x,y;
public:
void set(double xx,double yy)
{
cin>>xx>>yy;
x=xx;y=yy;
}
friend double distance(CPoint &a,CPoint &b);
};
double distance(CPoint &a,CPoint &b)
{
double l;
l=sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
return l;
}
int main()
{
CPoint p1,p2;
double a,b,c,d,n;
p1.set(a,b);
p2.set(c,d);
n=distance(p1,p2);
cout<<"Distance="<<n<<endl;
return 0;
} 展开
#include<iostream>
using namespace std;
class CPoint
{
private:
double x,y;
public:
void set(double xx,double yy)
{
cin>>xx>>yy;
x=xx;y=yy;
}
friend double distance(CPoint &a,CPoint &b);
};
double distance(CPoint &a,CPoint &b)
{
double l;
l=sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
return l;
}
int main()
{
CPoint p1,p2;
double a,b,c,d,n;
p1.set(a,b);
p2.set(c,d);
n=distance(p1,p2);
cout<<"Distance="<<n<<endl;
return 0;
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询