您的位置:首页 > 其它

第七周项目(1)-普通函数求两点间距离

2016-04-13 13:24 176 查看
问题及代码:

/*
*copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称:hellow.cpp
*作者:田甜
*完成日期:2016年4月13日
*版本号:v1.0
*问题描述:用普通函数求两点间的距离
*输入描述:////

*程序输出:按要求处理后输出

*/

#include <iostream>
#include <cmath>
using namespace std;

class CPoint
{
private:
double x;
double y;
public:
CPoint(double,double);
double get_x();
double get_y();
};
double get_far(CPoint &p1,CPoint &p2);//get_char为普通函数,声明要在类之后
CPoint::CPoint(double xx,double yy):x(xx),y(yy){}
double CPoint::get_x()
{
return x;
}
double CPoint::get_y()
{
return y;
}
double get_far(CPoint &p1,CPoint &p2)
{
double far=sqrt((p1.get_x()-p2.get_x())*(p1.get_x()-p2.get_x())+(p1.get_y()-p2.get_y())*(p1.get_y()-p2.get_y()));
return far;
}
int main()
{
CPoint p1(0,0),p2(1,1);
cout<<get_far(p1,p2)<<endl;
return 0;
}

运行结果:



心得体会:

普通函数用到类中成员的时候声明一定要在类之后
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: