KEMBAR78
Friend Function and Class | PDF | Scope (Computer Science) | C++
0% found this document useful (0 votes)
5 views4 pages

Friend Function and Class

Uploaded by

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

Friend Function and Class

Uploaded by

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

Friend Function

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.

A simple and complete C++ program to demonstrate friend function of another class.

#include <iostream>

class B;

class A {
public:
void showB(B);
};

class B {
private:
int b;

public:
B() { b = 0; }
friend void A::showB(B x); // Friend function
};

void A::showB(B x)
{
// Since showB() is friend of B, it can
// access private members of B
std::cout << "B::b = " << x.b;
}

int main()
{
A a;
B x;
a.showB(x);
return 0;
}
Output:
B::b = 0

A simple and complete C++ program to demonstrate global friend.

#include <iostream>

class A {
int a;

public:
A() { a = 0; }

// global friend function


friend void showA(A&);
};

void showA(A& x)
{
// Since showA() is a friend, it can access
// private members of A
std::cout << "A::a=" << x.a;
}

int main()
{
A a;
showA(a);
return 0;
}
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.

A simple and complete C++ program to demonstrate friend Class.


#include <iostream>
class A {
private:
int a;

public:
A() { a = 0; }
friend class B; // Friend Class
};

class B {
private:
int b;

public:
void showA(A& x)
{
// Since B is friend of A, it can access
// private members of A
std::cout << "A::a=" << x.a;
}
};

int main()
{
A a;
B b;
b.showA(a);
return 0;
}
Output: A::a=0

Advantages/Disadvantages ff 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