English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn about friend functions and friend classes in C ++create friend functions and friend classes, and use them effectively in the program.
OOPOne of the important concepts is data hiding, which isnon-member functioncannot access the private or protected data of an object.
However, sometimes this restriction may force programmers to write long and complex code. Therefore, C ++Programming has built-in mechanisms that allow private or protected data to be accessed from non-member functions.
This is achieved by using friend functions and friend classes.
If the function is defined as a friend function, then you can usefunctionto access the private and protected data of the class.
By using the keywordfriend, the compiler knows that the given function is a friend function.
To access data, you should declare the friend function with the keyword friend inside the class (it can be anywhere inside the class, either in the private part or the public part).
class class_name { ... .. ... friend return_type function_name(argument)/s); ... .. ... }
Now, you can define a friend function as a normal function to access the data of the class. No keywords are used in the friend definition.
class className { ... .. ... friend return_type functionName(argument)/s); ... .. ... } return_type functionName(argument/s) { ... .. ... // Private and protected data of className can be accessed from this position //Because this function is a friend function of className. ... .. ... }
/* C ++The program demonstrates the working of friend functions.*/ #include <iostream> using namespace std; class Distance { private: int meter; public: Distance(): meter(0) { } //Friend function friend int addFive(Distance); }; // Definition of friend function int addFive(Distance d) { //Accessing private data from non-member functions d.meter += 5; return d.meter; } int main() { Distance D; cout << "Distance: " << addFive(D); return 0; }
Output result
Distance: 5
Here, the friend function addFive() is declared in the Distance class. Therefore, private data can be accessed from this function.
Although this example gives you an idea about the concept of friend functions, it does not show any meaningful usage.
When you need to operate objects of two different classes, there will be a more meaningful usage. Then, friend functions will be very helpful.
You can completely operate objects of two different classes without using friend functions, but the program will be very long, complex, and difficult to understand.
#include <iostream> using namespace std; // Forward declaration class B; class A { private: int numA; public: A(): numA(12) { } //Declaration of Friend Functions friend int add(A, B); }; class B { private: int numB; public: B(): numB(1) { } // Declaration of Friend Functions friend int add(A, B); }; //The add() function is a friend function of class A and B //Access member variables numA and numB int add(A objectA, B objectB) { return (objectA.numA + objectB.numB); } int main() { A objectA; B objectB; cout << "Sum: " << add(objectA, objectB); return 0; }
Output result
Sum: 13
In this program, class A and B have declared add() as a friend function. Therefore, this function can access the private data of these two classes.
Here, the add() function adds the private data numA and numB of the two objects objectS and object, and returns it to the main function.
To make this program work properly, it should be declared as in the above example, a class B should be declared in advance.
This is because the following code in class A references the friend function of class B: friend int add(A, B);
Similarly, like a friend function, a class can also use the keyword friend to become a friend class of another class. For example:
... .. ... class B; class A { // class B is a friend class of class A friend class B; ... .. ... } class B { ... .. ... }
When a class becomes a friend class of another class, it means that all member functions of this class are friend functions of the other class.
In this program, all member functions of class B are friend functions of class A, so any member function of class B can access the private and protected data of class A, but the member functions of class A cannot access the data of class B.
How to implement class A and B as friends, so that A can access B's private and B can also access A's private? The following is an example:
#include <iostream> using namespace std; //It must be declared before class B to avoid compilation errors. class B; class A { private: int a; public: friend class B; A() { cout << "Class A is constructed" << endl; a =; 20; } ~A() { cout << "Class A is being destructed" << endl; } void show(B &b); }; class B { private: int b; public: friend class A; B() { cout << "Class B is constructed" << endl; b =; 12; } B() { cout << "Class B is being destructed" << endl; } void show(A &a) { cout << "a=" << a.a; cout << "b=" << b << endl; } }; //The function cannot be placed in class A, otherwise it will cause a compilation error. void A::show(B &b) { cout << "a=" << a; cout << "b=" << b.b << endl; } int main() { A a; B b; a.show(b); b.show(a); return 0; }Running Result:
Class A is constructed Class B is constructed a=20 b=12 a=20 b=12 Class B is destructed Class A is destructed
The method of being friend classes is to declare friend class B in class A; declare friend class A in class B;
Note:Where class B is used in class A must be defined after the declaration of class B, and only declared in class A. For example, the show function in class A on the left cannot be defined directly in class A, but only defined after the declaration of class B.