题解 | #设计立方体类#

设计立方体类

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

全部评论

相关推荐

10-25 12:05
已编辑
湖南科技大学 Java
若梦难了:我有你这简历,已经大厂乱杀了
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务