YTU 2922: Shape系列-8

2922: Shape系列-8

时间限制: 1 Sec  内存限制: 128 MB
提交: 172  解决: 99

题目描述

小聪又想借用小强的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;
};
//小聪用于测试的函数
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;
}
提示:不用提交全部程序,只提交补充部分。
 

输入

输出

输出小聪测试的各个数据

样例输出

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

im0qianqian_站在回忆的河边看着摇晃的渡船终年无声地摆渡,它们就这样安静地画下黄昏画下清晨......可怜

#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;
};
Cylinder::Cylinder(float x,float y,float r,float h)
{
    height=h;
    radius=r;
    this->y=y;
    this->x=x;
}
Circle::Circle(float x,float y,float r) {}
void Cylinder::setHeight(float a)
{
    height=a;
}
Point::Point(float a,float b) {}
void Circle::setRadius(float a)
{
    radius=a;
}
void Point::setPoint(float a,float b)
{
    x=a;
    y=b;
}
ostream & operator<<(ostream &a,const Point &b)
{
    a<<"["<<b.x<<","<<b.y<<"]"<<endl;
    return a;
}
ostream &operator<<(ostream &a,const Circle &b)
{
    a<<"Center=["<<b.x<<","<<b.y<<"], r="<<b.radius<<", area="<<b.radius*b.radius*PI<<endl;
    return a;
}
ostream& operator<<(ostream &a,const Cylinder &b)
{
    a<<"Center=["<<b.x<<","<<b.y<<"], r="<<b.radius<<", h="<<b.height<<endl;
    a<<"area="<<2*PI*b.radius*b.radius+2*PI*b.radius*b.height<<", volume="<<b.radius*b.radius*PI*b.height<<endl;
    return a;
}
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;
}


posted @ 2016-01-11 15:50  小坏蛋_千千  阅读(183)  评论(0编辑  收藏  举报