Geekband C + + learning notes (a little bit of enlightenment from friend)

Source: Internet
Author: User

Hello, everyone, I'm Geekband's student, and in the course of learning the web, because of the business relationship, I'm going to make a list of some of my thoughts and notes on C + + that might not be very good, because it's only half a year since I got to the programming language. So if there is something wrong or not quite right, you can talk to me, or QQ to me, I can talk to you about computer things

    Then let's start.!What I want to share this week is that I amFriend (Friend Yuan)'s understanding, thatfriendWhat is? FriendIs the function that can be used to adjust the number of the numbers in the category, the characteristic of which is:Enclosure and information are hidden, and thus PublicWithPrivate, which means that only the member functions of the category can visit the private members of the category.(private), non-category membership functions can only be accessed by public members(public)As follows:

Class Temp{public:int a=10;private:int b=20;}; </span></span>

In this class, we use int to define two numbers (a, a, a, b), but because the positions of the two numbers are different, the difference comes out, and outside of the temp, we can't adjust and change B. But a can, because the rights of the two are different.

We can find that if we want to protect the numbers and maintain the enclosure, we have to keep the numbers in private, but sometimes it becomes a very troublesome time to use them, and it becomes very bad in both security and the efficiency of the program.

In order to understand these problems, friend This program is designed to be born . A friend is a function or category beyond the definition, however, in the same time, it is necessary to put friend in the whole function or category before the same time, so in strict view, the real friend is not a member of the category, but can visit the private members, so can improve the efficiency of the line, But it also breaks down the nature of the packaging and concealment, so in the use of the same as Kan caution, the following is the friend usage I wrote:

int complex (int a,int b); class temp{public:private:int aa,bb;friend int complex (int a,int b);}; </span>

But I think so, because the function you created is outside, and firend I think of him as a mark, to tell her in the category, to tell you that I am your friend or member, and that I am free to take your object.

Here, in fact, each object in the category can be considered a friend of the opposite side, let's look at:

Class temp{public:temp (int a=0,int b=0): AA (a), BB (b) {}int func (const temp& param) {return param.aa+param.bb;} Private:int AA,BB;}; </span>
This code is by the teacher in the curriculum, I changed a few letters, I can see that the actual function of the Func is not broken in the time of the packaging, but his front also did not add friend, this is why? Because every object in class is mutual friend , just don't need to write friend.

Finally we end up with the metaphysical nature and meaning:

1. Friend is for the purposes of the adjustment, the efficiency is better, but also lost the packaging

2. Friends are free to get private members when they are in action

3.friend is a function or class that you add to the private


Geekband C + + learning notes (a little bit of enlightenment from friend)

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.