Without Using Friend Function
#include <stdafx.h>
#include <iostream>
#include <string>
using namespace std;
class TestFriend
{
// Without using friend function.
int i; // Here i is a private member of the class TestFriend.
};
void display(int a)
{
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:
error C2248: 'TestFriend::i' : cannot access private member declared in class 'TestFriend'