编程小练习

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


  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 CPoint  
  13. {  
  14. public:  
  15.    CPoint(double xx=0,double yy=0):x(xx),y(yy){}  
  16.    double getX(){return x;}  
  17.    double getY(){return y;}  
  18. private:  
  19.     double x;//横坐标  
  20.     double y;//纵坐标  
  21. };  
  22. class Line  
  23. {  
  24. public:  
  25.     Line(CPoint p1,CPoint p2);  
  26.     void len1();  
  27. private:  
  28.     CPoint p1,p2;  
  29.     double len;  
  30. };  
  31. Line::Line(CPoint xp1,CPoint xp2):p1(xp1),p2(xp2)  
  32. {  
  33.     double x=p1.getX()-p2.getX();  
  34.     double y=p1.getY()-p2.getY();  
  35.     len=(double)sqrt(x*x+y*y);  
  36. }  
  37. void Line::len1()//成员函数len1()的实现,len1前加 Line  
  38. {  
  39.     cout<<"The distance is: "<<len<<endl;  
  40. }  
  41. int main()  
  42. {  
  43.     CPoint myp1(1.0,1.0),myp2(4.0,5.0);  
  44.     Line line(myp1,myp2);  
  45.     line.len1();  
  46.     return 0;  
  47. }
0 0
原创粉丝点击