7. The Use of C++ Friends

Keywords: Programming

Use of 2.7 and C++ Friends

  • Friend elements have Friend functions and Friend classes.

  • Friends exist to directly access private data of classes without calling member functions, so as to improve the efficiency of program operation.

  • Friends actually destroy the encapsulation of classes.

  • Friend keyword: friend.

2.7.1. Friend Function

  • In the class body, add the keyword friend before the type descriptor of the function.
  • In the external definition of the class, the definition format is the same as that of ordinary functions.
  • Friend function is not a member function.
  • Friend functions can directly access private members in the class body.
  • Friend function: friend double my_distance(Point a, Point b);
//An example of friend function
#include <iostream>
#include <math.h>
using namespace std;

class Point
{
public:
    Point(double i, double j)
    {
        x = i;
        y = j;
    }

    void getxy()
    {
        cout<<"("<<x<<","<<y<<")"<<endl;
    }

    friend double my_distance(const Point &a, const Point &b); //Explanation of Friend Function
private:
    double x;
    double y;
};

double my_distance(const Point &a, const Point &b)   //Definition of Friend Function
{
    double dx=a.x-b.x;
    double dy=a.y-b.y;

    return sqrt(dx*dx+dy*dy);
}

int main()
{
    double d11=3.0,d12=4.0,d21=6.0,d22=8.0;

    Point p1(d11,d12);
    Point p2(d21,d22);
    p1.getxy();
    p2.getxy();

    double d = my_distance(p1,p2);          //Call of friend function
    cout<<"Distance is "<<d<<endl;

    return 0;

}

2.7.2. Friends

  • All member functions of a friend class are friend functions of another class and can access hidden information in another class (including private members and protected members).

  • The statement format for defining friend classes is as follows:
    The class name of friend class (that is, the class name of friend class);
    Among them: friend and class are the key words.

  • When a class A is a friend of another class B, all member functions in the friend class A are friend functions of another class B.

    //Examples of friend classes
    #include <iostream>
    using namespace std;
    
    class X
    {
    public:
        friend class Y;    //Class Y is a friend class of class X
        void set(int i, int j)
        {
            x = i;
            y = j;
        }
    
        void display()
        {
            cout << "x=" << x << "," << "y=" << y << endl;
        }
    
    private:
        int x;
        int y;
    };
    
    class Y
    {
    public:
        Y(int i,int j);
        void display();
    
    private:
        X a;    //Data members are subobjects of class X
    };
    
    Y::Y(int i,int j)
    {
        a.x = i;
        a.y = j;
    }
    
    void Y::display()
    {
      cout << "x=" << a.x << "," << "y=" << a.y << endl;
    }
    
    int main()
    {
    //    X b;
    //    b.set(5,6);
    //    b.display();
        Y c(6,9);
        c.display();
    //    b.display();
    
        return 0;
    }
    

2.7.3. Attentions of Youyuan

  • The function of Friends is mainly to improve the efficiency of the program and facilitate programming. However, with the improvement of hardware performance, the role of Friends is not obvious. On the contrary, Friends destroy the encapsulation of classes. Therefore, when using Friends, the advantages and disadvantages should be weighed.
  • Friendship cannot be inherited.
  • Friendship is one-way, not exchangeable. If class B is a friend of class A, class A is not necessarily a friend of class B, depending on whether there is a corresponding declaration in the class.
  • Friendship is not transitive. If class B is the friend of class A, class C is the friend of class B, class C is not necessarily the friend of class A, but also depends on whether there is a corresponding declaration in the class.
  • Friends are currently mainly used for operator overloading there.

Posted by joel24 on Mon, 30 Sep 2019 16:57:32 -0700