> 其他 >
编写一个程序计算三角形、正方形和圆形三种图形的面积.依题意,可以抽象出一个基类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 编译通过。
推荐
猜你喜欢
© 2024 79432.Com All Rights Reserved.
电脑版|手机版