题解 | #设计立方体类#
设计立方体类
https://www.nowcoder.com/practice/0f02d35dcd564f0a87865d604eccbe18
#include <iostream> using namespace std; class Cube { // write your code here...... private: int length = 0; int width =0; int height =0; public: void setLength(int a); void setWidth(int a); void setHeight(int a); int getLength(); int getWidth(); int getHeight(); int getArea(); int getVolume(); }; void Cube::setLength(int a) { length = a; } void Cube::setWidth(int a) { width = a; } void Cube::setHeight(int a) { height = a; } int Cube::getLength() { return length; } int Cube::getWidth() { return width; } int Cube::getHeight() { return height; } int Cube::getArea() { int s=2*(length*width+length*height+width*height); return s; } int Cube::getVolume() { int v=length*width*height; return v; } 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; }