【问题描述】
利用给定的矩形类,编写长方体类
class Rect {
public:
Rect(float l, float w); //定义矩形接受长和宽
float getArea(); //矩形面积
protected:
float mLength; //定义长
float mWidth; //定义宽
};
Rect::Rect(float l, float w) {
this->mLength = l;
this->mWidth = w;
}
float Rect::getArea() {
float area = mWidth * mLength;
return area;
}
class cubiod : public Rect {
public:
cubiod(float l, float w, float h);
float getVol();
float Area();
protected:
float mHeight;
};
cubiod::cubiod(float l, float w, float h) : Rect(l, w) {
this->mHeight = h;
}
float cubiod::getVol() { //体积
float underside = getArea();
float V = mHeight * underside;
return V;
}
float cubiod::Area() { //表面积
float underside = mLength * mWidth;
float side1 = mHeight * mWidth;
float side2 = mHeight * mLength;
float Area = (underside + side1 + side2) * 2;
return Area;
}
#include<iostream>
using namespace std;
int main() {
int j, k, l;
cout << "请输入矩形的长和宽:";
cin >> j >> k; //键盘接收长和宽
Rect r1(j, k);
cout << "矩形的面积为:" << r1.getArea() << endl;
cout << "请输入立方体的高:";
cin >> l; //接受高
cubiod c1(j, k, l);
cout << "立方体体积为:" << c1.getVol() << endl;
cout << "立方体表面积为:" << c1.Area();
return 0;
}