#include
#include
#define pi 3.14
using namespace std;
class shape
{
public:
virtual float area()=0;
};
class circle:public shape
{
private:
float r;
public:
circle(float r1)
{
r=r1;
}
float area()
{
return (float)pi*r*r;
}
};
class rectangle:public shape
{
private:
float width,height;
public:
rectangle(float w1,float h1)
{
width=w1;height=h1;
}
float area()
{
return width*height;
}
};
class square : public rectangle//其实是一样的,把rectangle当基类
{
public:
square(float len):rectangle(len,len){};
~square(){};
float area(float len)
{
return len * len;
};
};
float total(shape*s[],int n)
{
float sum=0.0;
for(int i=0;i
return sum;
}
int main()
{
shape* s[2];
s[0]=new circle(1);
cout<area()<
cout<area()<
cout << s[2]->area() << endl;//
cout << total( s, 3 ) << endl;//改为3
for( int i = 0; i < 3; i++ )//释放
{//
delete [] s[ i ];//
}//
system( "pause" );
return 0;
}