Accessing data members of a Private Class using a normal function outside the scope of the class by making it a friend function.
Language : C++
[sourcecode language='c++']
// friend function using a normal function
#include
class employee
{ int empid;
int deptno;
int sal;
public:
void getdata();
friend void computesal(employee e);
};
void employee :: getdata()
{
cout<<"Enter EmpID : ";
cin>>empid;
cout<<"Enter deptNo : ";
cin>>deptno;
cout<<"Enter sal : ";
cin>>sal;
}
void computesal(employee e)
{
e.sal=e.sal+1000;
cout<<“\nTotal Sal : “<
void main()
{
employee e1,e2;
e1.getdata();
computesal(e1);
e2.getdata();
computesal(e2);
}
[/sourcecode]






