使用面向对象思想
#include <iostream>
using namespace std;
// 声明一个体积抽象接口
class Volume{
// 求表面积
virtual int surface() = 0;
// 求体积
virtual int area() = 0;
};
// 声明一个长方体,并实现体积抽象接口
class Cuboid : public Volume{
public:
Cuboid(int width, int height, int tall): _width(width), _height(height), _tall(tall){}
int surface() {
return 2 * (_width * _height + _width * _tall + _height * _tall);
}
int area() {
return _width * _height * _tall;
}
private:
int _width;
int _height;
int _tall;
};
int main() {
int width, height, tall;
cin >> width >> height >> tall;
Cuboid cuboid(width, height, tall);
cout << cuboid.surface() << endl;
cout << cuboid.area() << endl;
return 0;
}