这是我自己写的,有什么不对的请多多指教
下面有三种方法
#include<iostream>
using namespace std;
//方法一
int main() {
double length,width,high;
double surface, volume;
cout<< "请输入长方体的长,宽和高:" << endl;
cin >> length >> width >> high;
surface = 2 * (length * width + length * high + width * high);
volume = length * width * high;
cout << "表面积为:" << surface << endl;
cout << "体积为:" << volume << endl;
return 0;
}
//方法二
double get_surface(double l, double w, double h) {
return 2 * (l * w + l * h + w * h);
}
double get_volume(double l, double w, double h) {
return l * w * h;
}
int main() {
double length, width, high;
double surface, volume;
cout << "请输入长方体的长,宽和高:" << endl;
cin >> length >> width >> high;
surface = get_surface(length, width, high);
volume = get_volume(length, width, high);
cout << "表面积为:" << surface << endl;
cout << "体积为:" << volume << endl;
return 0;
}
//方法三
//面向对象
class Cuboid {
public:
Cuboid(double l = 0, double w = 0, double h = 0) //定义一个构造函数,便于后续初始化函数
{
length = l;
width = w;
high = h;
}
double GetSurface()
{
return 2 * (length * width + length * high + width * high);
}
double GetVolume() {
return length * width * high;
}
private:
double length, width, high;
};
int main() {
double i, j, k;
cout << "请输入长方体的长,宽和高:" << endl;
cin >> i >> j >> k;
Cuboid c(i,j,k);
cout << "表面积为:" << c.GetSurface() << endl;
cout << "体积为:" << c.GetVolume() << endl;
return 0;
}