可执行程序,希望对你有帮助
#include
#include
usingnamespacestd;
classShape
{
public:
Shape(){}
~Shape(){}
//纯虚函数
virtualfloatGetArea()const=0;
};
classCircle:publicShape
{
public:
Circle(floatsz):r(sz)
{}
~Circle(){}
floatGetArea()const;
private:
//半径
floatr;
};
//圆只用半径就可以计算面积了
floatCircle::GetArea()const
{
constfloatpi=3.1415926f;
returnpi*r*r;
}
classSquare:publicShape
{
public:
Square(floatx1,floaty1,floatx2,floaty2)
{
cx=x1;
cy=y1;
px=x2;
py=y2;
}
~Square(){}
floatGetArea()const;
private:
//中心点
floatcx;
floatcy;
//顶点
floatpx;
floatpy;
};
//正方形面积=对角线乘机的一半
floatSquare::GetArea()const
{
returnpowf(2*sqrt((cx-px)*(cx-px)+(cy-py)*(cy-py)),2)/2;
}
intmain()
{
Circlec(10.0);
Squares(10.0,10.0,4.0,4.0);
cout