题解 | #设计立方体类#
设计立方体类
https://www.nowcoder.com/practice/0f02d35dcd564f0a87865d604eccbe18
#include <iostream> using namespace std; class Cube { // write your code here...... public: void setHeight(int h) { m_height = h; } void setLength(int l) { m_length = l; } void setWidth(int w) { m_width = w; } int getLength() const { return m_length; } int getWidth() const { return m_width; } int getHeight() const { return m_height; } int getArea() const { return 2 * (m_height * m_length + m_height * m_width + m_width * m_length); } int getVolume() const { return m_height * m_length * m_width; } private: int m_length; int m_width; int m_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; }
对成员变量只读的成员函数,末尾最好加const