第七周项目1-一般函数

来源:互联网 发布:数据结构与算法笔试题 编辑:程序博客网 时间:2024/04/27 15:39



 
问题及代码:

/*  *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的值。
 
 
 
学习心得:一般函数不能直接访问类的私有成员。

0 0
原创粉丝点击