以下是一个面向对象的C++程序,用于计算并输出三个长方柱的体积:
```cpp
#include <iostream>
using namespace std;
class RectangularBox{
private:
double length;
double width;
double height;
public:
void setDimensions(double l, double w, double h){
length = l;
width = w;
height = h;
}
double getVolume(){
return length * width * height;
}
};
int main(){
RectangularBox box1, box2, box3;
double l, w, h;
cout << "Enter dimensions of box 1 (length, width, height): ";
cin >> l >> w >> h;
box1.setDimensions(l, w, h);
cout << "Enter dimensions of box 2 (length, width, height): ";
cin >> l >> w >> h;
box2.setDimensions(l, w, h);
cout << "Enter dimensions of box 3 (length, width, height): ";
cin >> l >> w >> h;
box3.setDimensions(l, w, h);
cout << "The volume of box 1 is " << box1.getVolume() << endl;
cout << "The volume of box 2 is " << box2.getVolume() << endl;
cout << "The volume of box 3 is " << box3.getVolume() << endl;
return 0;
}
```
在上面的程序中,我们定义了一个名为RectangularBox的类,它有三个数据成员:length、width和height,分别表示长方柱的长、宽和高。这个类有两个成员函数,setDimensions用于设置长方柱的尺寸,getVolume用于计算长方柱的体积。
在主函数中,我们创建了三个RectangularBox对象:box1、box2和box3。然后,通过从键盘读入的尺寸,调用setDimensions函数设置每个长方柱的尺寸。最后,调用getVolume函数计算并输出每个长方柱的体积。
注意,我们使用了面向对象的编程方法,将长方柱的尺寸和计算体积的过程封装在一个类中。这样可以使程序更加清晰,易于维护和扩展。