第十周C++实验报告(一)

来源:互联网 发布:mac粉色系口红 编辑:程序博客网 时间:2024/06/04 17:41
  1. #include<iostream>    
  2. #include<Cmath>    
  3. using namespace std;    
  4. class Point //定义坐标点类    
  5. {    
  6. public:    
  7.     double x,y;   //点的横坐标和纵坐标    
  8.     Point(){x=0;y=0;}    
  9.     Point(double x0,double y0) {x=x0; y=y0;}     
  10.     void PrintP(){cout<<" Point:("<<x<<","<<y<<")";}    
  11. };  
  12.   
  13. class Line: public Point   //利用坐标点类定义直线类, 其基类的数据成员表示直线的中点    
  14. {    
  15. private:    
  16.     class Point pt1,pt2;   //直线的两个端点    
  17. public:    
  18.     Line(Point pts, Point pte);  //构造函数    
  19.     double Dx(){return pt2.x-pt1.x;}    
  20.     double Dy(){return pt2.y-pt1.y;}    
  21.     double Length();//计算直线的长度    
  22.     void PrintL();  //输出直线的两个端点和直线长度    
  23. };    
  24. //构造函数,分别用参数初始化对应的端点及由基类属性描述的中点    
  25. Line::Line(Point pts, Point pte)    
  26. {    
  27.     pt1=pts;    
  28.     pt2=pte;    
  29.     x=Dx()/2;  
  30.     y=Dy()/2;  
  31. }  
  32.   
  33. double Line::Length(){return sqrt(Dx()*Dx()+Dy()*Dy());};//计算直线的长度  
  34.   
  35. void Line::PrintL()    
  36. {    
  37.     cout<<" 1st ";    
  38.     pt1.PrintP();    
  39.     cout<<"\n 2nd ";    
  40.     pt2.PrintP();    
  41.     cout<<"\n The middle point of Line: ";    
  42.     PrintP();    
  43.     cout<<"\n The Length of Line: "<<Length()<<endl;    
  44. }    
  45. int main()    
  46. {    
  47.     Point ps(-2,5),pe(7,9);    
  48.     Line l(ps,pe);    
  49.     l.PrintL(); //输出直线l的信息    
  50.     l.PrintP();//输出直线l中点的信息    
  51.     cout<<endl;    
  52.     system("pause");    
  53.     return 0;    
  54. }  
原创粉丝点击