class A {\fbox{ancestor}
public: A() { n = 0; } void add( int i ) { n = n + i; } virtual int val() { return n; } protected:// private would deny access to D
int n; }; class D : public A {\fbox{descendant}
public: D() : A() { } int val() { return n \$%\$ 2; } };
Allowing descendants full access to the instance variables defined by ancestors, however, increases the dependency on the actual implementation of these ancestors, with the risk of a total collapse when the implementation of an ancestor changes. See chapter \ref{Composition mechanisms} for a discussion of techniques to avoid such risks.