求助:C++编程题

来源:百度知道 编辑:UC知道 时间:2024/06/23 18:07:07
下列shape类是一个表示形状的抽象类,area( )为求图形面积的函数,total( )则是一个通用的用以求不同形状的图形面积总和的函数.请从shape类派生三角形类(triangle),矩形类(rectangle),并给出具体的求面积函数.
class shape{
public:
virtual float area( )=0;
};
float total(shape *s[ ],int n)
{ float sum=0.0;
for(int i=0;iarea( );
return sum;
}

帮你写了一个圆和矩形的抽象类
参考一下吧!!!

#include<iostream.h>
class shape{
public:
virtual float perimeter()=0;
virtual float area()=0;
virtual void disp()=0;
};
class rectangle:public shape
{
private:
float height;
float width;
public:
rectangle(float x,float y)
{
SetHeight(x);
SetWidth(y);
}
void SetHeight(float x)
{
height=x;
}
void SetWidth(float y)
{
width=y;
}
virtual float perimeter()
{
return (height+width)*2;
}
virtual float area()
{
return height*width;
}
virtual void disp()
{
cout<<"这是一个矩形对象\n";
cout<<"height="<<height<<endl;
cout<<"width="<<width<<endl;
}
};
class circle:public shape
{
private:
int a;
int b;
int r;
pu