
C++ Program without friend function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> using namespace std; class one { private: int n1=1; public: int add(one object1) { return (object1.n1 + 1); } }; int main() { one object1; cout<<"Sum: "<< object1.add(object1); return 0; } |
C++ Program with friend function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> using namespace std; class one { private: int n1=1; public: friend int add(one); }; int main() { one object1; cout<<"Sum: "<< add(object1); return 0; } int add(one object1) { return (object1.n1 + 1); } |
How Friend function Can access private, protected, and public members of a classÂ