/*
* 程序的版权和版本声明部分
* Copyright (c)2012, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称: 1.cpp
* 作者:李慧慧
* 完成日期: 2013年 5 月 20日
* 版本号: v1.0
* 输入描述:无
* 问题描述:
* 程序输出:
*/
#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(); //输出点的信息
private:
double x,y; //点的横坐标和纵坐标
};
void Point::PrintPoint()
{
cout<<"Point:("<<x<<","<<y<<") ";
}
class Line: public Point //利用坐标点类定义直线类, 其基类的数据成员表示直线的中点
{
public:
Line(Point pt1, Point pt2):pt1(pt1),pt2(pt2){}; //构造函数,用初始化直线的两个端点及由基类数据成员描述的中点
double Length();
void PrintLine(); //输出直线的两个端点和直线长度
private:
class Point pt1, pt2; //直线的两个端点
};
double Line::Length()
{
double s,a,b;
a=(pt1.getx()-pt2.getx())*(pt1.getx()-pt2.getx());
b=(pt1.gety()-pt2.gety())*(pt1.gety()-pt2.gety());
s=sqrt(a+b);
return s;
}
void Line::PrintLine()
{
cout<<"直线的两个端点为:"<<endl;
pt1.PrintPoint();
pt2.PrintPoint();
cout<<endl;
cout<<"直线的长度为:"<<Length()<<endl;
}
int main()
{
Point ps(-2,5),pe(7,9);
Line l(ps,pe);
//下面输出直线l 的端点、长度和、中点的信息
l.PrintLine();
cout<<"中点坐标为:"<<"("<<(ps.getx()+pe.getx())/2.0<<","<<(ps.gety()+pe.gety())/2.0<<")";
return 0;
}
运行结果: