您的位置:首页 > 其它

2013第十一周上机任务【项目1 三角形类 直线类】

2013-05-10 10:48 483 查看
/*
* Copyright (c) 2013, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作者:樊露露
* 完成日期:2013 年 5 月1 0日
* 版本号: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) {};
void PrintPoint();//输出点的信息
double x,y;
};
void Point::PrintPoint()
{
cout<<"Point:("<<x<<","<<y<<")";
}

class Line:public Point //利用坐标点类定义直线类,由基类的数据成员表示直线的中点
{
public:
Line(Point pts,Point pte):pt1(pts),pt2(pte) {};//构造函数,用初始化直线的两个端点及由基类数据成员描述的中点
double Length();//计算并返回直线的长度
void PrintLine1();//输出直线的1个中点
void PrintLine2();//输出直线的2个端点
private:
class Point pt1,pt2;//直线的两个端点
};

double Line::Length()
{
double m;
m=sqrt((pt1.x-pt2.x)*(pt1.x-pt2.x)+(pt1.y-pt2.y)*(pt1.y-pt2.y));
return m;
}

void Line::PrintLine1()
{
Point ptm;
ptm.x=(pt1.x+pt2.x)/2;
ptm.y=(pt1.y+pt2.y)/2;
cout<<"("<<ptm.x<<","<<ptm.y<<")";
}
void Line::PrintLine2()
{
cout<<"("<<pt1.x<<","<<pt1.y<<")"<<"  "<<"("<<pt2.x<<","<<pt2.y<<")";
}
int main()
{
Point ps(-2,5),pe(7,9);
Line l(ps,pe);
double d;
d=l.Length();
cout<<"\n The length of Line:";
cout<<d<<endl;
cout<<"\n The endpoint of Line:";
l.PrintLine2();
cout<<"\n The middle point of Line:";
l.PrintLine1();
cout<<endl;
return 0;
}


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