您的位置:首页 > 其它

第十一周上机项目4点类派生直线类

2015-05-20 08:47 363 查看
/*
*Copyright (c) 2015, 烟台大学计算机学院
*All rights reserved.
*文件名称:text.cpp
*作者:陈栋梁
*完成日期:2015年 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();
protected:
double x,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;
};
Line::Line(Point pt1, Point pt2):Point((pt1.getX()+pt2.getX())/2,(pt1.getY()+pt2.getY())/2)
{
pts=pt1;
pte=pt2;
}
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<<" 1st "<<endl;
pts.PrintPoint();
cout<<" 2nd "<<endl;
pte.PrintPoint();
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();
cout<<"The middle point of Line is: ";
l.PrintPoint();
return 0;
}


运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: