天天看點

15第十一周項目三——點類派生直線類

#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 get_X();
    double get_Y();
protected:
    double x,y;   //點的橫坐标和縱坐标
};
void Point::PrintPoint()
{
    cout<<"Point: ("<<x<<","<<y<<")"<<endl;    //輸出點
}
class Line: public Point   //利用坐标點類定義直線類, 其基類的資料成員表示直線的中點
{
public:
    Line(Point pts, Point pte); //構造函數,用初始化直線的兩個端點及由基類資料成員描述的中點
    double Length();    //計算并傳回直線的長度
    void PrintLine();   //輸出直線的兩個端點和直線長度
private:
    class Point pts,pte;   //直線的兩個端點,從Point類繼承的資料成員表示直線的中點
};

double Point::get_X()
{
    return x;
}

double Point::get_Y()
{
    return y;
}

Line::Line(Point p1,Point p2):Point((p1.get_X()+p2.get_X())/2,(p1.get_Y()+p2.get_Y())/2)
{
    pts=p1;
    pte=p2;
}
double Line::Length()
{
    double d1,d2,d;
    d1=pts.get_X()-pte.get_X();
    d2=pts.get_Y()-pte.get_Y();
    d=sqrt(d1*d1+d2*d2);
    return d;
}

void Line::PrintLine()
{
    cout<<" 1st:";
    pts.PrintPoint();
    cout<< "2nd:";
    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();  //輸出直線l的資訊:兩端點及長度
    cout<<"The middle point of Line is: ";
    l.PrintPoint(); //輸出直線l中點的資訊
    return 0;
}
           
15第十一周項目三——點類派生直線類

學習總結:感覺好事不太熟練,思路時好時壞,這樣真的不好!!!