0% found this document useful (0 votes)
7 views4 pages

Friend Fun

A friend function in C++ allows access to a class's private and protected members, declared with the 'friend' keyword. It can be called like a normal function and is not scoped within the class, requiring an object to access class members. Similarly, a friend class can access private members of another class, but friendship is not mutual or inherited, and excessive use can compromise encapsulation.

Uploaded by

ajju10080
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Friend Fun

A friend function in C++ allows access to a class's private and protected members, declared with the 'friend' keyword. It can be called like a normal function and is not scoped within the class, requiring an object to access class members. Similarly, a friend class can access private members of another class, but friendship is not mutual or inherited, and excessive use can compromise encapsulation.

Uploaded by

ajju10080
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Friend Function In C++

A friend function in C++ is a function that is preceded by the keyword “friend”. When
the function is declared as a friend, then it can access the private and protected data
members of the class.

A friend function is declared inside the class with a friend keyword preceding as
shown below.
class className{

……

friend returnType functionName(arg list);


};
As shown above, the friend function is declared inside the class whose private and
protected data members are to be accessed. The function can be defined anywhere in
the code file and we need not use the keyword friend or the scope resolution, operator.

There are some points to remember while implementing friend functions in our
program:
 A friend function can be declared in the private or public section of the class.
 It can be called like a normal function without using the object.
 A friend function is not in the scope of the class, of which it is a friend.
 A friend function is not invoked using the class object as it is not in the scope
of the class.
 A friend function cannot access the private and protected data members of the
class directly. It needs to make use of a class object and then access the
members using the dot operator.
 A friend function can be a global function or a member of another class.
Example Of A Friend Function
Let us implement a programming Example to better understand the usage of
Friend Function.
1 #include <iostream>
#include <string>
2
using namespace std;
3 class sample{
4 int length, breadth;
5
6 public:
7 sample(int length, int breadth):length(length),breadth(breadth)
{}
8 friend void calcArea(sample s); //friend function declaration
9
10 };
11
12
13 //friend function definition
void calcArea(sample s){
14 cout<<"Area = "<<s.length * s.breadth;
15 }
16 int main()
17 {
sample s(10,15);
18 calcArea(s);
19
20 return 0;
21 }
22
23
Output:
Area = 150

In the above program, we have a class sample with private members length and
breadth. We have a public constructor that initializes the values of length and breadth.
Next, we have a friend function “calcArea” that calculates the area by taking length
and breadth into account.

Note that calcArea is a friend function and is a not part of the class. In the main
function, after creating an object of the class sample, we pass it to the calcArea
function that calculates area and displays the value.

Friend Class
Just like friend functions, we can also have a friend class. Friend class can access
private and protected members of the class to which it is a friend.

class A{
……
friend class B;
};
class B{
……..
};
As depicted above, class B is a friend of class A. So class B can access the private and
protected members of class A.

But this does not mean that class A can access private and protected members of the
class B. Note that the friendship is not mutual unless we make it so.
Similarly, the friendship of the class is not inherited. This means that as class B is a
friend of class A, it will not be a friend of the subclasses of class A.

Let us take a programming example to demonstrate the friend class.


1
2
3 #include <iostream>
4 #include <string>
5 using namespace std;
class Area{
6 int length,breadth,area;
7
8 public:
9
10 Area(int length,int breadth):length(length),breadth(breadth)
11 {}
12 void calcArea(){
area = length * breadth;
13 }
14
15 friend class printClass;
16
17 };
18 class printClass{
19
20 public:
void printArea(Area a){
21 cout<<"Area = "<<a.area;
22 }
23 };
24 int main(){
Area a(10,15);
25 a.calcArea();
26 printClass p;
27 p.printArea(a);
28
29 return 0;
30 }
31
32
Output:
Area = 150

In this program, we have two classes. The Class “Area” which calculates the area
using the length and breadth parameters. Note that the fields, area, length, and breadth
are all private members of the class Area.
The next class that is used is the “printClass” that prints the area calculated by a
function calcArea in the Area class. As the members are private, we need to make
printClass a friend of Area class.

Once that is done, in the main function we create an object of the Area class, calculate
the area and pass the object to printArea function of printClass class to display the
area.

Advantages/Disadvantages Of Friend Function


One advantage of the friend function or class is that we can access the private and
protected data of the class. For Example, if we are implementing a linked list, then
we can make the linked list class as a friend of the node class and access its data as the
linked list consists of nodes.
Thus in a certain situation using a friend function or class can prove to be
advantageous. However, it has some disadvantages as well.

One of the distinguishing features of C++ is encapsulation i.e. bundling of data and
functions operating on that data together so that no outside function or class can
access the data. But by allowing the friend functions or class to access the private
members of another class, we actually compromise the encapsulation feature.

In order to prevent this, we should be careful about using friend functions or class. We
should ensure that we should not use too many friend functions and classes in our
program which will totally compromise on the encapsulation.

You might also like