C++primer 15.5-day exercise

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.