A summary of friends in C + +

Source: Internet
Author: User

1. What is it?

The friend mechanism allows a class to grant access to its non-public members to the established function or class. --"C + + Primer"

2. Why should it be presented? :
Some of the member functions in a class are frequently used, require a significant amount of time overhead, and in order to reduce the time overhead, allow the encapsulation of the program to be broken, using an external function or class to access the private members of the class. So the role of friends is to improve the efficiency of the program (that is, to reduce the type check and security checks, such as the need for time overhead).

3. Questions about Friends:
The use of friends destroys the encapsulation and concealment of classes, allowing non-member functions to access private members of the class.

4. Friend's history:
Friends of the production is due to the initial speed of the computer is not now fast, and the C + + language is speed, in order to solve other classes to easily access the class's private variables or private member functions, so the use of friends this mechanism, but now the speed of the computer, the role of friends have gradually seen the effect came, But the C + + standard did not cancel friends.

5. Note points used by friends:
Friend has a bit of a breakthrough in the object-oriented concept of encapsulation, can use less than the use of less.
Let's talk about when to use friend function and friend class, in fact, there is not much difference, if only the general function to access the private members of the class or private data members, so that the UF meta-function can be, if you use the class to access other classes of private data members and private member functions to make the UF meta-class.
A friend's declaration begins with a keyword friend and can only appear inside the class definition, where a friend declaration may appear anywhere in the class, and the friend is not a member of the class that grants the friend relationship, so they are not affected by the access control that is part of their declaration. It is usually best to place the start or end of a class definition.

Give an example:

(1) for-class point (dot), define-functions to implement the same capability-calculate the distance-between. One function is defined as member function of Point (named Distance_2), and the other is defined as friend function (named dis Tance_1). Complete the definition of the class, the program run successfully. Keep the output in your document.

#include <iostream>#include <cmath>using namespace STD;classPoint {friend DoubleDistance_1 (point&,point&); Public: Point (Doublexx =0.0,Doubleyy =0.0{x = xx; y = yy;}DoubleGetX () {returnX }DoubleGetY () {returnY }DoubleDistance_2 (Point &);Private:Doublex, y;};DoubleDistance_1 (Point & A, point &b) {return  sqrt(POW(A.x-b.x,2)+POW(A.y-b.y,2));}DoublePoint::d istance_2 (Point & P) {return sqrt(POW(X-p.x,2)+POW(Y-P.Y,2));}intMain () {point P1 (3,5), p2 (4,6);DoubleRESULT1 = Distance_1 (P1, p2);DoubleRESULT2 = p1.distance_2 (p2);cout<<"The distance is"<< RESULT1 <<' \ t '<< result2 << Endl;return 0;}

The distance between two points is calculated using the member function and the friend function respectively. It is important to note here that only distance_1 can be declared as a friend function, and distance_2 cannot be declared as a friend function. Because distance_2 has only one parameter, and the friend function is not a member function, the distance calculation of a parameter cannot be

A summary of friends in C + +

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.