定义类
class Student {
private:
string name;
int score;
public:
Student(string n, int s) : name(n), score(s) {}
void introduce() {
cout << "我是" << name << "," << score << "分" << endl;
}
};
Student s("小明", 92);
s.introduce();
private 成员外部不能直接访问,public 是对外接口。冒号后的列表是成员初始化列表,比在构造体里赋值更高效。