點派生出線2

來源:互聯網
上載者:User
/** Copyright (c) 2013, 煙台大學電腦學院* All rights reserved.* 檔案名稱:test.cpp* 作者:邱學偉* 完成日期:2013 年 5 月 11 日* 版本號碼:v1.0* 輸入描述:無* 問題描述: 定義點類,並以點類為基類,派生出直線類,從基類中繼承的點的資訊表示直線的中點* 程式輸出:* 問題分析:* 演算法設計:略*/#include <iostream>#include <cmath>using namespace std;class Point{  public:  Point():x(0),y(0){};  Point(double x1,double y1)  {     x=x1;     y=y1;  }  double getx(){return x;}  double gety(){return y;}  void display();  private:  double x,y;};void Point::display(){    cout<<"Point:("<<x<<","<<y<<")"<<endl;}class Line:public Point{    public:    Line(Point p1,Point p2);    double Lengh();    void PrintLine();    void PrintPoint();    private:    class Point pts,pte;};Line::Line(Point p1,Point p2){        pts=p1;        pte=p2;}double Line::Lengh(){    double x0=pts.getx()-pte.getx();    double y0=pts.gety()-pte.gety();    return sqrt(x0*x0+y0*y0);}void Line::PrintLine(){    cout<<"端點為:"<<endl;    pts.display();    pte.display();    cout<<"長度:"<<Lengh()<<endl;}void Line::PrintPoint(){    cout<<"("<<(pts.getx()+pte.getx())/2<<","<<(pts.gety()+pte.gety())/2<<")"<<endl;}int main(){    Point pt(-2,5),pe(7,9);    Line l(pt,pe);    l.PrintLine();    cout<<"中點:"<<endl;    l.PrintPoint();    return 0;}

 

心得體會:這是自己敲出來的,跟老師的有不少不同之處;

/** Copyright (c) 2013, 煙台大學電腦學院* All rights reserved.* 檔案名稱:test.cpp* 作者:邱學偉* 完成日期:2013 年 5 月 11 日* 版本號碼:v1.0* 輸入描述:無* 問題描述: 定義點類,並以點類為基類,派生出直線類,從基類中繼承的點的資訊表示直線的中點* 程式輸出:* 問題分析:* 演算法設計:略*/#include <iostream>#include <cmath>using namespace std;class Point{  public:  Point():x(0),y(0){};  Point(double x1,double y1)  {     x=x1;     y=y1;  }  double getx(){return x;}  double gety(){return y;}  void display();  private:  double x,y;};void Point::display(){    cout<<"Point:("<<x<<","<<y<<")"<<endl;}class Line:public Point{    public:    Line(Point p1,Point p2);    double Lengh();    void PrintLine();    void PrintPoint();    private:    class Point pts,pte;};Line::Line(Point p1,Point p2):Point((p1.getx()+p2.getx())/2,(p1.gety()+p2.gety())/2){        pts=p1;        pte=p2;}double Line::Lengh(){    double x0=pts.getx()-pte.getx();    double y0=pts.gety()-pte.gety();    return sqrt(x0*x0+y0*y0);}void Line::PrintLine(){    cout<<"端點為:"<<endl;    pts.display();    pte.display();    cout<<"長度:"<<Lengh()<<endl;}void Line::PrintPoint(){    cout<<"("<<(pts.getx()+pte.getx())/2<<","<<(pts.gety()+pte.gety())/2<<")"<<endl;}int main(){    Point pt(-2,5),pe(7,9);    Line l(pt,pe);    l.PrintLine();    cout<<"\n The middle point of Line: ";    l.PrintPoint() ;//輸出直線l中點的資訊    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.