Tutorials
C++In this C++ tutorials, you will learn about friend functions, need for friend function, how to define and use friend function and few important points regarding friend function, explained with example.
As discussed in the earlier sections on access specifiers, when a data is declared as private inside a class, then it is not accessible from outside the class. A function that is not a member or an external class will not be able to access the private data. A programmer may have a situation where he or she would need to access private data from non-member functions and external classes. For handling such cases, the concept of Friend functions is a useful tool.
A friend function is used for accessing the non-public members of a class. A class can allow non-member functions and other classes to access its own private data, by making them friends. Thus, a friend function is an ordinary function or a member of another class.
The friend function is written as any other normal function, except the function declaration of these functions is preceded with the keyword friend. The friend function must have the class to which it is declared as friend passed to it in argument.
Example to understand the friend function:
|
The output of the above program is
The result is:295
The function compute() is a non-member function of the class exforsys. In order to make this function have access to the private data a and b of class exforsys , it is created as a friend function for the class exforsys. As a first step, the function compute() is declared as friend in the class exforsys as:
friend int compute (exforsys e1)
The keyword friend is placed before the function. The function definition is written as a normal function and thus, the function has access to the private data a and b of the class exforsys. It is declared as friend inside the class, the private data values a and b are added, 5 is subtracted from the result, giving 295 as the result. This is returned by the function and thus the output is displayed as shown above.
| A Friend Function is a special type of function which has given a special permission to access both provate and protected data members and member function |
|
//friend int compute(exforsys e1) A semi-colon is missig in the above statement. //cout |
| Friend fuction is a non member function which can access the private ,protected members and methods of the class at outside. |
| A friend function is an outsider function which can access the private or protected class members of the class to which it has been a friend.also it does not need SRO(::) for invocation it gets executed as a normal function and also does not need any odject to make a call for its invocation, it acesses the pvt. class members using indirection operaator(-->).A function is made the friend to any class by placing the keyword friend before its declaration inside the class definition and its definition resides outside the class a friend function to any class may simultaneously work as the member function to any other class.the main function of a friend function is to act as a bridge between two diff. classes. |
| In the defination of friend function value returned is (e1.a e2.b)-5 in this expression object e2 reference is not resolved. |
| how to create pointers objects as arguments to the friend functions? |
|
int compute(exforsys *e1) { //Friend Function Definition which has access to private data return int(e1->a+e1->b)-5; } |
|
#include<iostream.h> #include<conio.h> class class1 { private: int num; public: void get() { cout<<"Number:"; cin>>num; } friend void display(class1 cl); }; void display(class1 cl) { cout<<cl.num; } void main() { class1 cls; clrscr(); cls.get(); display(e); getch(); } //It doesn't work....... // Whats the actual error in this..... |
| friend function should not contain ; semicolon |
|
Its working pls see the below .. #include<iostream.h> class class1 { private: int num; public: void get() { cout<<"Number:"; cin>>num; } friend void display(class1 cl); }; void display(class1 c1) { cout<<c1.num; } int main() { class1 cls; cls.get(); display(cls); } |
| Friend function can be used to access private data member, without using object |
| How to calculate the multiplication of two complex number using friend function? |
| Friend function is a function used to access the private data member and member function from out side the class. |
|
Friend functions still differ from member function in a way they access private data members, friend will have to use '.' or '->' operator to access the element of the object. We can understand friend function do this because they are declared in the scope of the class and they use '.' or '->' operator because they are outsider to class (implies that they can't be attached to an object as member function are attached by default, there for we have to pass object and use indirection operator). |
| friend functions are outside function that access the private data of a class,it is not a member of any otherclasses.it is also used the keyword friend function also. |
| The private data mmembers of a class can be accessed through the non-memeber functions outside the class by declaring it with the keyword "this" |
|
Hello, I am Mr. Girish Pounikar, 26yrs., Pune. I have a very good knowledge of C and C++, I have gone through the concept of friend Function in C++... I have observed that there is no use of friend function as an Accessible member function whether it is private or protected. Because when we define the friend function, It acts as a normal Function outside the class. you can remove the friend function declaration from the class and see the outer function definition will work without declaring prototype inside the class. According to me there is no use of friend function, If you can explain me about this query then I'll be more thankful to you. Regards, Mr. Girish APPIN Technology, Pune. |
|
class abc { ...... ...... public: ...... ...... friend void xyz(void); //declaretion }; |
|
Dear all As the word friend implies that friend is not a owner of your things but becasue he is your friend he can use your assets. so the same concept applies here. A Friend can use the private memebrs but he does not own these members. Friendship is granted not taken. I would like to explain for new commers that suppose you have a car and some of your real friend wants to use it. Now my question is can he use it for a day or two. ok, next can he sell it, so this same scenario in this illustration. Thanks |
|
Can a friend function be drawn as a dependency relationship in a UML diagram? or is more of an association? Regards |
| can member function of one class can be a firend of another class?if yes then how?explain this with an example |
|
First of all, tell me what are the advantages of a friend function? Does it avoid the use of multiple definitions of the same function? Since we pass object as an argument to a friend function, we have to define that friend for each class. So, where lies the difference? |