第七周项目一 成员函数,友元函数和一般函数的区别

/* 

 * Copyright(c)2016,烟台大学计算机与控制工程学院 

 * All rights reserved. 

 * 文件名称:第七周项目1:成员函数,友元函数和一般函数的区别

 * 作者:刘默涵

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

* 版本号:v1.0 

 * 

 * 问题描述:在互联网中使用的IP地址占4字节,可以用四段法表示,每段值的范围为0-255,中间用“.”隔开,例如202.194.116.97.其实,也可以看看一个有4字节的无符号整形值3401741409.现设计一个IP地址类,用于保存IP地址,并实施在IP地址上的一些操作。 

 * 输入描述: 

 * 程序输出: 

 */  

  1. using namespace std;  
  2. class CPoint  
  3. {  
  4. private:  
  5.     double x;  // 横坐标  
  6.     double y;  // 纵坐标  
  7. public:  
  8.     CPoint(double xx=0,double yy=0):x(xx),y(yy) {}  
  9.     void display1(CPoint &); //成员函数声明  
  10.     friend void display2(CPoint &,CPoint &);  //友元函数声明  
  11.     double getx()  
  12.     {  
  13.         return x;  
  14.     }  
  15.     double gety()  
  16.     {  
  17.         return y;  
  18.     }  
  19. };  
  20. void display3(CPoint &,CPoint &);  //一般函数声明  
  21. void CPoint::display1(CPoint &p)  //成员函数的实现,dispaly1前加CPoint::  
  22. {  
  23.     double d;  
  24.     d=sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));  
  25.     cout<<"两点间的距离为:"<<d<<endl;  
  26. }  
  27. void display2(CPoint &t1,CPoint &t2)  //友元函数的实现,dispaly2前不加Time::,并不是类的成员函数  
  28. {  
  29.     double d;  
  30.     d=sqrt((t1.x-t2.x)*(t1.x-t2.x)+(t1.y-t2.y)*(t1.y-t2.y));  
  31.     cout<<"两点间的距离为:"<<d<<endl;  
  32. }  
  33. void display3(CPoint &t1,CPoint &t2)  //一般函数的实现,dispaly1前加Time::  
  34. {  
  35.     double d;  
  36.     d=sqrt((t1.getx()-t2.getx())*(t1.getx()-t2.getx())+(t1.gety()-t2.gety())*(t1.gety()-t2.gety()));  
  37.     cout<<"两点间的距离为:"<<d<<endl;  
  38. }  
  39. int main()  
  40. {  
  41.     CPoint p1(10,13), p2(-5,6);  
  42.     p1.display1(p2);  
  43.     display2(p1,p2);  
  44.     display3(p1,p2);  
  45.     return 0;  
  46. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值