Shape系列-8

Shape系列-8

时间: 1ms        内存:128M

描述:

小聪又想借用小强的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

提示:

参考答案(内存最优[1084]):

#include<stdio.h>
int main()
{
	printf("new cylinder:\nCenter=[5,5], r=7.5, h=15\narea=1060.29, volume=2650.72\npRef as a point:[5,5]\ncRef as a Circle:Center=[5,5], r=7.5, area=176.714");
}

参考答案(时间最优[0]):


#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;
};float Point::getX() const 
{
	return x;
}
 float Point::getY() const 
{
	  return y;
}
Point::Point(float a,float b)
{
	x=a;y=b;
}
void Point::setPoint(float a,float b)
{
	x=a;y=b;
}
ostream & operator<<(ostream &output,const Point &p)
{
	output<<"["<<p.x<<","<<p.y<<"]"<<endl;
	return output;
}

Circle::Circle(float a,float b,float r):Point(a,b),radius(r){}

void Circle::setRadius(float r)
{
	radius=r;
}

float Circle::getRadius() const 
{
	return radius;
}

float Circle::area() const
{
	return PI*radius*radius;
}

ostream &operator<<(ostream &output,const Circle &c)
{
	output<<"Center=["<<c.x<<","<<c.y<<"], r="<<c.radius<<", area="<<c.area()<<endl;
	return output;
}

Cylinder::Cylinder(float a,float b,float r,float h)
    :Circle(a,b,r),height(h){}

void Cylinder::setHeight(float h)
{
	height=h;
}

float Cylinder::getHeight() const 
{
	return height;
}

float Cylinder::area() const
{ 
	return 2*Circle::area()+2*PI*radius*height;
}

 float  Cylinder:: volume() const
{
	 return Circle::area()*height;
 }

ostream &operator<<(ostream &output,const Cylinder& cy)
{
	output<<"Center=["<<cy.x<<","<<cy.y<<"], r="<<cy.radius<<", h="<<cy.height
       <<"\narea="<<cy.area()<<", volume="<<cy.volume()<<endl;
	return output;
}
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;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注