编程小练习

来源:互联网 发布:微信h5棋牌源码 编辑:程序博客网 时间:2024/06/08 16:01

  1. /*Copyright (c)2016,烟台大学计算机与控制工程学院 
  2.  *All rights reserved. 
  3.  *文件名称:main.cpp 
  4.  *作    者:李落才
  5.  *完成日期:2016年4月11日 
  6.  * 
  7.  *问题描述:两点间距离之友元函数 
  8.  */  
  9. #include <iostream>  
  10. #include<cmath>  
  11. using namespace std;  
  12. class Point  
  13. {  
  14. public:  
  15.     Point(int x=0,int y=0):x(x),y(y){}  
  16.     int getX(){return x;}  
  17.     int getY(){return y;}  
  18.     friend float dist(Point &p1,Point &p2);//dist是友元函数  
  19. private:  
  20.     int x,y;  
  21. };  
  22. float dist(Point &p1,Point &p2)  //友元函数dist1的实现,不加Time::,友元并不是类的成员  
  23. {  
  24.     double x=p1.x-p2.x;  
  25.     double y=p1.y-p2.y;  
  26.     return static_cast<float>(sqrt(x*x+y*y));  
  27. }  
  28. int main()  
  29. {  
  30.     Point myp1(1,1),myp2(4,5);  
  31.     cout<<"The distance is: ";  
  32.     cout<<dist(myp1,myp2)<<endl;  
  33.     return 0;  
  34. }
0 0