/*
* 程序的版权和版本声明部分
* Copyright (c)2014, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称:a.cpp
* 作 者:孔云
* 完成日期:2014年5月11日
* 版 本 号: v1.0
* 输入描述:主函数已给出。
* 问题描述:定义点类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) {}
void PrintPoint(); //输出点的信息
double getX();
double getY();
protected:
double x,y; //点的横坐标和纵坐标
};
double Point::getX()
{
return x;
}
double Point::getY()
{
return y;
}
void Point::PrintPoint()
{
cout<<"Point: ("<<x<<","<<y<<")"; //输出点
}
class Line: public Point //利用坐标点类定义线段类, 其基类的数据成员表示线段的中点
{
public:
Line(Point pts, Point pte); //构造函数,用初始化线段的两个端点及由基类数据成员描述的中点
double Length(); //计算并返回线段的长度
void PrintLine(); //输出线段的两个端点和线段长度
private:
class Point pts,pte; //线段的两个端点,从Point类继承的数据成员表示线段的中点
};
Line::Line(Point ps, Point pe):Point((ps.getX()+pe.getX())/2,(ps.getY()+pe.getY())/2)
{
pts=ps;
pte=pe;
}
double Line::Length()
{
double x1=pts.getX()-pte.getX();
double y1=pts.getY()-pte.getY();
return sqrt(x1*x1+y1*y1);
}
void Line::PrintLine()
{
cout<<"线段的端点:"<<"("<<pts.getX()<<","<<pts.getY()<<")"<<'\t'<<"("<<pte.getX()<<","<<pte.getY()<<")"<<endl;
cout<<"线段的长度:"<<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;
}

心得体会:这个程序很有趣,忽略了x,y的类型,忘记了它们的调用需要接口函数哦
,继续加油吧