下面用简短的代码说明在最常见的public继承情况下访问权限的变化
#include <iostream> using namespace std; class A { public: A():a(0),b(0),c(0){} int get_a(void ){return a;} int a; protected: int get_b(void) {return b;} int b; private: int get_c(void) {return c;} int c; }; class B : public A { public: B():aaa(0),bbb(0),ccc(0){} int get_aaa(void ){return aaa;} int aaa; int get_b(void){return A::get_b() + b;} protected: int get_bbb(void){return bbb;} int bbb; private: int get_ccc(void){return ccc;} int ccc; }; void print(int n) { cout<<n<<endl; } int main(int argc, char* argv[]) { B object; print(object.a); print(object.aaa); print(object.get_a()); print(object.get_aaa()); print(object.get_b()); return 0; };
输出如下:
0
0
0
0
0
请按任意键继续. . .