#include<iostream>
#include<cmath>
using namespace std;
class Cpoint
{
private:
double x; // 横坐标
double y; // 纵坐标
public:
Cpoint(double xx=0,double yy=0):x(xx),y(yy){}
double distance2(Cpoint&t1);
friend double distance1(Cpoint&t1,Cpoint&t2);
double get_x();
double get_y();
};
double distance1(Cpoint&t1,Cpoint&t2)
{
double dDistance;
dDistance=sqrt((t1.x-t2.x)*(t1.x-t2.x)+(t1.y-t2.y)*(t1.y-t2.y));
return dDistance;
}
double Cpoint::distance2(Cpoint&t)
{
double dDistance;
dDistance=sqrt((x-t.x)*(x-t.x)+(y-t.y)*(y-t.y));
return dDistance;
}
double Cpoint::get_x()
{
return x;
}
double Cpoint::get_y()
{
return y;
}
double distance3(Cpoint&t1,Cpoint&t2)
{
double a,b,c,d,dDistance;
a=t1.get_x();
b=t1.get_y();
c=t2.get_x();
d=t2.get_y();
dDistance=sqrt((a-c)*(a-c)+(b-d)*(b-d));
return dDistance;
}
int main()
{
Cpoint C1,C2(3,4);
cout<<"两点之间的距离为"<<endl;
cout<<C1.distance2(C2)<<endl;
cout<<distance1(C1,C2)<<endl;
cout<<distance3(C1,C2)<<endl;
return 0;
}
2015.4.15成员函数,友元函数,一般函数的区别
最新推荐文章于 2017-11-13 22:36:00 发布