c ++ in a friend

c ++ classes in a package sex, class private data only member functions of the class can access the program to access private members of the class, you must call the member functions through an object, but the frequent invocation will reduce operating efficiency.

In order to solve the above problem, c ++ friend added mechanism, by calling the friend can not directly access the private data member function of class to improve operational efficiency.

Friend may be a function (a friend function) may also be a category (friend class)

Friend function:

  • Body type described, plus keyword friend, in vitro defined class, define the format and the same general function.
  • Friend functions are non-member functions, and calls on the same general function.
  • Friend function can directly access the private members of the class.

Global friend function or functions do:

 1 #include<iostream>
 2 #include<cmath>
 3 using namespace std;
 4 class Point
 5 {
 6     public:
 7     Point(double xx, double yy)
 8     {
 9         x = xx;
10         y = yy;
11     }
12     void Getxy();
13     friend double Distance(Point &a, Point &b);
14     private:
15     double x, y;
16 };
17 void Point::Getxy()
18 {
19     cout << "(" << x << "," << y << ")" << endl;
20 }
21 double Distance(Point &a, Point &b)
22 {
23     double dx = a.x -­‐ b.x;
24     double dy = a.y -­‐ b.y;
25     return sqrt(dx*dx + dy*dy);
26 }
27 int main(void)
28 {
29     Point p1(3.0, 4.0), p2(6.0, 8.0);
30     p1.Getxy();
31     p2.Getxy();
32     double d = Distance(p1, p2);
33     cout << "Distance is " << d << endl;
34     return 0;
35 }
View Code

Class member functions do friend function:

. 1 #include <the iostream>
 2 #include <the cmath>
 . 3  the using  namespace STD;
 . 4  class Point;
 . 5  // forward declaration, an incomplete type is ⼀ statement that simply provide the class name (No need to provide class implementation) It can be. Using only be used to declare the pointer and the lead 
6  Use with.
. 7  class ManagerPoint
 . 8  {
 . 9      public :
 10      Double Distance (& Point A, Point & B);
 . 11  };
 12 is  class Point
 13 is  {
 14      public :
 15      Point ( Double XX, Double YY)
16     {
17         x = xx;
18         y = yy;
19     }
20     void Getxy();
21     friend double ManagerPoint::Distance(Point &a, Point &b);
22     private:
23     double x, y;
24 };
25 void Point::Getxy()
26 {
27     cout << "(" << x << "," << y << ")" << endl;
28 }
29 double ManagerPoint::Distance(Point &a, Point &b) 
30 { 
31     double dx = a.x -­‐ b.x; 
32     double dy = a.y -­‐ b.y; 
33     return sqrt(dx*dx + dy*dy); 
34 } 
35 int main(void)
36 {
37     Point p1(3.0, 4.0), p2(6.0, 8.0);
38     p1.Getxy();
39     p2.Getxy();
40     ManagerPoint mp;
41     float d = mp.Distance(p1,p2);
42     cout << "Distance is " << d<< endl;
43     return 0;
44 }
View Code

Tomomoto类:

  • All member functions are friend class of another class friend function, you can access the hidden information in another class
  • Format; friend class friend class name
  • Description When a class A B is a friend of another class, the class A friend in friend All member functions are functions of another class B
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 class X{
 6 public:
 7     friend class Y; //类Y 是类X 的友元类
 8     void set(int i) { x=i; }
 9     void display() 
10     {
11         cout<<"x="<<x<<","<<"y="<<y<<endl;
12     }
13 is      Private :
 14      int X;
 15      static  int Y; // Static Data Description 
16  };
 . 17  
18 is  class the Y
 . 19  {
 20 is      public :
 21 is          the Y ( int I, int J);
 22 is          void the display ();
 23 is      Private :
 24          X- a; // data member as an object of class X 
25  };
 26 is  
27  int X :: Y = 10 ; // static data define and initialize 
28  
29 Y::Y(int i,int j)
30 {
31     a.x=i; X::y=j;
32 }
33 
34 void Y::display()
35 {
36     cout<<"x="<<a.x<<","<<"y="<<X::y<<endl;
37 }
38 
39 int main()
40 {
41     X b;
42     b.set(5);
43     b.display();
44     Y c(6,9);
45     c.display();
46     b.display();
47     return 0;
48 }
View Code

 

to sum up:

  1. Friend While increasing the operating efficiency of the code, but he destroyed the class encapsulation.
  2. Friendship is not inherited
  3. Friend relationship is one-way , no exchangeable
  4. Friendship is not transitive

 

Guess you like

Origin www.cnblogs.com/fanhua666/p/11521382.html