问题及代码:
/*
*Copyright (c)2016,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:main.cpp
*作 者:王艺霖
*完成日期:2016年4月9日
*版 本 号:v1.0
*
*问题描述:成员函数,友元函数和一般函数的区别
*/
#include <iostream>
#include<cmath>
using namespace std;
class CPoint//点类
{
private:
double x;//横坐标
double y;//纵坐标
public:
CPoint(int x=0,int y=0):x(x),y(y){}//构造函数
double getX(){return x;}
double getY(){return y;}
friend double line(CPoint &p1,CPoint &p2);
};
double line(CPoint &p1,CPoint &p2)
{
double x=p1.x-p2.x;
double y=p1.y-p2.y;
return sqrt(x*x+y*y);
}
int main()
{
CPoint point1(0,0),point2(3,4);
cout<<"两点之间的距离为:";
cout<<line(point1,point2)<<endl;
return 0;
}
运行结果: