一个最小的类

👁️ 1 人浏览 💬 0 人评论 ❤️ 添加收藏

(本条路线统一用 g++ -std=c++17 -O0 -Wall 编译)构造、初始化列表、两个只读方法:

#include <iostream>
#include <string>

class Box {
public:
    Box(int w, int h) : w_(w), h_(h) {}      /* 初始化列表 */
    int area() const { return w_ * h_; }
    int w() const { return w_; }

private:
    int w_;
    int h_;
};

int main() {
    Box b(3, 4);
    std::cout << b.w() << "/" << b.area() << "\n";
    return 0;
}
提交你的答案
请登录后提交答案。
去登录
代码编辑器
Ctrl + Enter 运行
本次输入:
输出:

                        
👩‍🏫
AI
💬 题目评论

全部评论