下面给出了图形类Shape的定义,请以Shape为基类派生出矩形类Rectangle和三角形类Triangle,并实现他们各自的求面积函数area(),该函数返回double类型。
下面是一份供你填充的代码:
#include <iostream>
using namespace std;
class Shape
{
protected:
int w, h;
public:
Shape(int w, int h): w(w), h(h){}
};
/* 实现两个派生类 */
int main()
{
int w, h;
cin >> w >> h;
Rectangle a(w, h);
cout << a.area() << endl;
cin >> w >> h;
Triangle b(w, h);
cout << b.area() << endl;
return 0;
}
为了确保你按要求编写程序,我们将使用OJ的代码模板功能。
在提交代码时,只能提交两个派生类的定义和实现,绝不要提交其他的任何代码;在评测时,你提交的代码首先会被插入上面这段代码中间的注释那个位置,然后再编译运行。
务必将Rectangle与Triangle实现为Shape的派生类。如果你没有做到这一点,你可能会得0分。
输入描述
输入由主函数完成,你提交的代码中不应包含任何输入语句。
输出描述
输出由主函数完成,你提交的代码中不应包含任何输出语句。
示例1:
输入:7 8 7 7
输出:56 24.5
/*
#include <iostream>
using namespace std;
c