Talk about the isolation of parent class and subclass

Source: Internet
Author: User

Previously written code knew to set the provider outside the class, such as the following example:

1 class Money2 {3  Public:4Money (intMoney ): M_curvalue (Money) {}5       6       voidStoreintMoney) {M_curvalue + =Money ;}7       voidSpent (intMoney) {M_curvalue-=Money ;}8 Private:9    intM_curvalue;Ten }; One  A intMainintargcChar**argv) - { -Money Mymoney ( -); theMymoney.store ( -); Accessing money through the access interface of the cash class, but not directly before the operation -Mymoney.spent ( -); -     return 0; -}

But it never pays attention to the parent class and the subclass to communicate through the interface.

The words in the book:
derived classes can directly access the containing data members of the base class, even if they are initialized at construction time, but generally do not do so, but rather through the base class interface (member functions) to access them, initialization is also through the constructor of the base class.
The advantage of this is that once the implementation of the base class has errors, the modification of the base class does not affect the operation of the derived class unless the interface is involved.
Classes and classes directly, you do yours, I do mine, to interface for communication. Even the base and derived classes are not class-based. That's why classes can play their part.

For example:

1 classFather2 {3  Public:4Father (stringFIRSTNM,stringSECONDNM ="Mao"): M_firstnm (FIRSTNM), M_SECONDNM (SECONDNM)5     {6      }7     8     stringGetfistname () {returnm_firstnm;}9 Ten protected: One    stringm_firstnm; A Private: -    stringm_secondnm; - };  the  - classson:public Father - { -  Public: +Son (stringFIRSTNM,stringsecondnm): Father (FIRSTNM), M_secondname (SECONDNM) -      { +      } A //The following function directly uses the base class of the function that contains the member is bad style at      BOOLIsfirstname (stringgivennm) -       { -              returnM_firstnum = = givennm?true:false; -       } - //The following function is used by the interface to be a better style -      BOOLIsfirstname (stringgivennm) in       { -              returnGetfirstname () = = givennm?true:false; to       } + Private: -     stringM_secondname; the};

Well, the words in the book should be the code above, too. You have to pay attention to it later, and then bypass some pits in unconsciousness, don't step in.

Talk about the isolation of parent class and subclass

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.