第十周任务三

来源:互联网 发布:淘宝商标侵权被告 编辑:程序博客网 时间:2024/06/06 06:41
#include <iostream>                          //点类using namespace std;const double pi = 3.1415926;class Point{protected:double x;double y;public:Point() {x = 0;  y = 0;}Point (double x0,double y0):x(x0),y(y0){}~Point(){}double Getx(){cin >> x;return x;}double Gety(){cin >> y;return y;}friend ostream & operator<<(ostream &,Point &);};class Circle:public Point                  //圆类{protected:double r;public:Circle() {x = 0; y = 0; r = 0;}Circle(double x0,double y0,double r0):Point( x0,y0),r(r0){}double Getr(){cin >> r;return r;}double area();friend ostream & operator<<(ostream &,Circle &);}; class Cylinder:public Circle                         //圆柱类{private:double h;public:Cylinder() { x = 0; y = 0; r = 0; h = 0;}Cylinder(double x0,double y0,double r0,double h0):Circle( x0,y0,r0),h(h0){}double Geth(){cin >> h;return h;}double areaCylinder();double volumeCylinder();friend ostream & operator<<(ostream &, Cylinder&);};ostream & operator<<(ostream &output, Point &p)             //输出流函数定义{      output<<"("<<p.x<<","<<p.y<<")"<<endl;      return output;  }  ostream &operator<<(ostream &output, Circle &p)              //输出流函数定义{      output<<"Center=("<<p.x<<", "<<p.y<<"), r="<<p.r<<", area="<<p.area( )<<endl;      return output;  }  ostream &operator<<(ostream &output, Cylinder& p)              //输出流函数定义{      output<<"Center=("<<p.x<<","<<p.y<<"), r="<<p.r<<", h="<<p.h          <<"area="<<p.areaCylinder( )<<", volume="<<p.volumeCylinder( )<<endl;      return output;  } double Circle::area()  {                                                                       //圆的面积函数定义     return (pi * r * r);}double Cylinder::areaCylinder()                          //圆柱的面积函数定义{return (2 * Circle::area() + 2 * pi * r * h);}double Cylinder::volumeCylinder()                        //圆柱的体积函数定义{return (Circle::area() * h);}int main()                                                 //主函数定义{Cylinder c1(3,4,5,6),c2;                               //定义圆柱对象cout << "请输入相关数值:(x,y,r,h)" << endl;c2.Getx();c2.Gety();c2.Geth();c2.Getr();cout << "area1=" << c1.areaCylinder() << endl;cout << "volume1=" << c1.volumeCylinder() << endl << endl;cout << "area2=" << c2.areaCylinder() << endl;cout << "volume2=" << c2.volumeCylinder() << endl;system("pause");return 0;}
	
				
		
原创粉丝点击