#include<iostream> #include<string> //#include<vector> using namespace std; class Base{ public: int j; int basemem( Base &b){ k = 1; //使用了k //k =b.k; error C2248: 'Base::k' : cannot access private member declared in class 'Base' 同样不行。 return 0; }; int basemem(){return k;}; void setk(){ k=1;} protected: int i; private: int k; }; class pub_Base : public Base{ public: void pub( pub_Base &d,const Base &b){ // k =1; //因为它在派生类中无法调用了 i = 1; d.i = 1; cout << d.i << endl; i = d.i; // b.i = 1; } int fun(){ this->setk(); //k = 1; cout << basemem()<<endl; return 0; } int pub_i; }; class pubb_Base : public pub_Base{ public: int pubb_i; void pubbb(const pub_Base &d,const Base &b){ i = 1; //i = d.i;//因为d.i不在pub_base 中了 // b.i = 1; } }; void fun1(pub_Base &d){ //d.i; 通常 cout << "fun1 是"<<endl; } int main(){ int test = 0; Base b = Base(); Base b1 = Base(); pub_Base pb = pub_Base(); pub_Base pb1 = pub_Base(); pb.fun(); // pb.i; //protected 可以被调用,但是仅仅局限于在成员内部 b.basemem(b1); pubb_Base pbb = pubb_Base(); pb.pub(pb1,b); //pbb. test = b.basemem(); //pb.i; //b.i; // error ; i 是protected 成员不能运行。 b.j = 3; pb.pub_i = 3; pb.j; //pb.i; //protected 可以被调用,但是仅仅局限于在成员内部 //pb.k; //b.k; // error; k 是private成员不能远行 } 测试了public private protected三种标号使用情况; 派生类中的基类的private成员不能直接被访问; 派生类中的基类的protected 可以在派生类中访问;