Friend Function
Friend Function
Explain friend
function.
There are two types of friend function
1) friend non member function
2) friend member function
Ø
C++
allows the nonmember functions, to be made as a friend of that class, now the
non-member function, which is declared as a friend of that class, can have
access to the private data members of that class to which it has been declared
as a friend.
Ø
There
could be a situation where we would like two classes to share a particular
function.
Ø
For
example, consider a case where two classes try to share a common function.
Ø
Here, C++ allows the common function to be made friendly, so here the function can access to the private data
of both classes.
Ø
So
Friend function work as a bridge between two classes.
Ø
To
make an outside function friendly to the class, we have to declare the
non-member function as a friend of the class by preceding the ‘friend’ keyword
before the function prototype inside the class.
Syntax:
Friend
<data_type><function_name>();
Friend non-member function.
When an outside
function, this does not belong to any of the other class.
class sample
{
Int a;
public:
friend void
change(sample &);
};
Void
change(sample &x)
{
x.a=5;
}
Int main()
{
Sample A;
Change(A);
return 0;
}
Friend member
functions:
If
a member function of class say, ‘dimple’ wants to access the private data
members of another class say ‘simple’, then the member function of class
‘dimple’ has to be declared as the
friend of the class ‘simple’ within the class ‘simpe’ declaration. These types
of friend functions are known as friend member function.
Class
simple;
Class
dimple
{
Public:
Void set_data(simple &,int);
};
Class
simple
{
Private:
Int x;
Public:
Int get_data();
Friend void dimple::set_data(simple
&, int);
};
Void
dimple::set_data(simple &a, int)
{
a.x=b;
}
Int
simple::get_data()
{
Return 0;
}
Int
main()
{
simple e;
dimple f;
f.set_data(e,5);
cout<<e.get_data()<<endl;
return 0;
}
List the
operators that a friend cannot be used?
Operator
|
||
=
|
-
|
Assignment
operator
|
()
|
-
|
Function call
operator
|
[]
|
-
|
Subscripting
operator
|
->
|
Class member
access operator
|
characteristics of friend function.
Ø The friend function can be invoked without the help of any object.
Ø The friend function is not the scope of the class to
which the function has been declared as friend.
Ø The fiend functions usually have objects as their
arguments.
Ø The friend function can be declared either in the
private or public section of the class.
Ø The friend function has to be preceded by the keyword
‘friend’.
Ø Unlike member functions, the friend function can not
access the member names directly. The data members of the class has to be
accessed using an object name and object to member access operator.
Explain the friend class.
It
is possible for one class to be a friend of another class. When this is the
case, the friend class and all of its member functions can access to the
private members of class within which it is declared.
Example
class
temp
{
int a,b;
public:
void set(int x,int y)
{
a=x;
b=y;
}
friend class minimum;
};
class
minimum
{
public:
int min(temp obj)
{
if(obj.a>obj.b)
return obj.a;
else
return obj.b;
}
};
int
main()
{
temp t1;
minimum m;
clrscr();
t1.set(4,5);
cout<<"ans="<<m.min(t1);
getch();
return 0;
}
Difference between member function and friend function.
Member
function
|
Friend
function
|
It is a member of a class.
|
It is not a member of a
class.
|
Member function can be call
using object and dot operator.
|
Friend can be call without
using object and dot operator.
|
We can be
defined the member function inside or outside the class.
|
Friend function can be only
defined outside the class
|
There is no need any keyword
to the declaration of member function.
|
We have to use friend keyword For the declaration of
friend function.
|
Syntax
Return_type
function_name(arg. List);
|
Syntax
Friend return_type
function_name(arg.list);
|