Exercise 15.18
User code can use derived classes to convert to a base class only if the derived class is public-inheriting the base class: B &tb=D;
Base *p=&d1; Correct, Pub_derv is the public inherit base
p=&d2; Error, Priv_derv is a private inheritance base
p=&d3; Error, Prot_dery is protecting inherited base
p=&dd1; Right, Derived_from_public is the public inheritance in Pub_dery
p=&dd2; Error, Priv_derv is private inherited from base
p=&dd3; Error, Prot_dery is protection inherited from base
Exercise 15.19
1 classBase2 { 3CharPriv; 4 protected: 5intProt; 6 Public: 7voidpub () {}8voidMEMFCN (Base &b) {b = * This; }//correct9 }; Ten structPub_dery: PublicBase One { A voidMEMFCN (Base &b) {b = * This; }//correct -intF () {returnProt;} - }; the structProt_dery:protectedBase - { -voidMEMFCN (Base &b) {b = * This; }//correct - }; + structPriv_dery:PrivateBase - { +voidMEMFCN (Base &b) {b = * This; }//correct AintF1 () {returnProt;} at }; - structDfpub: PublicPub_dery - { -voidMEMFCN (Base &b) {b = * This; }//correctly, a derived class of a public inheritance derived class can access its public and protected parts -intUse_base () {returnProt;} - }; in structDfpro: PublicProt_dery - { tovoidMEMFCN (Base &b) {b = * This; }//correctly, a derived class that protects inherited derived classes can access its protection section + }; - structDFPRI: PublicPriv_dery the { *voidMEMFCN (Base &b) {b = * This; }//error, the base type is inaccessible and does not allow for the conversion of inaccessible base class "base". The reason is that priv_dery is a private inheritance base, so for Dfpri, the base portion of its priv_dery part is private.
Exercise 15.20
See above
Exercise 15.21
C++primer 15.5-day exercise