C + + friends Getting Started Tutorial

Source: Internet
Author: User
Tags function definition modifier

Before explaining what a friend is, let's explain why you need friends and friends:

Usually for ordinary functions, it is impossible to access the protection members of a class, and if you want to do so, you have to make the members of the class public (shared), but the problem with this is that any external function can access it without constraint, and C + + uses the friend modifier, You can allow some of the functions you set to operate on these protected data to avoid setting all class members to public and to protect data members to the maximum extent possible.

Friends can make common function direct access to class protection data, avoid the frequent invocation of class member functions, can save processor overhead, improve the efficiency of the program, but the paradox is that even the maximum protection, but also destroy the package characteristics of the class, which is the disadvantage of friends, Now the CPU speed is getting faster today we do not recommend it, but it as a necessary knowledge of C + +, a complete component, we still need to discuss.

Declaring a normal function in a class, preceded by a friend modifier, then the function becomes a friend of the class, and you can access all the members of that class.

Let's look at a piece of code to see how we use friends to access all the members of a class.

#include <iostream>
using namespace Std;
Class Internet
{
Public
Internet (char *name,char *address)
{
strcpy (Internet::name,name);
strcpy (internet::address,address);
}
friend void shown (Internet &obj);//friend function declaration
Public
Char name[20];
Char address[20];
};
void shown (Internet &obj)/function definition, cannot write, void Internet::shown (Internet &obj)
{
cout<<obj.name<<endl;
}
void Main ()
{
Internet A ("China Software Development Laboratory", "www.cndev-lab.com");
Shown (a);
Cin.get ();
}

The code above is defined by the friend function, we have successfully accessed the protection member name of object A, and the friend function is not considered a member function of a class, it is simply a normal function declared as a class friend, so the definition part of the class outer function cannot be written as a void Internet::shown ( Internet &obj), this should be noted.

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.