What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

C++ Friend Function: Syntax, Features & Uses (with code)

  • May 03, 2023
  • 6 Minute Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Abrar Ahmed
C++ Friend Function: Syntax, Features & Uses (with code)

In OOPs, data hiding is an essential idea that limits outsiders to access private data and protects private data and information. There comes the concept of friend function in C++, which we willl understand in this article with its example and characteristics.

What is a friend function in C++?

A friend function in C++ is a type of function that is used to grant permissions to access private and protected members of a class only. Beyond the confines of the class, they are defined universally. The keyword "friend" is used before the function to declare it as a friend function. 

Note that friend functions aren't class member functions. It is a type of keyword that is defined outside of a class but has access to the class's protected and private members.

There could be times when coders wish for two classes to share members in the programmer. These members could be function templates, class functions, or data members. In these situations, we add the needed function as a friend to both classes to access the members of the class' secret and protected data.

In general, non-member functions are unable to access a class's secret members including the private and protected ones once it has been designated as a friend function.

The syntax of the friend function in C++ is as follows:

class MyClass {


int privateData;


friend void friendFunction(MyClass& myObject);



Features of a friend function in C++

Following are the basic Characteristics of Friend function that you must know about:

  1. Accessing private members of a class: In C++, a friend function helps to access the private members of a class, which allows the user to manipulate the object’s state in ways that are not normally possible.
  2. Not a member function: A function that can be designated as a friend of the class but not a member of that class. Instead, the friend function is a stand-alone function that grants access only to the members of the class.
  3. Declared inside a class: It is possible to declare it outside the class definition using the friend keyword followed by a certain function prototype. But declaring it inside the class definition is suggested to be the best method for the same.
  4. A friend function cannot be inherited: Just because a class is designated as a friend of another class, it does not follow that its subclasses will also be friends with the second class.
  5. Doesn’t contain this pointer: A friend function does not have this pointer, since it is not a member of the class function. Instead, it runs according to the parameters that have been set.

Example of Friend function in C++

Below we have given an example program for Friend Function:


class MyClass {


int privateData;


MyClass() : privateData(0) {}

friend void friendFunction(MyClass& myObject);


void friendFunction(MyClass& myObject) {

myObject.privateData = 42;

std::cout << "Friend function has set privateData to " << myObject.privateData << std::endl;


int main() {

MyClass myObject;


return 0;




Friend function has set privateData to 42


In the above example, the friend function is declared as “friendFunction”, inside the class of “MyClass”. This simply means that the function can be accessed only by private members of the class. In the friendFunction implementation, we set the privateData member of the myObject parameter to 42, which is allowed because friendFunction is a friend of MyClass.

Then, we create an instance of MyClass and call a friend function on it. This simply sets the private data member of the class to 42 and the program delivers the output as a message “ Friend function” has set privateData to 42” to the console.

What are the uses of the friend function in C++?

Friend Function is very useful and offers a variety of benefits as discussed below:

  • Accessing private members: In C++, a friend function helps to access the private members of a class, which allows the user to manipulate the object’s state in ways that are not normally possible.
  • Better Encapsulation: With the help of a friend function, individuals can restrict access to the whole implementation and provide access only to a set of functions that needs access by the user.
  • Overloading of operators: To overload the operators, friend functions are utilized. For instance, one may overload the + operator to perform the addition of a class together. So, to access the private members of the object, the + operator has to be declared as a friend function.
  • Avoiding getter and setter methods: While programming, there could be times when an individual wishes to give direct access to a class’s private member without using getter or setter methods. This direct access can be offered by a friend function while yet maintaining encapsulation.
  • Performance improvement: In some circumstances, individuals can enhance performance by avoiding the overhead of function calls and utilizing a friend function to access the secret elements of a class.


Overall, this innovative C++ friend function breaks data concealment in an object-oriented programming language. Happy Learning :)

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Abrar Ahmed
An ambivert individual with a thirst for knowledge and passion to achieve. Striving to connect Artificial Intelligence in all aspects of life. I am also an avid coder and partake in coding challenges all the time on Leetcode and CodeChef.