123456789101112131415161718192021222324252627282930313233343536373839404142 |
- #include <iostream>
- using namespace std;
- // 定义长方形Rect类
- class Rect {
- public:
- double length;
- double width;
- double area;
- Rect(double l, double w) {
- length = l;
- width = w;
- area = length * width;
- }
- };
- // 派生出长方体类Cub
- class Cub : public Rect {
- public:
- double height;
- double volume;
- double surfaceArea;
- Cub(double l, double w, double h) : Rect(l,w) {
- height = h;
- volume = area * height;
- surfaceArea = 2 * (length*width + length*height + width*height);
- }
- };
- int main() {
- double l, w, h;
- cout << "输入长方体的长、宽、高:"<<endl;
- cin >> l >> w >> h;
- Cub c(l, w, h);
- cout << "length=" << c.length << " width=" << c.width << " height=" << c.height << endl;
- cout << "表面积=" << c.surfaceArea << " 体积=" << c.volume << endl;
- return 0;
- }
|