Exforsys.com
 

Sponsored Links

 

C++ Tutorials

 
Home Tutorials C++
 

C++ Friend Functions

 

C++ Friend Functions

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.



Need for Friend Function:

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.


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.


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 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.



Read Next: C++ Static Functions



 

 

Comments


RAJENDRA PANDA said:

  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
December 6, 2007, 5:22 am

Karthik B.S. said:

  //friend int compute(exforsys e1)

A semi-colon is missig in the above statement.


//cout
February 21, 2008, 3:27 am

Suresh Ragala said:

  Friend fuction is a non member function which can access the private ,protected members and methods of the class at outside.
July 29, 2008, 9:21 am

Masrat Ahmad Wani said:

  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.
September 24, 2008, 3:26 pm

S.Sameer said:

  In the defination of friend function value returned is (e1.a e2.b)-5 in this expression object e2 reference is not resolved.
September 27, 2008, 5:37 am

sankar said:

  how to create pointers objects as arguments to the friend functions?
October 30, 2008, 3:15 am

pushpendra said:

  int compute(exforsys *e1)
{
//Friend Function Definition which has access to private data
return int(e1->a+e1->b)-5;
}


November 11, 2008, 12:27 pm

Pricy said:

  #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.....
January 30, 2009, 1:32 am

ritesh patel said:

  friend function should not contain ; semicolon
February 10, 2009, 6:22 am

Jags said:

  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);
}
February 10, 2009, 9:15 pm

balaji said:

  Friend function can be used to access private data member, without using object
March 20, 2009, 7:12 am

manab said:

  How to calculate the multiplication of two complex number using friend function?
March 27, 2009, 3:39 pm

dappu said:

  Friend function is a function used to access the private data member and member function from out side the class.
April 22, 2009, 12:07 am

Vivek Kumar said:

  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).
May 5, 2009, 2:55 am

anass said:

  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.
July 8, 2009, 5:32 am

rubab said:

  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"
July 20, 2009, 12:10 am

Girish Pounikar said:

  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.
August 8, 2009, 12:30 am

harkesh saini said:

  class abc
{
......
......
public:
......
......
friend void xyz(void); //declaretion
};
August 13, 2009, 10:32 am

muhammad ijaz said:

  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
August 14, 2009, 4:11 pm

Stefan said:

  Can a friend function be drawn as a dependency relationship in a UML diagram? or is more of an association?

Regards
September 3, 2009, 12:18 pm

pinky said:

  can member function of one class can be a firend of another class?if yes then how?explain this with an example
September 16, 2009, 3:46 am

Deepak said:

  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?
September 22, 2009, 1:04 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape