C ++ youyuan

Source: Internet
Author: User

As mentioned in the previous blog post, you can record the access permission here when you think that youyuan can change the access permission.

You can use friend to apply for membership requests. Other classes cannot access private members of this type. However, you can access this type after applying for membership.


Generally, youyuan has three forms:

1) youyuan class
2) youyuan function 3) youyuan member function

1 youyuan class
Class A; // The first step declares the membership class B // The second step defines the class for which you want to apply for membership (specify the membership relationship in the class) {public: B (INT _ age ): age (_ age) {} friend a; // declare the friend relationship private: int age;}; Class A // step 3 defines the friend class {public: void print (const B & B) {cout <"B. age = "<B. age <Endl ;}};

2Youyuan Function

Class B // The first step defines the class for which you want to apply for membership (specify the membership relationship in the class) {public: B (INT _ age): Age (_ age) {} friend void print (const B & B); // declare the friend relationship private: int age;}; // define the void print (const B & B) function in step 2) {cout <"B. age = "<B. age <Endl ;}

3. member functions (the most troublesome one)

Class B; // The first step declares that you want to apply for a membership class (because it is used in the membership class) Class A // The second part can define the membership class (except for the membership function in the class, because it is used to apply for membership in the friend class, and the class currently applying for membership is not defined) {public: A () {cout <"Run constructor" <Endl ;} void test () {cout <"Run test" <Endl;} void print (const B & B); // declare the member function in the class, just declare }; class B // step 3 defines the class for which you want to apply for membership (specify the membership relationship in the class) {public: B (INT _ age): Age (_ age) {} friend void a: Print (const B & B); // declare the friend relationship private: int age ;}; // The fourth part defines the member function void :: print (const B & B) {cout <"B. age = "<B. age <Endl ;}

Related Article

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.