题解 | #牛牛学立体#

牛牛学立体

https://ac.nowcoder.com/acm/problem/21999

使用面向对象思想

#include <iostream>

using namespace std;

// 声明一个体积抽象接口
class Volume{
    // 求表面积 
    virtual int surface() = 0;  
    
    // 求体积
    virtual int area() = 0;
};

// 声明一个长方体,并实现体积抽象接口
class Cuboid : public Volume{
    public:
        Cuboid(int width, int height, int tall): _width(width), _height(height), _tall(tall){}
        int surface() {
            return 2 * (_width * _height + _width * _tall + _height * _tall);
        }
    
        int area() {
            return _width * _height * _tall;
        }
    private:
        int _width;
        int _height;
        int _tall;
};

int main() {
    int width, height, tall;
    cin >> width >> height >> tall;
    Cuboid cuboid(width, height, tall);
    cout << cuboid.surface() << endl;
    cout << cuboid.area() << endl;
    return 0;
}

全部评论

相关推荐

码农索隆:这种hr,建议全中国推广
点赞 评论 收藏
分享
但听说转正率很低,我现在有在实习了,好纠结要不要去
熬夜脱发码农:转正率低归低,但是实习的经历你可以拿着,又不是说秋招不准备了
点赞 评论 收藏
分享
06-07 19:59
门头沟学院 C++
补药卡我啊😭:都快15年前的了还在11新特性
你的简历改到第几版了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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