class Base{ public: int Base_public; protected: int Base_protected; private: int Base_private; }; class Derived:public Base{ public: int Derived_public; protected: int Derived_protected; private: int Derived_private; public: void Check(); void Check(Base& vBase,Derived& vDerived); }; void Derived::Check() { //this 是Derived类型的,在this所属类作用域(Derived作用域)里
//,Derived本身定义的变量,无论什么访问标号都可以 Derived_private =5; //ok Derived_protected=5; //ok Derived_public = 5; //ok //Derived继承Base来的变量,可以访问Base中public和protected标号的成员变量 Base_public =5;//ok Base_protected =5;//ok //Base_private =5;//error //给每个变量前加上this指针,更方便理解 //类类型变量在所属类作用域里,变量中由所属类自身定义的成员变量可以任意访问 //所属类继承来的变量,只可以访问public和protected成员变量 //类类型变量在其他作用域里,只能访问public成员变量 } // void Derived::Check(Base &vBase,Derived &vDerivde) { //vBase是Base类型,在Derived类作用域里(非本身作用域),只能访问public标示的成员变量 //vBase.Base_private =5;//error //vBase.Base_protected =5;//error vBase.Base_public =5;//ok //vDerived是Derived类型,在Derived作用域里(本身作用域),所属类定义的变量,可任意访问 vDerivde.Derived_public =5;//ok vDerivde.Derived_protected =5;//ok vDerivde.Derived_private =5;//ok //所属类继承来的变量,只能访问public,protected //vDerivde.Base_private =5;//error vDerivde.Base_protected =5;//ok vDerivde.Base_public =5;//ok //private任何时候只能在本身作用域使用 }
ezhong的博客园:http://www.cnblogs.com/ezhong/
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ezhong的博客签名-------------------------------------
以上内容来自ezhong的博客园,作者:ezhong
ezhong的博客园: http://www.cnblogs.com/ezhong
感谢您的阅读。感谢您的分享。