點派生出線1

來源:互聯網
上載者:User
/* * Copyright (c) 2013, 煙台大學電腦學院                     * All rights reserved.                     * 檔案名稱:test.cpp                     * 作者:邱學偉                    * 完成日期:2013 年 5 月 10 日                     * 版本號碼: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 pt1, Point pt2); //建構函式,初始化直線的兩個端點及由基類資料成員描述的中點    double Length();    //計算並返回直線的長度    void PrintLine();  //輸出直線的兩個端點和直線長度    void PrintPoint();private:    class Point pts,pte;   //直線的兩個端點};//下面定義Line類的成員函數Line::Line(Point pt1,Point pt2){    pts=pt1;    pte=pt2;}double Line::Length(){    double d;    d=sqrt((pts.x-pte.x)*(pts.x-pte.x)-(pts.y-pts.y)*(pts.y-pts.y));    cout<<"長度:"<<d;    return 0;}void Line::PrintPoint(){    cout<<"Point:("<<(pts.x+pte.x)/2<<","<<(pts.y+pte.y)/2<<")"<<endl;}void Line::PrintLine(){    Point p1,p2;    p1=pte;    p2=pts;    cout<<"端點:"<<endl;    p1.PrintPoint();    cout<<" ";    p2.PrintPoint();}int main(){    Point ps(-2,5),pe(7,9);    Line l(ps,pe);    l.PrintLine();    cout<<endl;    l.Length();//輸出直線l的資訊(請補全代碼)    cout<<endl;    cout<<"The middle point of Line: ";    l.PrintPoint();//輸出直線l中點的資訊(請補全代碼)}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.