---
title: "Friend function and friend class in C++"  
description: "In this blog, I’m explaining about friend function and friend class in C++Friend function1. Friend function can access the private or protected member"  
author: "priyanka kushwaha"  
published: 2015-01-30  
updated: 2015-01-30  
canonical: https://www.mindstick.com/blog/750/friend-function-and-friend-class-in-c-plus-plus  
category: ".net"  
tags: ["visual c++"]  
reading_time: 1 minute  

---

# Friend function and friend class in C++

In this blog, I’m explaining about [friend function](https://answers.mindstick.com/qa/92532/what-is-a-friend-function) and friend [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) in [C++](https://www.mindstick.com/blog/745/array-in-c-plus-plus)

##### Friend function

1. Friend function can [access the private](https://www.mindstick.com/interview/2468/can-you-access-the-private-method-from-outside-the-class) or protected members of the class in which they are declared to be friend, but they can use the members for a specific object.

2. Friend can be declared anywhere in the class.

##### Friend class

A class can also be declared to be the friend of some other class. When we create a friend class then all the member [functions](https://www.mindstick.com/forum/160140/explain-the-role-of-functions-as-a-service-faas-in-serverless-computing) of the friend class also become the friend of the other class. This requires the [condition](https://www.mindstick.com/forum/12711/select-query-with-and-condition) that the friend becoming [must](https://www.mindstick.com/articles/12978/top-reasons-why-every-magento-ecommerce-store-must-opt-for-mobile-app) be first declared or defined.

##### Example:

```
// FriendProgram.cpp : main project file. #include "stdafx.h"#include <iostream>using namespace std;using namespace System;class ADemo{public: //Declare  a friend class            friend class DriveClass ;            void get()            {                        cout<<"Enter two values";                        cin>>val1>>val2;            }            //Declare a friend function            friend float mean(ADemo objDemo);            ADemo();            ~ADemo();private:int val1,val2; };class DriveClass{public:            //Access private members using reference            void change(ADemo obj)            {                        obj.val1=20;                        cout<<"val1=";                        cout<<obj.val1;                        } };float mean(ADemo objDemo){            return float(objDemo.val1+objDemo.val2)/2;}ADemo::ADemo(){} ADemo::~ADemo(){} int main(array<System::String ^> ^args){                       ADemo obj;      obj.get();              cout<<"\n mean value is :"<<mean(obj);            DriveClass dreobj;            dreobj.change(obj);              Console::ReadKey(0);    return 0;}
```

---

Original Source: https://www.mindstick.com/blog/750/friend-function-and-friend-class-in-c-plus-plus

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
