知我者谓我何求
幼苗
共回答了20个问题采纳率:85% 举报
可执行程序,希望对你有帮助
#include
#include
using namespace std;
class Shape
{
public:
Shape(){}
~Shape(){}
//纯虚函数
virtual float GetArea() const=0;
};
class Circle : public Shape
{
public:
Circle(float sz):r(sz)
{}
~Circle(){}
float GetArea()const;
private:
//半径
float r;
};
//圆只用半径就可以计算面积了
float Circle::GetArea()const
{
const float pi=3.1415926f;
return pi*r*r;
}
class Square : public Shape
{
public:
Square(float x1,float y1,float x2,float y2)
{
cx=x1;
cy=y1;
px=x2;
py=y2;
}
~Square(){}
float GetArea()const;
private:
//中心点
float cx;
float cy;
//顶点
float px;
float py;
};
//正方形面积=对角线乘机的一半
float Square::GetArea()const
{
return powf(2*sqrt((cx-px)*(cx-px)+(cy-py)*(cy-py)),2)/2;
}
int main()
{
Circle c(10.0);
Square s(10.0,10.0,4.0,4.0);
cout
1年前
8