What
is friend function? How it is implemented in C++? Explain advantages of using
friend function with the help of an example.
Ans
What is a Friend Function?
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.
Need for Friend Function:
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.
How to define and use Friend Function in C++:
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.
Some important points to note while using friend functions in
C++:
* The keyword friend is placed only in the function
declaration of the friend function and not in the function definition.
* It is possible to declare a function as friend in any
number of classes.
* When a class is declared as a friend, the friend class has
access to the private data of the class that made this a friend.
* A friend function, even though it is not a member function,
would have the rights to access the private members of the class.
* It is possible to declare the friend function as either
private or public.
* The function can be invoked without the use of an object.
The friend function has its argument as objects, seen in example below.
Example to understand the friend function:
#include
class exforsys
{
private:
int a,b;
public:
void test()
{
a=100;
b=200;
}
friend int compute(exforsys e1)
//Friend Function Declaration with keyword friend and with
the object of class exforsys to which it is friend passed to it
};
int compute(exforsys e1)
{
//Friend Function Definition which has access to private data
return int(e1.a+e2.b)-5;
}
main()
{
exforsys e;
e.test();
cout<<"The result is:"<
//Calling of Friend Function with object as argument.
}
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)
Friend Functions
A C++ friend functions are special functions which can access
the private members of a class. They are considered to be a loophole in the
Object Oriented Programming concepts, but logical use of them can make them
useful in certain cases. For instance: when it is not possible to
implement some function, without making private members accessible in them.
This situation arises mostly in case of operator overloading.
In the following example, the friend function print is a
member of class TWO and accesses the private data members a and b of class ONE
#include <iostream>
using namespace std;
//Must be known to TWO
//before declaration of ONE.
class ONE;
class TWO
{
public:
void print(ONE&
x);
};
class ONE
{
int a, b;
friend void
TWO::print(ONE& x);
public:
ONE() : a(1), b(2) {
}
};
void TWO::print(ONE& x)
{
cout << "a
is " << x.a << endl;
cout << "b
is " << x.b << endl;
}
int main()
{
ONE xobj;
TWO yobj;
yobj.print(xobj);
}
Friend functions have the following properties:
- 1) Friend of the class can be member of some other class.
- 2) Friend of one class can be friend of another class or all the classes in one program, such a friend is known as GLOBAL FRIEND.
- 3) Friend can access the private or protected members of the class in which they are declared to be friend, but they can use the members for a specific object.
- 4) Friends are non-members hence do not get “this” pointer.
- 5) Friends, can be friend of more than one class, hence they can be used for message passing between the classes.
6) Friend can be
declared anywhere (in public, protected or private section) in the class.
No comments:
Post a Comment