Google News
logo
CPP - Interview Questions
What is a Friend Function in C++?
A friend function in C++ is defined as a function that can access private, protected and public members of a class.
 
The friend function is declared using the friend keyword inside the body of the class.
 
Friend Function Syntax :
class className {
    ... .. ...
    friend returnType functionName(arguments);
    ... .. ...
}
By using the keyword, the ‘friend’ compiler knows that the given function is a friend function.
 
We declare friend function inside the body of a class, starting with the keyword friend to access the data. We use them when we need to operate between two different classes at the same time.
Advertisement