Friend Function – Object Oriented Programming (OOP)

Download our Android App from Google Play Store and start reading Reference Notes Offline.

friend functionFriend Function | Reference Notes
Object Oriented Programming (OOP)
Third Semester | Second year
BSc.CSIT | Tribhuvan University (TU)

Friend Function
As we have discussed that the private members cannot be accessed from outside the class. It implies that a non-member function cannot have an access to the private data of a class. Let us suppose, we want a function operate on objects of two different classes. In such situations, object oriented programming provides the friend function which is used to access the private members of a class. Friend function is not a member of any class. So it is defined without scope resolution operator. The syntax of declaring friend function is given below:

class class_name
{
…………
………
Public:
………..
………..
friend return type function_name(arguments);
}

Let us consider the complete C++ program to find out sum of n given numbers to understand the concept of friend function.
#include
#define MAX_SIZE 100
class Sum
{
int num[MAX_size];
int n;
public:
void get_number(void);
friend int add(void)
};
void Sum::get_number(void)
{
cout<<”\nEnter the total number(n):”<<endl; cin>>n;
cout<<”\nEnter the number:”<<endl;
for(int i=0;j<n;i++) cin>>num[i];
}
int add(void)
{
Sum s;
int temp=0;
s.get_number()
for(int i=0;i<s;i++)
temp+=s.num[i];
return temp;
}
void main()
{
Int res;
Res=add();
Cout<<”The sum of n values is=”<<res<<endl;
}

If you look at the above program, you can easily see that the function add is declared as friend function of class Sum. The add function accesses the private data, adds the numbers of array and return value to the main function where it is called upon. Furthermore, you can also see that friend function add() is defined without scope resolution operator (::), because it does not belong to a class.
Now, let us consider a situation in which we want to operate on objects of two different classes. In such situation, friend function can be used to bridge the two classes.
#include
class Two;       //forward declaration like function prototype
class One
{
int a;
public:
void get_a(void);
friend int min(One,Two);
};
class Two
{
Int b;
Public:
Void get_b(void);
friend int min(One,Two);
};
void One::get_a(void)
{
cout<<”Enter the value of a:”<<endl; cin>>a;
}
void Two::get_b(void)
{
cout<<”Enter the value of b:”<<endl; cin>>b;
}
int min(One o, Two t)
{
If(o.a<t.b)
Return o.a;
Else
Return t.b;
}
void main()
{
One one;
Two two;
int minvalue;
one.get_a();
two.get_b();
minvalue=min(one,two);
cout<<”Minimum=”<<minvalue<<endl;
}

You can observe that the above program contains two classes named one and two. The function min() is declared in the both the classes with the keyword friend. An object of each class has been passed as an argument to the function min(). Being a friend function, it can access the private members of both classes through these arguments in object oriented programming. Now, let us note some special properties possessed by friend function:

  • A friend function is not in the scope of the class to which it has been declared as friend.
  • A friend function cannot be called using the object of that class. It can be invoked like a normal function without the use of any object.
  • Unlike member functions, it cannot access the members directly. However, it can use the object and dot membership operator with each member name to access both private and public members.
  • It can be declared either in the public or the private part of a class without affecting its meaning.
  • Generally, it has got object as arguments.
(Visited 609 times, 1 visits today)

Posted By : Digvijay | Comment RSS | Category : Third Semester
Tag : ,

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

Wordpress DMCA
Community | Toolbar | Android App | Founder/Developer : Hari Prasad Chaudhary | CSIT Portal Manager : Digvijay Chaudhary