第十周實驗報告一

來源:互聯網
上載者:User

<pre class="plain" name="code">/* (程式頭部注釋開始)  

* 程式的著作權和版本聲明部分  

* Copyright (c) 2011, 煙台大學電腦學院學生   

* All rights reserved.  

* 檔案名稱:    C++第十周【任務1】                          

* 作    者:鮑增凱                       

* 完成日期:   2012      年   5    月   14日

* 版 本 號:  10-1-1          

* 對任務及求解方法的描述部分  

* 輸入描述:   在類的定義中,填充函數功能

* 問題描述:在下面一段類的定義中,需要填充的函數由注釋內容給出了功能。  

* 程式輸出:   

* 程式頭部的注釋結束  

*/  

</pre><p></p><pre class="cpp" name="code">#include<iostream>    #include<Cmath>    using namespace std;    class Point //定義座標點類    {    public:        double x,y;   //點的橫座標和縱座標        Point(){x=0;y=0;}        Point(double x0,double y0) {x=x0; y=y0;}         void PrintP(){cout<<" Point:("<<x<<","<<y<<")";}    };      class Line: public Point   //利用座標點類定義直線類, 其基類的資料成員表示直線的中點    {    private:        class Point pt1,pt2;   //直線的兩個端點        public:            Line(Point pts, Point pte);  //建構函式           double Dx(){return pt2.x-pt1.x;}            double Dy(){return pt2.y-pt1.y;}            double Length();//計算直線的長度            void PrintL();  //輸出直線的兩個端點和直線長度    };    //建構函式,分別用參數初始化對應的端點及由基類屬性描述的中點    Line::Line(Point pts, Point pte):Point(((pte.x,pts.x)/2),((pte.y,pts.y)/2))    {       pt1=pts;        pt2=pte;        /*  if(pt1.x>pt2.x)     point((-Dx()/2),(-Dy()/2));      else      point((Dx()/2),(Dy()/2));*/    }    double Line::Length(){return sqrt(Dx()*Dx()+Dy()*Dy());};//計算直線的長度    void Line::PrintL()    {        cout<<" 1st ";        pt1.PrintP();       cout<<"\n 2nd ";       pt2.PrintP();        cout<<"\n The middle point of Line: ";        PrintP();        cout<<"\n The Length of Line: "<<Length()<<endl;    }    int main()    {        Point ps(-2,5),pe(7,9);        Line l(ps,pe);        l.PrintL(); //輸出直線l的資訊        l.PrintP();//輸出直線l中點的資訊        cout<<endl;        system("pause");        return 0;    } 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.