编程学习 /C++ /进阶提高 /类与对象
⚙️ C++ 进阶提高

类与对象

定义类、构造方法与访问限定符。

📅 2026-11-23 👁 0 次阅读 🔗 参考 cppreference.com
类与对象示意图

定义类

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 是对外接口。冒号后的列表是成员初始化列表,比在构造体里赋值更高效。