---
title: "Please provide sample code of Friend Function in C++."  
description: "Please provide sample code of Friend Function in C++."  
author: "Sandra Emily"  
published: 2023-09-07  
updated: 2023-09-25  
canonical: https://www.mindstick.com/forum/159857/please-provide-sample-code-of-friend-function-in-c-plus-plus  
category: "visual c++"  
tags: ["c++", "functions", "friend function"]  
reading_time: 2 minutes  

---

# Please provide sample code of Friend Function in C++.

[Please provide sample](https://www.mindstick.com/forum/159382/how-can-i-change-an-element-s-class-with-javascript-please-provide-sample) [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) of [Friend Function](https://answers.mindstick.com/qa/92532/what-is-a-friend-function) in C++.

## Replies

### Reply by Aryan Kumar

In C++, friend functions are functions that are not members of a class but are granted access to the private and protected members of that class. They can be useful in scenarios where you need to allow external functions or classes to manipulate or access the private data of a class. Here's a [sample](https://www.mindstick.com/news/2174/mars-mission-by-nasa-prepares-for-tests) code that demonstrates how to declare and use a friend [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) in C++:

```plaintext
#include <iostream>
class MyClass {
private:
   int privateData;
public:
   MyClass(int data) : privateData(data) {}
   // Declare the friend function
   friend void FriendFunction(MyClass&);
   // Member function to access private data
   void DisplayPrivateData() {
       std::cout << "Private Data: " << privateData << std::endl;
   }
};
// Define the friend function
void FriendFunction(MyClass& obj) {
   // The friend function can access private members of MyClass
   obj.privateData += 10;
}
int main() {
   MyClass myObject(42);
   myObject.DisplayPrivateData(); // Display the initial private data
   // Call the friend function to modify privateData
   FriendFunction(myObject);
   myObject.DisplayPrivateData(); // Display the modified private data
   return 0;
}
```

In this example:

1. We have a **MyClass** class with a private data member **privateData**.
2. We declare the **FriendFunction** as a friend of the **MyClass** class by including the **friend** keyword in the class declaration.
3. The **FriendFunction** is defined outside the class and can access the private member **privateData** of **MyClass**.
4. In the **main** function, we create an instance of **MyClass** and demonstrate how the friend function can modify the private data of the class.

Please note that while friend functions can be useful in certain situations, they should be used sparingly, as they can break encapsulation and make the code less maintainable if overused. It's generally recommended to prefer member functions and accessor methods (getters and setters) for interacting with the private data of a class when possible.


---

Original Source: https://www.mindstick.com/forum/159857/please-provide-sample-code-of-friend-function-in-c-plus-plus

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
