c++ - C ++ Child class cannot inherit private member from Father class? -
i using win8 vc++2012.
the code above show child class b under no circumstances can access a::a. neither can change access attribute of a::a a::b , a::c.
so a::c not inherited b. sizeof(a) , sizeof(b) 12 , 24 respectively, means a::a occupy memory in b.
- how b stores a::a in memory while never access it?
- the book c++ primer says, can restore access attribute of base class member can not change it. here code shows can change access attributes of a::b protected public in b. why?
here's code:
#include <iostream> using namespace std; class { private: int a; protected: int b; public: int c; a(int a, int b, int c): a(a), b(b), c(c) { cout << "a: "; cout << << " "; cout << b << " "; cout << c << endl; } }; class b: protected { private: int d; protected: int e; //using a::a; compile error public: int f; //a::a; compile error using a::c; //restore a::c public access a::b; // change a::b protected public b(int d, int e, int f): a(d, e, f), d(d), e(e), f(f) { cout << "b\n"; //cout << << endl; compile error cout << b << " "; cout << c << " "; cout << d << " "; cout << e << " "; cout << f << endl; } }; int main() { a(1,2,3); b b(4,5,6); cout << "sizeof(a)=" << sizeof(a) << endl; //output 12 cout << "sizeof(b)=" << sizeof(b) << endl; //output 24 return 0; }
children inherit parent's private members, can not access them. access make them protected
class { protected: // <<------------ make `a` protected in parent int a;
Comments
Post a Comment