一个最小的类
(本条路线统一用 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;
}
全部评论