第七周C++实验报告(2)

来源:互联网 发布:知乎数据挖掘考研 编辑:程序博客网 时间:2024/06/06 20:58
  1. #include<iostream>  
  2. #include<Cmath>  
  3. using namespace std;  
  4. class CPoint    
  5. {  
  6. private:  
  7.     double x;  // 横坐标   
  8.     double y;  // 纵坐标    
  9. public:  
  10.     CPoint(double xx=0, double yy=0):x(xx),y(yy){}  
  11.     double distance1(const CPoint &p);  
  12.     friend double distance2(const CPoint &p1,const CPoint &p2);  
  13.     double getx() const {return x;}  
  14.     double gety() const {return y;}  
  15. };  
  16.   
  17. double distance3(const CPoint &p1,const CPoint &p2);  
  18.   
  19. double CPoint::distance1(const CPoint &p)  
  20. {  
  21.     double dx=this->x-p.x;  
  22.     double dy=this->y-p.y;  
  23.     return sqrt(dx*dx+dy*dy);  
  24. }  
  25.   
  26. double distance2(const CPoint &p1,const CPoint &p2)  
  27. {  
  28.     double dx=p1.x-p2.x;  
  29.     double dy=p1.y-p2.y;  
  30.     return sqrt(dx*dx+dy*dy);  
  31. }  
  32.   
  33. double distance3(const CPoint &p1,const CPoint &p2)  
  34. {  
  35.     double dx=p1.getx()-p2.getx();  
  36.     double dy=p1.gety()-p2.gety();  
  37.     return sqrt(dx*dx+dy*dy);  
  38. }  
  39.   
  40. int main()  
  41. {  
  42.     CPoint p1(2,4);  
  43.     CPoint p2(3,7);  
  44.     cout<<p2.distance1(p1)<<endl;  
  45.     cout<<distance2(p1,p2)<<endl;  
  46.     cout<<distance3(p1,p2)<<endl;  
  47.     system("pause");  
  48.     return 0;  



原创粉丝点击