要求定义一个描述形状的抽象类shape,类内包括求面积的area和求各图形总面积的total函数.

要求定义一个描述形状的抽象类shape,类内包括求面积的area和求各图形总面积的total函数.
1.从shape派生出三角形,圆形,正方形类,要求类中有构造函数,修改显示成元值的函数,求面积的函数.
2.写main()函数,计算三边为7,8,9的三角形,边长为9.9的正方形和半径为4的圆形(必须调用total函数计算) 诸位大侠帮忙忙吧.考试要用.
程序问题不大,关键是total函数,怎么在shape类中声明定义,以及是在main函数中,怎样调用.希望可以针对问题.
liuchuangyuan 1年前 已收到1个回答 举报

lipeng3310 幼苗

共回答了21个问题采纳率:100% 举报

class Trapezoid : public Shape
{
private:
double top;
double bottom;
double height;
public:
Trapezoid(double t, double b, double h)
{
top = t;
bottom = b;
height = h;
}
double Area()
{
return (top + bottom) * height / 2;
}
};
#define PI 3.1415926
class Circle : public Shape
{
private:
double r;
public:
Circle(double r)
{
this->r = r;
}
double Area()
{
return PI * r * r;
}
};
class Triangle : public Shape
{
private:
double bottom;
double height;
public:
Triangle(double b, double h)
{
bottom = b;
height = h;
}
double Area()
{
return bottom * height / 2;
}
};

1年前

10
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 16 q. 0.546 s. - webmaster@yulucn.com