/*
* Copyright (c) 2011, 煙臺大學計算機學院
* All rights reserved.
* 作 者:王靜
* 完成日期: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 showx(){return x;}
double showy(){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); //構造函數,用初始化直線的兩個端點及由基類數據成員描述的中點
double Length(); //計算並返回直線的長度
void PrintLine(); //輸出直線的兩個端點和直線長度
private:
class Point pts,pte; //直線的兩個端點
};
//下面定義Line類的成員函數
Line::Line(Point pt1, Point pt2):Point((pt1.showx()+pt2.showx())/2,(pt1.showy()+pt2.showy())/2) //構造函數,用初始化直線的兩個端點及由基類數據成員描述的中點
{
pts=pt1;
pte=pt2;
}
double Line::Length() //計算並返回直線的長度
{
double a,b,dis;
a=pts.showx()-pte.showx();
b=pts.showy()-pte.showy();
dis=sqrt(a*a+b*b);
return dis;
}
void Line::PrintLine() //輸出直線的兩個端點和直線長度
{
cout<<"直線的兩個端點爲"<<endl;
pts.PrintPoint();
pte.PrintPoint();
cout<<"直線長度爲"<<Length();
}
int main()
{
Point ps(-2,5),pe(7,9);
Line l(ps,pe);
l.PrintLine();//輸出直線l的信息(請補全代碼)
cout<<"\n The middle point of Line: ";
l.PrintPoint();//輸出直線l中點的信息(請補全代碼)
return 0;
}
運行結果: