C++ provides us a function which is a non-member function
and it can still access the public, as well as private members of
the class. This function is known as the friend function.
A friend function can also be declared in multiple classes and
it can even
access the public and private members of all the classes, in which it is declared.
Important points about the friend function
A friend function can be declared in either the private or public section of a class, without changing its meaning.
A friend function can access the private and public data members and member functions of a class, in which it is declared.
Although a friend function is declared in a class but it is not a member of class, hence a friend function is called like a normal function i.e. without the object of the class.
In general, a friend function has object as its arguments i.e. object of classes in which it is declared as friend.
A friend function example
Let us see how to create a friend function in a program, which can still access the public, as well as private members of
the class.
#include<iostream>
using namespace std;
class A
{
private:
int a;
public:
int b;
int result;
//Declaration of a friend function
friend int add(A);
};
//Definition of a friend function
int add(A ob)
{
ob.a = 10;
ob.b = 20;
ob.result = ob.a+ ob.b;
return ob.result;
}
int main()
{
A ob;
cout<< "The sum of a and b is : " << add(ob);
}
Output
The sum of a and b is : 30
Program Analysis
In the program above, we have declared a friend function add(A) in class A. Although this friend function was declared in the class A but
the friend function is called just like a normal function i.e. only by its name and without any object of class A.
Advertisement
A friend function common to multiple classes
Let us see how to create a friend function, which is declared as friend function in two classes. This friend function can
access the public, as well as private members of
the classes in which it is classes.
#include<iostream>
using namespace std;
//Forward declaration of class B
class B;
class A
{
private:
int a;
public:
void setA()
{
a = 20;
}
//A friend function declaration
friend int add(A,B);
};
class B
{
private:
int b;
public:
void setB()
{
b = 20;
}
//A friend function declaration
friend int add(A,B);
};
//A friend function definition
int add(A ob1, B ob2)
{
int result = ob1.a+ ob2.b;
return result;
}
int main()
{
A ob1;
ob1.setA();
B ob2;
ob2.setB();
cout<< "The sum of a and b is : " << add(ob1, ob2);
}
Output
The sum of a and b is : 40
Program Analysis
As you can see in the program just above, we have declared a friend function named add(A,B) in class A and class B.
The friend function add(A,B) takes objects of class A and class B as arguments.
As you can see in the beginning of this program, we have declared the class B before defining it.
class B;
This makes it possible for us to declare the friend function add(A,B),
which takes objects of class A and class B. Declaring a class before defining it, known as forward declaration. Without this forward declaration
of class B, compiler will issue an error.
A friend function, which is common to multiple classes and takes objects of these classes as arguments, must be defined after defining these classes or else, the
compiler will issue an error. Hence, the friend function add(A,B) is defined after defining class A and B.