1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- #include <iostream>
- using namespace std;
- class Rect {
- protected:
- double length;
- double width;
-
- public:
- Rect(double l, double w) : length(l), width(w) {}
-
- double area() {
- return length * width;
- }
- };
- class Cub : public Rect {
- private:
- double height;
-
- public:
- Cub(double l, double w, double h) : Rect(l, w), height(h) {}
-
- double surfaceArea() {
- return 2 * (area() + length * height + width * height);
- }
-
- double volume() {
- return area() * height;
- }
-
- void print() {
- cout << "length=" << length << " width=" << width << " height=" << height << endl;
- cout << "表面积=" << surfaceArea() << " 体积=" << volume() << endl;
- }
- };
- int main() {
- double l, w, h;
- cout << "输入长方体的长、宽、高:" << endl;
- cin >> l >> w >> h;
-
- Cub cub(l, w, h);
- cub.print();
-
- return 0;
- }
|