您的位置:首页 > 其它

第七周项目1-一般函数

2016-04-17 12:40 295 查看


问题及代码:

/*

*copyright (t) 2016,烟台大学计算机学院

*All rights reserved.

*文件名称:test.cpp

*作者:张晴晴

*完成日期:2016年4月17日

*版本号:v1.0

*问题描述:一般函数中使用类的成员。
*输入描述:无。

*程序输出:两点之间的距离。

*/

#include<iostream>
#include<cmath>
using namespace std;
class cpoint
{
public:
cpoint(double xx=0,double yy=0):x(xx),y(yy){}
double getx(){return x;}
double gety(){return y;}

private:
double x;
double y;

};

void display3(cpoint&p1,cpoint&p2)
{
double x=p1.getx()-p2.getx();
double y=p1.gety()-p2.gety();
cout<< sqrt(x*x+y*y)<<endl;
}

int main()
{
cpoint p1(6,6);
cpoint p2(4,4);
display3(p1,p2);   //一般函数的调用
return 0;
}


运行结果:



知识点总结:一般函数不能使用类的私有成员,只能通过共有成员中的函数知道私有成员x和y的值。

学习心得:一般函数不能直接访问类的私有成员。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: