Need a project done?

C++ Programming Developer

Search This Blog

Friend Function C++

Friend Function in C++ Program is given below:


//Friend Function program in C++
#include <iostream>
using namespace std;

class a{
friend void f(a &obj);
public:
int getData(){
return a_mem;
}
private:
int a_mem;
};

void f(a &obj){//This function being friend function has the access to private members of passed object (obj) of class 'a'.
obj.a_mem = 5; //You cannot do a_mem = 5; without passing any argument, since function is not a member of class.
}


int main(){
a obj;
f(obj);
cout << obj.getData() << endl;
return 0;
}
"Don't let anyone ever make you feel like you don't deserve what you want."