C + + language Learning (vii)-friend

Source: Internet
Author: User
Tags cmath

C + + language Learning (vii)-friend one, friend Introduction 1, Friend introduction

The design mechanism of object-oriented programming implements the hiding and encapsulation of data, the member variables of class are generally defined as private members, and the member functions are generally defined as public ones, and are the communication interfaces between class and external. In practice, some functions outside the class require frequent access to the member variables of the class, and functions outside the class can be defined as friend functions of the class. In addition to the friend function, there are friend classes, which are collectively referred to as friends. The role of friends is to improve the efficiency of the program (that is, to reduce the type checking and security checks, such as the need for time overhead), but friend destroyed the encapsulation and concealment of the class, so that non-class member functions can access the private members of the class.
Friend is a relationship in the C + + language, where a friend relationship occurs between a function and a class or between a class and a class. A friend relationship is one-way and cannot be passed.
A function that has a friend relationship with a class is called a friend function, and a class with a friend relationship is called a friend class.

2, the characteristics of friends

The properties of friends are as follows:
A. Declare friend in class with friend keyword
B, the friend of the class can be another class or a specific function
C, Friend is not part of the class
D, Friend is not restricted by access level in class
E, friends can access all members of a specific class directly
F, friend relationship cannot be inherited
G, friend relationship is one-way, non-commutative
H, friend relationship does not have transitivity

3, the nature of friends

The essence of a friend is to let other members (global functions, member functions of other classes, other classes) that are not part of this class become members of this class and have attributes of members of this class.

Two, friend function 1, friend function introduction

A friend function is a non-member function that can directly access a private member of a class, a function defined outside the class, a global function that does not belong to any class, or a member function of another class, but it needs to be declared in the definition of the class.

2. Global function is friend function

The global function, as a friend declaration of a class, only needs to precede the name of the friend with the keyword friend, whose format is as follows:
friend 类型 函数名(形式参数);
A function can be a friend function of more than one class, and it needs to be declared separately in each class.

#include <iostream>#include <cmath>using namespace std;class Point{public:    Point(double x = 0, double y = 0)    {        this->x = x;        this->y = y;    }    void printPoint()    {        cout << "(" << x << "," << y << ")";    }    //友元函数声明    friend double getDistance(const Point &a, const Point &b);private:    double x;    double y;};double getDistance(const Point &a, const Point &b){    double dx = a.x - b.x;    double dy = a.y - b.y;    return sqrt(dx*dx + dy*dy);}int main(int argc, char *argv[]){    Point a(0,0);    Point b(1,8);    cout << "Point";    a.printPoint();    cout << " and Point";    b.printPoint();    cout << " has distance at "<< getDistance(a, b) << endl;    return 0;}
3. class member function is friend function

Class member functions as friend declarations of a class simply precede the name of the friend with the keyword friend, whose format is as follows:
friend 类型 类名::函数名(形式参数);
A function can be a friend function of more than one class, and it needs to be declared separately in each class.

  #include <iostream> #include <cmath>using namespace Std;class point;class managerpoint{public: Double getdistance (const point &a, const point &b);};        Class Point{public:point (Double x = 0, double y = 0) {this->x = x;    This->y = y;    } void Printpoint () {cout << ("<< x <<", "<< y <<") ";    }//friend function declares friend Double managerpoint::getdistance (const point &a, const point &b);p rivate:double x; Double y;};    Double managerpoint::getdistance (const point &a, const point &b) {double dx = a.x-b.x;    Double dy = a.y-b.y; return sqrt (DX*DX + dy*dy);}    int main (int argc, char *argv[]) {Managerpoint Manager;    Point A (0,0);    Point B (1,8);    cout << "point";    A.printpoint ();    cout << "and point";    B.printpoint ();    cout << "have distance at" << Manager.getdistance (A, b) << Endl; return 0;}  

The preceding declaration of the class is used in the preceding code. A forward declaration is an incomplete type (forward declaration) declaration that simply provides the class name (without providing a class implementation). The forward declaration function is limited:
A, the object of the class cannot be defined.
B, can be used to define pointers or references to this type.
C, used to declare (not define) a function that uses the type as a formal parameter or return type.

Three, friend class

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. When you want a class to have access to a private member of another class, to protect a member, you can declare the class as a friend of another class.
The statement format that defines the friend class is as follows:
friend class 类名;
Friend and class are keywords, and the class name must be a defined class in the program.

  #include <iostream> #include <cmath>using namespace Std;class point;class managerpoint{public: Double getdistance (const point &a, const point &b);};        Class Point{public:point (Double x = 0, double y = 0) {this->x = x;    This->y = y;    } void Printpoint () {cout << ("<< x <<", "<< y <<") ";    }//Friend class declares friend class Managerpoint;private:double X; Double y;};    Double managerpoint::getdistance (const point &a, const point &b) {double dx = a.x-b.x;    Double dy = a.y-b.y; return sqrt (DX*DX + dy*dy);}    int main (int argc, char *argv[]) {Managerpoint Manager;    Point A (0,0);    Point B (1,8);    cout << "point";    A.printpoint ();    cout << "and point";    B.printpoint ();    cout << "have distance at" << Manager.getdistance (A, b) << Endl; return 0;}  
ManagerPoint类的所有成员函数都是类Point的友元函数,能访问类Point的私有成员和保护成员。

C + + language Learning (vii)-friend

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.