第十一周上机实践项目——点类派生直线类
2014-05-11 21:21
309 查看
/* *程序的版权和版本声明部分: *Copyright(c)2013,烟台大学计算机学院学生 *All rights reserved. *文件名称:点类派生直线类 *作者:刘中林 *完成日期:2014年5月11日 *版本号:v0.1 *对任务及求解方法的描述部分: *输入描述:自定义所需的信息 *问题描述:定义点类Point,并以点类为基类,派生出直线类Line,从基类中继承的点的信息表示直线的中点。 *程序输入:自定义所需的信息 *程序输出: *问题分析:无 *算法设计:派生类 *我的程序: */ #include<iostream> #include<Cmath> using namespace std; class Point //定义坐标点类 { public: Point():x(0),y(0) {}; Point(double x0, double y0):x(x0), y(y0){} double getx() { return x; } double gety() { return y; } void PrintPoint(); //输出点的信息 protected: double x,y; //点的横坐标和纵坐标 }; void Point::PrintPoint() { cout<<"Point: ("<<x<<","<<y<<")"; //输出点 } class Line: public Point //利用坐标点类定义直线类, 其基类的数据成员表示直线的中点 { public: Line(Point pts1, Point pte2):Point((pts1.getx()+pte2.getx())/2,(pts1.gety()+pte2.gety())/2) //构造函数,用初始化直线的两个端点及由基类数据成员描述的中点 { pts=pts1; pte=pte2; } double Length(); //计算并返回直线的长度 void PrintLine(); //输出直线的两个端点和直线长度 private: class Point pts,pte; //直线的两个端点,从Point类继承的数据成员表示直线的中点 }; double Line::Length() { double dx = pts.getx() - pte.getx(); double dy =pts.gety() - pte.gety(); return sqrt(dx*dx+dy*dy); } void Line::PrintLine() { cout<<" first: "<<endl; pts.PrintPoint(); cout<<endl; cout<<" senond: "<<endl; pte.PrintPoint(); cout<<endl; cout<<" The Length of Line: "<<Length()<<endl; } int main() { Point ps(-2,5),pe(7,9); Line l(ps,pe); cout<<"About the Line: "<<endl; l.PrintLine(); //输出直线l的信息:两端点及长度 cout<<"The middle point of Line is: "; l.PrintPoint(); //输出直线l中点的信息 return 0; }
*样例输出:
*心得体会:不知不觉、又一年。。
相关文章推荐
- 第十一周上机实践项目3——点类派生直线类
- 第十一周上机项目三点类派生直线类
- 第十一周上机项目4点类派生直线类
- 第十一周 课后实践:项目三——点类派生直线类
- 第十一周项目点类派生直线类
- 第十一周项目三 点类派生直线类
- 第十一周上机实践项目——职员有薪水了(拓展)
- 第十一周上机实践项目3(1):警察和厨师
- 第十一周 项目 3 点类派生直线类
- 第十一周上机实践项目——项目3-警察和厨师-(2)
- 第十一周 项目3 - 点类派生直线类
- 第十一周项目三-点类派生直线类
- 第十周第十一周上机实践项目-项目1-3-点-圆-圆柱类族的设计
- 第十一周上机实践项目 项目3-警察和厨师(2)
- 第十一周项目二——点类派生直线类
- 第十一周项目三点类派生直线类
- 第十一周项目3-点类派生直线类
- 第十一周上机实践—项目1(4)—哈夫曼编码的算法验证
- 第十一周上机实践项目——项目1-点-圆-圆柱类族的设计
- 第十一周上机实践项目——正三角形