01./* 02.* 程式的著作權和版本聲明部分 03.* Copyright (c)2013, 煙台大學電腦學院學生 04.* All rightsreserved. 05.* 檔案名稱: point.cpp 06.* 作 者:趙冠哲 07* 完成日期:2013年5月9日 08.* 版本號碼: v1.0 09.* 輸入描述: 10.* 問題描述: 11.*/#include<iostream.h>#include<Cmath>#define PI 3.1415926//定義符號常量class Point //定義座標點類{private: double x;//橫座標 double y;//縱座標public: Point(double x0,double y0) {x=x0; y=y0;} ~Point () {cout<<"Destructor called"<<endl;} double get_x(){return x;} //公用基類的私人資料成員在衍生類別中不能訪問,需要擷取私人資料成員的公用介面 double get_y(){return y;} friend ostream &operator << (ostream &, Point &);//聲明運算子"<<"的重載};//利用座標點類定義圓類, 其基類的資料成員表示圓的中心class Circle: public Point{private: double d;public: Circle(double xx,double yy,double dd): Point(xx,yy) { d=dd; } ~Circle() { } friend ostream &operator << (ostream &, Circle &); double get_d(){return d;}};class Cylinder: public Circle{private: double h;public: Cylinder(double xx,double yy,double dd,double hh): Circle (xx,yy,dd) { h=hh; } ~Cylinder() { } friend ostream &operator << (ostream &,Cylinder &); double get_h(){return h;} double superficial_area();//表面積 double volume();//圓柱體的體積};ostream &operator << (ostream & output, Point & c){ output<<"點的橫座標為:"<<c.x<<endl; output<<"點的縱座標為:"<<c.y<<endl; return output;}ostream &operator << (ostream & output, Circle & c){ output<<"圓的半徑為:"<<c.get_d()<<endl; output<<"圓的圓心為"<<"("<<c.get_x()<<","<<c.get_y()<<")"<<endl; return output;}ostream &operator << (ostream & output,Cylinder & c){ output<<"圓柱的高為:"<<c.get_h()<<endl; output<<"圓柱底面圓的半徑為:"<<c.get_d()<<endl; output<<"圓柱底面圓的圓心為"<<"("<<c.get_x()<<","<<c.get_y()<<")"<<endl; return output;}//圓柱體的表面積double Cylinder::superficial_area(){ double s=2*PI*get_d()*get_d()+2*PI*get_d()*get_h(); return s;}//圓柱體的體積double Cylinder::volume(){ double v=PI*get_d()*get_d()*get_h(); return v;}int main(){ Point p(1,1); cout<<p; Circle ci(1,2,3); cout<<ci; Cylinder cy(1,2,4,6); cout<<cy; cout<<"圓柱的體積為:"<<cy.volume ()<<endl; cout<<"圓柱的表面積為:"<<cy.superficial_area ()<<endl; return 0;}
運行結果: