Point类模板派生方式生成Line类(二)
2018-01-28 13:18
127 查看
#include<iostream> using namespace std; template <class T> class Point { public: Point() = default; Point(T x, T y); T Distance(Point &point); void Display(void); void set_point(T x, T y); T get_x() { return point_x; } T get_y() { return point_y; } private: T point_x, point_y; T distance; }; template<class T> Point<T>::Point(T x, T y) { point_x = x; point_y = y; } template<class T> T Point<T>::Distance(Point &point) { distance = sqrt(pow(point_x - point.point_x,2) + pow(point_y - point.point_y,2)); return distance; } template<class T> void Point<T>::set_point(T x, T y) { point_x = x; point_y = y; } template<class T> void Point<T>::Display(void) { cout << "两点之间的距离:" << distance << endl; } template<class T> class Line :public Point<T> //继承Point中的public成员 { public: Line() = default; Line(float x,float y) { this->set_point(x, y); } void Display(void); void clcu_distance(Line&line1, Line&line2); private: T distance; }; template<class T> void Line<T>::Display() { cout << "两点之间的距离" << distance << endl; } template<class T> void Line<T>::clcu_distance(Line&line1,Line&line2) { distance = sqrt(pow(line1.get_x()-line2.get_x(),2)+ pow(line1.get_y() - line2.get_y(), 2)); } void main() { Point<float> a; Point<float> b(10.2, 9.7), c(12.9, 15.6) //构造函数在声明的时候就会被调用 a = c; cout << "两点之间的距离" << a.Distance(b) << endl; Line<float> M(10.2, 9.7), N(12.9, 15.6); Line<float> L; L.clcu_distance(M,N); L.Display(); }
相关文章推荐
- Point类组合方式生成Line类(一)
- 如何正确规范模板解释生成方式
- Itext生成PDF文档页脚问题的处理,是采用HeaderFooter还是采用模板的方式
- java利用Freemarker模板生成格式友好的doc文档(这种方式不支持docx)
- POI 使用替换字符方式进行模板生成word
- 最小生成树模板
- 简单的方式由数据库的物理表(TABLE)生成实体类(DTO)
- 自动生成Python初始模板——makecode.py增强版
- VS2013修改MVC4默认生成的模板
- 从JSP,ASP等动态页面生成静态页面的实现方式
- ASP.NET多种方式生成高质量缩略图
- 洛谷 P3366 【模板】最小生成树
- VC 6下word操作心得二---以.dot模板生成.doc文档
- 安卓开发-Get和Post两种请求三种方式(模板)
- [模板]最小生成树 kruskal算法
- 16个HTML5 框架、模板以及生成工具
- 【ssm】mybatis逆向工程生成的模板方法使用小结
- P3366 【模板】最小生成树
- nginx https证书生成方式
- poj 1258 Agri-Net(最小生成树模板代码)