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