Write a program to demonstrate the use of a friend function.
#include<iostream> using namespace std; class A { private: int x; public: friend class B; }; class B { public: void setdata(A&); void print(A&); }; void B :: setdata(A& t) { cout << "Enter value for x = "; cin >> t.x; } void B :: print(A& t) { cout << "x = " << t.x << endl; } int main() { A obj_a; B obj_b; obj_b.setdata(obj_a); obj_b.print(obj_a); return 0; }
Output
Enter value for x = 55 x = 55