类的设计很简单,而且函数只是获取类中某一成员或表面积体积,我这种是把定义和实现分开写(一大堆函数啊。。。)
#include <iostream> using namespace std; class Cube { public: void setLength(int l); void setWidth(int w); void setHeight(int h); int getLength(); int getWidth(); int getHeight(); int getArea(); int getVolume(); private: int length; int width; int height; }; void Cube::setLength(int l) { length = l; } void Cube::setWidth(int w) { width = w; } void Cube::setHeight(int h) { height = h; } int Cube::getLength() { return length; } int Cube::getWidth() { return width; } int Cube::getHeight() { return height; } int Cube::getArea() { return (length * width + length * height + width * height) * 2; } int Cube::getVolume() { return length * width * height; } int main() { int length, width, height; cin >> length; cin >> width; cin >> height; Cube c; c.setLength(length); c.setWidth(width); c.setHeight(height); cout << c.getLength() << " " << c.getWidth() << " " << c.getHeight() << " " << c.getArea() << " " << c.getVolume() << endl; return 0; }