/*
02.*烟台大学计算机学院学生
03.*All right reserved.
04.*文件名称*烟台大学计算机学院学生
05.*All right reserved.
06.*文件名称:成员函数,友元函数和一般函数的区别
07.*作者:王洪海
08.*完成日期:2013年4月11日
09.*版本号:v1.0
10.*对任务及求解方法的描述部分:成员函数,友元函数和一般函数的区别
11.*/
#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 distance1(Cpoint &); //成员函数求两点间距离
friend double distance2(Cpoint &a,Cpoint &b); //定义友元函数
void input() //输入坐标
{
cin>>x>>y;
}
void show()
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
double getx()
{
return x;
}
double gety()
{
return y;
}
};
double Cpoint::distance1(Cpoint &a)
{
return sqrt((a.x-x)*(a.x-x)+(a.y-y)*(a.y-y));
}
double distance2(Cpoint &a,Cpoint &b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double distance3(Cpoint &a,Cpoint &b)
{
return sqrt((a.getx()-b.getx())*(a.getx()-b.getx())+(a.gety()-b.gety())*(a.gety()-b.gety()));
}
int main()
{
Cpoint p1(2,3),p2;
cout<<"第一个点的坐标为: "<<endl;
p1.show();
cout<<"请输入第二个点的坐标: "<<endl;
p2.input();
cout<<"1.两点间的距离为: "<<p1.distance1(p2)<<endl;
cout<<"2.两点间的距离为: "<<distance2(p1,p2)<<endl;
cout<<"3.两点间的距离为: "<<distance3(p1,p2)<<endl;
return 0;
}
运行结果,如下图: