Practice
7.16
None, the interface of the class is defined after the public specifier, and the implementation details of the class are defined after the private specifier.
7.17
Yes. The default access permissions for class members are different. Class members that are private,struct by default are public by default.
Generally, a struct is used when all members of the class we want to define are public.
7.18
p242
7.19
"Friend"
Practice
7.20
A friend function is used when declaring an out-of-class function that requires a direct reference to a private member within a class.
Design reason: Not all functions related to a class are suitable for declaring a member function of that class, such as the Add (obj, obj) on the book;
Pros and cons: Unknown ...
7.21
7.22
#include <iostream>#include<string>using namespacestd;//Person.hclassPerson {friend IStream&read (IStream & is, Person &p); Public: Person ()=default; Person (Const string&name): Name (name) {} person (Const string&name,Const string&address): Name (name), address {} person (IStream&); stringGetName ()Const{returnname;} stringGetAddress ()Const{returnaddress;}Private: stringname; stringaddress;}; IStream&read (Istream&, person&); Ostream&print (Ostream&,Constperson&);//Person.cppIStream &read (IStream & is, Person &p) { is>> P.name >>p.address; return is;} Ostream&print (Ostream &os,ConstPerson &2) {OS<< P.getname () <<" "<<p.getaddress (); returnOS;} Person::P Erson (IStream& is) {Read ( is, * This);}//main.cppintMain () {person P1; Person P2 ("XKLF"); Person P3 ("Llyy"," China"); Person P4 (CIN); Print (cout, p1)<<Endl; Print (cout, p2)<<Endl; Print (cout, p3)<<Endl; Print (cout, p4)<<Endl; return 0;}
"C + + Primer, 5e" access control and encapsulation