#include<iostream.h>
#include<conio.h>
class test
{
private:
int a,b;
public:
void getdata(int x,int y)
{
a=x;
b=y;
}
friend void display(test);
} ;
void display(test t2)
{
cout<<"\n a="<<t2.a;
cout<<"\n b="<<t2.b;
}
main()
{
clrscr();
test t1;
t1.getdata(8,9);
display(t1);
getch();
}
..............................................................................................................
A friend function for a class is used in object-oriented programming to allow access to public, private, or protected data in the class from the outside. Normally, a function that is not a member of a class cannot access such information; neither can an external class. Occasionally, such access will be advantageous for the programmer. Under these circumstances, the function or external class can be declared as a friend of the class using the friend keyword.
A friend function is declared by the class that is granting access. Friend declaration can be placed anywhere in the class declaration. It is not affected by the access control keywords.
A similar concept is that of friend class.
Friends should be used with caution. Too many functions or external classes declared as friends of a class with protected or private data may lessen the value of encapsulation of separate classes in object-oriented programming and may indicate a problem in the overall architecture design.
VIEW SOURCE:- http://en.wikipedia.org/wiki/Friend_function

No comments:
Post a Comment