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

来源:互联网 发布:连接linux服务器工具 编辑:程序博客网 时间:2024/06/05 03:18

问题及描述:

/* *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成员无法直接输出,需要通过接口函数输出



0 0
原创粉丝点击