编写一个程序计算三角形、正方形和圆形三种图形的面积.依题意,可以抽象出一个基类base,在其中说明一个虚函数,用来求面积.并利用单接口、多实现版本设计各个图形面积的方法.例如,使得下面的主函数
int main() {
\x09base *p;
\x09triangle t(20,20);\x09//第一个参数为三角形底边,第二个参数为底边上的高
\x09square s(20);
\x09circle c(20);
\x09p = &t;
\x09p->disp();
\x09p = &s;
\x09p->disp();
\x09p = &c;
\x09p->disp();
\x09return 0;
}
的运行结果为:
三角形面积:200
正方形面积:400
圆形面积:1256.6
人气:426 ℃ 时间:2020-06-06 19:58:35
解答
#include
#include
#include
using namespace std;
class base
{
public:
virtual void disp() = 0;
};
class triangle : public base
{
public:
triangle(int base, int height) : m_base(base), m_height(height) {}
void disp()
{
cout #include 应该就可以。 或者去掉那一行,只影响输出的有效位数。 gcc 4.4 编译通过。
推荐
- 计算下面图形的面积.(6个三角形相同)他画了一个正方形他的边长是30,正方形中间分成4个三角形,
- 用一个三角形能不能推导出三角形面积计算公式呢?,写出你的推导过程
- 如图:圆中等腰直角三角形的面积是5平方厘米,求圆的面积是多少?怎样写计算过程
- 用Java语言编写,要求申明三角形类,继承图形抽象类,计算三角形的周长和面积!
- 则模样编写一个C程序计算三角形哦那个的哦面积
- 英语翻译:学习成绩优秀
- 学校买来了100kg白菜,吃了五分之二,还剩多少千克?
- The children ran to their father excitedly,but they got_ from him.A.everything B.anying Cnothing
猜你喜欢
- 从动物植物身上得到启示我想发明什么 作文
- 求一道物理题 甲物体放在粗糙的水平面上,乙物体放在光滑的水平面,他们在相等的水平推力作用下移动
- 用“和谐是一种美”也排比句,
- 如图,已知在△ABC中,AB=AC=6,BC=5,D是AB上一点,BD=2,E是BC上一动点,连接DE,作∠DEF=∠C,射线EF交线段AD于F
- 结缔组织作用
- 帮我解一道数学题,要分析和做题过程.
- Y=根号x-1+根号1-X+1,则X的2012平方+2012平方的Y=
- 生活是一杯酒,品出人生滋味的酸甜苦辣.这句话出自哪里?