1. 程式人生 > >第十一週上機專案4點類派生直線類

第十一週上機專案4點類派生直線類

/*
 *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;
}


執行結果: