Introduction to Friend Function in C++
In C++, a friend function is a function that is declared using the friend keyword to achieve the encapsulation feature and can access the private and protected data members easily by using functions. To access the data in C++, friend functions are declared inside the body of the class or inside the private and public section of the class. Let us see how to declare and use a Friend Function in C++ in this article.
A friend function while declaring is preceded with the “friend” keyword as shown here:
Syntax:
class <className>{
<few lines of code goes here>
private <variable>
protected <variable>
friend <returnDatatype> <functionName>(arguments list);
}
returnDatatype functionName(arguments list){
//function from which protected and private keywords
//can be accessed from as this is a friend method of className
}
As shown in the above code, the friend function needs to be declared in the same class where the protected or private keyword has been declared for those data to be accessible outside the class. This function is allowed to be declared anywhere in the entire program, just like a normal C++ method. The function definition does not require keywords like friends or any scope resolution operators.
Examples of Friend Function in C++ Program
Let us check out the working of the friend function a little better by taking a few examples below.
Example #1
Code:
/* C++ program which exhibits the working of friend function.*/
#include <iostream>
using namespace std;
class Weight
{
private:
int kilo;
public:
Weight(): kilo(0) { }
//Declaration of a friend function
friend int addWeight(Weight);
};
// Defining a friend function
int addWeight(Weight w)
{
//accessing private data from non-member function
w.kilo += 17;
return w.kilo;
}
int main()
{
Weight W;
cout<<"Weight: "<< addWeight(W);
return 0;
}
Output:
Here the friend function is the addWeight() method, which is declared inside the Weight class. Kilo is the private keyword declared inside the Weight method, which is then accessed from the addWeight function because of it. This example was just to showcase a friend’s basic usage, although there is no real-time usage here. Let us now deep dive into some meaningful examples.
Example #2
Code:
#include <iostream>
using namespace std;
// Forward declaration
class SecondClass;
class FirstClass {
private:
int first_num;
public:
FirstClass(): first_num(12) { }
// Declaring a friend function
friend int divide(FirstClass, SecondClass);
};
class SecondClass {
private:
int sec_num;
public:
SecondClass(): sec_num(4) { }
// Another friend declaration
friend int divide(FirstClass , SecondClass);
};
// Function divide() is the friend function of classes FirstClass and SecondClass
// that accesses the member variables first_num and sec_num
int divide(FirstClass fnum, SecondClass snum)
{
return (fnum.first_num / snum.sec_num);
}
int main()
{
FirstClass fnum;
SecondClass snum;
cout<<"The value got by dividing first by second number: "<< divide(fnum, snum);
return 0;
}
Output:
In this example, both the classes FirstClass and SecondClass have divide() declared as a friend function. Hence this function can access the private variables data from both the class. Here divide() function is used to add private variables first_num and sec_num of two objects fnum and snum and returns its value to the main method.

4.5 (8,205 ratings)
View Course
For this to function properly, a forward declaration for SecondClass needs to be done as shown in the code because SecondClass is referenced within FirstClass using the program:
friend int divide(FirstClass , SecondClass);
Friend Class: There is a friend class just like the friend function. The friend class can also access both private and protected variables of the class as it is a friend to it.
Syntax:
class One{
<few lines of code here>
friend class Two;
};
class Two{
<few lines of code>
};
As shown above, class Two is a friend of class One. Hence class Two can access the private and protected variables of class One. But class One cannot access protected or private variables of class Two because this is not a mutual friendship. For mutual friendship, we should declare it explicitly. In the same way, the class’s friendship is not inherited, meaning that class Two shall not be a friend of the subclasses of class One even though it is a friend of class One.
Example #3
Code:
#include <iostream>
#include <string>
using namespace std;
class Perimeter{
int len,brd,perimeter,temp;
public:
Perimeter(int len,int brd):len(len),brd(brd)
{}
void calcPerimeter(){
temp = len + brd;
perimeter = 2 * temp;
}
friend class printClass;
};
class printClass{
public:
void printPerimeter(Perimeter a){
cout<<"Perimeter = "<<a.perimeter;
}
};
int main(){
Perimeter a(10,15);
a.calcPerimeter();
printClass p;
p.printPerimeter(a);
return 0;
}
Output:
In this code, we have 2 classes: Perimeter class which finds the perimeter by using the length and breadth values. Variables len, brd, perimeter, and temp are all private variables of the class perimeter. Hence we need to make printClass a friend of the Perimeter class. This printClass uses the Perimeter value calculated in the function calcPerimeter() in the Perimeter class. As they all are private members, we have to make printPerimeter a friend of the Perimeter class. Once this is done, we have to create an object in the main class to calculate the perimeter and pass this object to the printPerimeter class to display the Perimeter.
Features of Friend Function in C++
- The method and the class to which it has been declared as a friend need not be the same.
- Since it is not in the respective class’s scope, it cannot be called by using its object.
- It can also be called just like a normal method, even without the usage of the object.
- It can only directly access the member names by using its object name and dot membership operator along with its member name.
- There is no restriction as it is allowed to be declared in either the private or the public part.
Conclusion
Considering all the above-discussed features and the examples of friend function in C++, one must also be careful while using a friend’s functions with numerous functions and external classes as it may lessen the importance of encapsulation of different classes in object-oriented programming. Hence it can be both a boon and a bane to the programmer.
Recommended Articles
This is a guide to Friend Function in C++. Here we discuss the Introduction to Friend function in C++ and its Examples along with Code Implementation and Output. You can also go through our suggested articles to learn more –