// ============================================================================ // p1c04_01_02 — 4.1.2 类的封装 // 来源:川大 C++/LinuxC wiki「02.Qt方向」 // 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用 // // --- 原文片段 [pageId:blockIdx] --- 标注出处。 // 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。 // ============================================================================ #include using namespace std; // --- 原文片段 [58954448:1] 4.1.2 类的封装 --- //封装两层含义 //1. 属性和行为合成一个整体 //2. 访问控制,现实事物本身有些属性和行为是不对外开放 class Person{ //人具有的行为(函数) public: void Dese(){ cout << "我有钱,年轻,个子又高,就爱嘚瑟!" << endl;} //人的属性(变量) public: int mTall; //多高,可以让外人知道 protected: int mMoney; // 有多少钱,只能儿子孙子知道 private: int mAge; //年龄,不想让外人知道 }; int main(){ Person p; p.mTall = 220; //p.mMoney 保护成员外部无法访问 //p.mAge 私有成员外部无法访问 p.Dese(); return EXIT_SUCCESS; } // --- main ---