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