Using Friend Function
#include <stdafx.h>
#include <iostream>
#include <string>
using namespace std;
class TestFriend
{
// Using friend function.
int i; // Here i is a private member of the class TestFriend.
public:
friend void display(int);
};
void display(int a) //A non-member function can access the private and protected members of a class if it is declared a friend of that class.
{
TestFriend tf;
tf.i=a; // Trying to access the private member i.
cout<< "Value of the private member :" << tf.i;
}
int main()
{
display(100);
return 0;
}
Output:
Value of the private member :100