"C + + Primer, 5e" access control and encapsulation

Source: Internet
Author: User

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

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.