给出下面的基类,要求: (1)建立基类Area的俩个派生类Rectangle(矩形)与Isosceles(三角形),让每一个派生类都包含一个函数getArea(),分别用来返回矩形与三角形的面积。用构造函数对height与width进行初始化。 (2)写出主程序,从键盘读入矩形的长和宽及三角形的底和高,输出矩形及三角形的面积。
Area类定义.png
输入格式:
一共两行,第1行为矩形的长和宽,第2行位三角形的底边和高。
输出格式:
第1行为矩形的面积 第2行为三角形的面积
输入样例:
在这里给出一组输入。例如:
10 5
4.5 6.5
输出样例:
在这里给出相应的输出。例如:
the Area of Rectangle is 50
the Area of Isosceles is 14.625
#include<iostream>
using namespace std;
class Area {
private:
double height;
double width;
double area=0.0;
public:
Area(double h=0.0, double w=0.0) {
height = h;
width = w;
}
void setArea(double h, double w) {
height = h;
width = w;
area = h * w;
}
double getArea(void) {
return area;
}
};
class Rectangle :public Area {
public:
Rectangle(double h=0.0, double w=0.0):Area(h,w){
setArea(h, w);
}
};
class Isosceles :public Area {
public:
Isosceles(double h = 0, double w = 0) :Area(h, w) {
setArea(h, w);
}
};
int main() {
double a = 0.0, b = 0.0, c = 0.0, d = 0.0;
double area1 = 0.0, area2 = 0.0;
cin >> a >> b;
Rectangle p1(a, b);
area1 = p1.getArea();
cin >> c >> d;
Isosceles p2(c, d);
area2 = p2.getArea()/2;
cout << "the Area of Rectangle is " << area1<<endl;
cout << "the Area of Isosceles is " << area2 << endl;
return 0;
}