Friends of C + +: Friend function and friend class detailed _c language

Source: Internet
Author: User

Introduction of Friends
We know that the member functions of a class can access other member functions of the same kind, including public, private, and protected members. The external function of a class can only access the public members of the class.

A friend is a mechanism that allows non-class member functions to access Non-public members of a class.
You can designate a function as a friend of a class, or you can designate an entire class as a friend of another class.

Friend function
Friend class

Two, friend function
The friend function is defined outside the class, but it needs to be described in the class body
To distinguish it from the member functions of the class, the method is defined by using the keyword friend in the class to describe the function, in the following format:

Friend type friend function name (parameter table);
The function of friend is to improve the efficiency of program operation

Friend function Considerations:
1,
the friend function is not a member of the class function, in the function body to access the members of the object, you must use the object name plus operator. Adds an object member name. But a friend function can access all members (public, private, protected) in a class, and a normal function can only access public members in a class.

2, the friend function is not subject to the access rights of the keyword restrictions, you can put it in the public, private, protected part of the class, but the result is the same.

3, the scope of a friend function of a class is not the scope of that class. If the friend function is a member function of another class, its scope is another type of scope, otherwise the same as a normal function.

4 . The friend function destroys the encapsulation of object-oriented programming class, so the friend function should be used sparingly if it is not necessary. Or use other means to guarantee the encapsulation.

Copy Code code as follows:

#include <math.h>
#include <iostream>
using namespace Std;
Class Point
{
Friend Double Distance (const point &P1, const point &P2);
Public
Point (int x, int y);
Private
int x_;
int y_;
};
Point::P oint (int x, int y): x_ (x), Y_ (y)
{
}
Double Distance (const point &P1, const point &P2)
{
Double dx = p1.x_-p2.x_;
Double dy = p1.y_-p2.y_;
return sqrt (DX * dx + dy * dy);
}
int main (void)
{
Point P1 (3, 4);
Point P2 (6, 9);
cout << Distance (p1, p2) << Endl;
return 0;
}

The distance in the program is a friend function of the point class that can access the private data members of the class.

Three, friend class
If a member function of a Class B frequently accesses a data member of another Class A, and the private/protectd limit of A's data member causes trouble with B access, B can only be accessed indirectly through the member function of A's public
Make B The Friend class of Class A, that is, Class A to open its PRIVATE/PROTECTD content to Class B, let B direct access
Friend class: A class can be a friend of another class
All member functions of a friend class are friend functions of another class
Declaration of friend Class:
Friend class class name;

Friend Class Considerations:
1, friend relationship is one-way
2, friend relations can not be passed
3, friend relations can not be inherited

TeleController.h:

Copy Code code as follows:

#ifndef _tele_controller_h_
#define _tele_controller_h_
Class Television;
Class Telecontroller
{
Public
void Volumeup (television &tv);
void Volumedown (television &tv);
void Chanelup (television &tv);
void Chaneldown (television &tv);
};
#endif//_tele_controller_h_

TeleController.cpp:
Copy Code code as follows:

#include "TeleController.h"
#include "Television.h"
void Telecontroller::volumeup (television &tv)
{
Tv.volume_ + 1;
}
void Telecontroller::volumedown (television &tv)
{
Tv.volume_-= 1;
}
void Telecontroller::chanelup (television &tv)
{
Tv.chanel_ + 1;
}
void Telecontroller::chaneldown (television &tv)
{
Tv.volume_-= 1;
}

Television.h:
Copy Code code as follows:

#ifndef _television_h_
#define _television_h_
Class Telecontroller;
Class Television
{
Friend class Telecontroller;
Public
Television (int volume, int chanel);
Private
int volume_;
int chanel_;
};
#endif//_television_h_

Television.cpp:
Copy Code code as follows:

#include "Television.h"
television::television (int volume, int chanel): volume_ (volume), Chanel_ (Chanel)
{
}

main.cpp:
Copy Code code as follows:

#include "Television.h"
#include "TeleController.h"
#include <iostream>
using namespace Std;

int main (void)
{
Television tv (1, 1);
Telecontroller TC;
Tc. VOLUMEUP (TV);
return 0;
}


The Telecontroller class is used as the friend class of the television class so that the member functions of the Telecontroller class can access all members of the television class, including private.

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.