您的位置:首页 > 其它

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

2016-04-13 12:55 309 查看
问题及描述:

/*
*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;
double far;
public:
CPoint(double,double);
double get_far(CPoint &p);
};

CPoint::CPoint(double xx,double yy):x(xx),y(yy){}
double CPoint::get_far(CPoint &p)
{
far=sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
return far;
}
int main()
{
CPoint p1(0,0),p2(1,1);
cout<<p1.get_far(p2)<<endl;//无法直接输出类的成员far,需要通过函数返回值或return语句
return 0;
}

运行结果:

心得体会:

类的private成员无法直接输出,需要通过接口函数输出


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: