您的位置:首页 > 其它

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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: