题解 | #设计立方体类#

设计立方体类

https://www.nowcoder.com/practice/0f02d35dcd564f0a87865d604eccbe18

#include <iostream>
using namespace std;

class Cube {

    // 设计一个立方体类(Cube)
    //定义立方体类的属性
    private:
        int length;
        int width;
        int height;
    //定义立方体类的方法
    public:
        void setLength(int len){
            this->length = len;   //方法功能:给类的属性赋值
        }
        void setWidth(int wid){
            this->width = wid;
        }
        void setHeight(int hei){
            this->height = hei;
        }
        int getLength(){
            return this->length;
        }
        int getWidth(){
            return this->width;
        }
        int getHeight(){
            return this->height;
        }
        int getArea(){
            return 2*(length*width + length*height + width*height);
        }
        int 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;
}

#你的秋招进展怎么样了##我的求职思考##零基础学习C++#
全部评论

相关推荐

牛客83700679...:简历抄别人的,然后再投,有反馈就是简历不行,没反馈就是学历不行,多投多改只要技术不差机会总会有的
点赞 评论 收藏
分享
06-18 13:28
已编辑
门头沟学院 Web前端
爱睡觉的冰箱哥:《给予你300的工资》,阴的没边了
点赞 评论 收藏
分享
这是什么操作什么意思,这公司我服了...
斯派克spark:意思是有比你更便宜的牛马了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务