[YTU]_2922(Shape系列-8)
Description
小聪又想借用小强的Shape类了,但是不巧的是小强去考英语四级去了,但是小聪自力更生创建了Point类,但是他没有写Point类、继承Point类的Circle类、继承Circle类的Cylinder类这3个类的构造函数,请你帮忙补充这几个类未完成的函数。
小聪写的各种类
#include <iostream.h>
#define PI 3.14159
class Point
{
public:
Point(float=0,float=0);
void setPoint(float,float);
float getX()const ;
float getY()const ;
friend ostream & operator<<(ostream &,const Point &);
protected:
float x,y;
};
class Circle:public Point
{
public:
Circle(float x=0,float y=0,float r=0);
void setRadius(float);
float getRadius() const;
float area () const;
friend ostream &operator<<(ostream &,const Circle &);
protected:
float radius;
};
class Cylinder:public Circle
{
public:
Cylinder (float x=0,float y=0,float r=0,float h=0);
void setHeight(float);
float getHeight() const;
float area() const;
float volume() const;
friend ostream& operator<<(ostream&,const Cylinder&);
protected:
float height;
};
#define PI 3.14159
class Point
{
public:
Point(float=0,float=0);
void setPoint(float,float);
float getX()const ;
float getY()const ;
friend ostream & operator<<(ostream &,const Point &);
protected:
float x,y;
};
class Circle:public Point
{
public:
Circle(float x=0,float y=0,float r=0);
void setRadius(float);
float getRadius() const;
float area () const;
friend ostream &operator<<(ostream &,const Circle &);
protected:
float radius;
};
class Cylinder:public Circle
{
public:
Cylinder (float x=0,float y=0,float r=0,float h=0);
void setHeight(float);
float getHeight() const;
float area() const;
float volume() const;
friend ostream& operator<<(ostream&,const Cylinder&);
protected:
float height;
};
//小聪用于测试的函数
int main()
{
Cylinder cy1(3.5,6.4,5.2,10);
cy1.setHeight(15);
cy1.setRadius(7.5);
cy1.setPoint(5,5);
cout<<"new cylinder:\n"<<cy1;
Point &pRef=cy1;
cout<<"pRef as a point:"<<pRef;
Circle &cRef=cy1;
cout<<"cRef as a Circle:"<<cRef;
return 0;
}
{
Cylinder cy1(3.5,6.4,5.2,10);
cy1.setHeight(15);
cy1.setRadius(7.5);
cy1.setPoint(5,5);
cout<<"new cylinder:\n"<<cy1;
Point &pRef=cy1;
cout<<"pRef as a point:"<<pRef;
Circle &cRef=cy1;
cout<<"cRef as a Circle:"<<cRef;
return 0;
}
提示:不用提交全部程序,只提交补充部分。
Input
无
Output
输出小聪测试的各个数据
Sample Output
new cylinder: Center=[5,5], r=7.5, h=15 area=1060.29, volume=2650.72 pRef as a point:[5,5] cRef as a Circle:Center=[5,5], r=7.5, area=176.714#include <iostream> using namespace std; #define PI 3.14159 class Point { public: Point(float=0,float=0); void setPoint(float,float); float getX()const ; float getY()const ; friend ostream & operator<<(ostream &,const Point &); protected: float x,y; }; class Circle:public Point { public: Circle(float x=0,float y=0,float r=0); void setRadius(float); float getRadius() const; float area () const; friend ostream &operator<<(ostream &,const Circle &); protected: float radius; }; class Cylinder:public Circle { public: Cylinder (float x=0,float y=0,float r=0,float h=0); void setHeight(float); float getHeight() const; float area() const; float volume() const; friend ostream& operator<<(ostream&,const Cylinder&); protected: float height; };Point::Point(float a,float b){x=a;y=b;} float Point::getX()const {return x;} float Point::getY() const {return y;} void Point::setPoint(float xx,float yy) {x=xx;y=yy;} ostream &operator<<(ostream &cout,const Point &p) {cout<<"["<<p.x<<','<<p.y<<']'<<endl;return cout;} Circle::Circle(float x,float y,float r):Point(x,y),radius(r){} void Circle::setRadius(float rr) {radius=rr;} float Circle::getRadius() const {return radius;} float Circle::area() const {return PI*radius*radius;} ostream& operator<<(ostream&cout,const Circle &c) {cout<<"Center=["<<c.x<<','<<c.y<<"], r="<<c.radius<<", area="<<c.area()<<endl;return cout;} Cylinder::Cylinder(float x,float y,float r,float h):Circle(x,y,r),height(h){} void Cylinder::setHeight(float h) {height=h;} float Cylinder::getHeight()const {return height;} float Cylinder::area()const {return PI*radius*radius;} float Cylinder::volume()const {return PI*radius*radius*getHeight();} ostream & operator<<(ostream &cout,const Cylinder& c) {cout<<"Center=["<<c.getX()<<","<<c.getY()<<"], "<<"r="<<c.getRadius()<<", "<<"h="<<c.getHeight()<<endl <<"area="<<2*PI*c.radius*c.getHeight()+2*PI*c.radius*c.radius<<", volume="<<PI*c.radius*c.radius*c.getHeight()<<endl;return cout;} int main() { Cylinder cy1(3.5,6.4,5.2,10); cy1.setHeight(15); cy1.setRadius(7.5); cy1.setPoint(5,5); cout<<"new cylinder:\n"<<cy1; Point &pRef=cy1; cout<<"pRef as a point:"<<pRef; Circle &cRef=cy1; cout<<"cRef as a Circle:"<<cRef; return 0; }