- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
设计要求实现如下功能:
Shape类,Rectangle类, Cube类,Circle类, Clinder类,这些类之间的继承关系如下图所示。
编程序完成:
,至少包含下列表的功能.
类名 数据成员 public float area() public float perimeter() public void show()
(功能:显示图形的参数) public void getName)
(功能:显示图形名) Shape 无 public float area()=0 float perimeter ()=0 void show()=0
void getName()=0 Rectangle Private x,y,len,wid len*wid 2*(len+wid) [x,y],
length=len,
width=wid 显示矩形 Cube Private h 侧面积+底面积 4*(len+wid)+2*h [x,y], length=len,
width=wid
height=h 显示长方体 Circle Privae x,y,r П*r*r П*r+2 Center=[x,y],
redius=r 显示圆 Clinder Private h 侧面积+底面积 2*(П*r+2)+2h Center=[x,y],
redius=r
Height=h 显示圆柱体 2. 实现一个非成员函数total,函数原形如下所示.。功能是求n个图形的面积之和.
float total(Shape *p[],int n)
{
…… //求n个图形的面积之和并作为函数值返回
}
3.在main() 中,定义菜单如下:
请书写代码完成各项菜单功能.
提示:
(1) 通过菜单选项,分别将各个图形对象存放在数组中 Shape* array[10];
例如,生成一圆对象后放入数组中
int count=0;
Circle c1= Circle(x,y,r)
array[count++]= c1;
(2) 显示已经创建的图形对象
for (I=0;Icount;I++) {
array[I]-show();
array[I]-getName();
…
}
(3) 调用函数total,求各个图形对象的面积之和并显示结果
#includeiostream
using namespace std;
const double pi=3.1415926;
class Shape
{public:
virtual double area()=0;//面积
virtual double perimeter()=0;//平面求周长,立方体求体积
virtual void show()=0;
virtual void getName()=0;};
class Rectangle:public Shape
{
private:
double x,y,length,width;
public:
Rectangle(double X,double Y,double len,double wid){x=X;y=Y;length=len;width=wid;}
double area(){return length*width;};
double perimeter(){return 2*(length+width);};
double getLength(){return length;};
double getWidth(){return width;};
void show(){coutcenter=(x,y)endl;
coutlength=length,width=widthendl;};
void getName(){cout矩形endl;};};
class Cube:public Rectangle
{
private:
double height;
public:
Cube(double X,double Y,double len,double wid,double H):Rectangle(X,Y,len,wid){height=H;}
double area(){return 2*(getLength()*getWidth()+getLength()*height+getWidth()*height);};
double perimete
文档评论(0)