Friend friend function in C + + to parse _c language in detail

Source: Internet
Author: User
Tags function definition cmath

A friend function is a non-member function that can directly access a private member of a class. It is a normal function that is defined outside of a class, and it does not belong to any class, but it needs to be declared in the definition of a class, with only the keyword friend before the name of the friend.

We already know that classes have an attribute of encapsulation and information hiding. Only member functions of a class can access private members of a class, and other functions in the program cannot access private members. Non-member functions can access public members in a class, but if the data members are defined as public, this also destroys the hidden attributes. In addition, you should see that in some cases, especially when multiple calls are made to some member functions, the time overhead is required because of parameter passing, type checking, and security checking, which affects the efficiency of the program.

In order to solve the above problems, this paper puts forward a scheme to make ufida. A friend is a generic function that is defined outside of a class, but it needs to be described in the class body , in order to differentiate it from the member functions of the class, and the keyword friend is preceded by the description. A friend is not a member function, but it can access private members in a class. The role of the friend is to increase the efficiency of the program (that is, to reduce the time overhead required for type checking and security checks), but it destroys the encapsulation and concealment of the class, allowing Non-members to access the private members of the class.

A friend can be a function called a friend function; A friend can also be a class, which is called a friend class.

A friend function is characterized by the ability to access a member function of a private member in a class. A friend function is syntactically the same as a normal function, that is, it is defined and called as a normal function.

Copy Code code as follows:

#include "Cmath"
#include "iostream"
using namespace Std;
Class Point
{
Public
Point (double xx,double yy)
{
X=xx;
Y=yy;
}
void Getxy ();
Friend Double Distance (Point &a,point &b);
Protected
Private
Double x,y;
};
void Point::getxy ()
{
cout<< "(" <<this->x<< "," <<this->y<< ")" <<endl;
cout<< "(" <<x<< "," <<y<< ")" <<endl;
}
Double Distance (Point &a,point &b)
{
Double length;
Length=sqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y)); It can refer to private members in a class
return length;
}
int main (void)
{
Point P1 (3.0,4.0), p2 (6.0,8.0);
P1.    Getxy (); The invocation method of a member function, which is invoked by using an object.
P2. Getxy ();
Double d = Distance (P1,P2); The invocation method of a friend function, like a call to a normal function, does not invoke the same as a member function
cout<<d<<endl;
System ("pause");
return 0;
}

Description: In the point class in the program, a friend function distance () is described, which adds the friend keyword in front of the description, identifying it as not a member function, but a friend function. It is defined in the same way as a normal function definition, and differs from the definition of a member function because it does not need to indicate the class to which it belongs. However, it can refer to private members in a class, and the a.x,b.x,a.y,b.y in the function body are private members of the class, which are referenced through objects. When you call the function, it is also the same as the normal function, do not call as a member function. In this case, p1. Getxy () and P2. Getxy () This is a call to a member function, to be represented by an object. and distance (P1, p2) is called the friend function, it is called directly, does not need the object representation, its argument is an object. (The program's function is known as two point coordinates, to find the distance of two points.) )

The following is an input to the above code and an overload of the output stream:

Copy Code code as follows:

#include <cmath>
#include <iostream>
using namespace Std;
Class Point
{
Public
Point (double xx,double yy)
{
X=xx;
Y=yy;
}
void Getxy ();
Friend Double Distance (Point &a,point &b);
Friend Ostream &operator << (ostream &a,point &b);
Protected
Private
Double x,y;
};
Friend ostream& operator<< (ostream& o,a& another);
Ostream &operator << (ostream &out,point &b)//when declared in a class, can be Ostream &a, and the function definition can also be ostream & Out
{
out<< "(" <<b.x<< "," <<b.y<< ")" <<endl;
return out;
}
void Point::getxy ()
{
cout<< "(" <<this->x<< "," <<this->y<< ")" <<endl;
cout<< "(" <<x<< "," <<y<< ")" <<endl;
cout<<*this;
}
Double Distance (Point &a,point &b)
{
Double length;
Length=sqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y));
return length;
}
int main (void)
{
Point P1 (3.0,4.0), p2 (6.0,8.0);
P1. Getxy ();
P2. Getxy ();
Double d = Distance (P1,P2);
cout<<d<<endl;
System ("pause");
return 0;

}

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.