C++ Programming

Accessing Private members using Friend Function of a different class

Accessing Private data members of a class from a different class’s member function using Friend Function.

Language : C++

[sourcecode language='c++']
// friend function accessing private members of different class
#include
class employee;
class account
{
public:
int computesal(employee e);
};

class employee
{ int empid;
int deptno;
int sal;

public:
void getdata();
friend int account :: computesal(employee e);
};

void employee :: getdata()
{
cout<<"Enter EmpID : ";
cin>>empid;
cout<<"Enter deptNo : ";
cin>>deptno;
cout<<"Enter sal : ";
cin>>sal;
}
int account :: computesal(employee e)
{
return(e.sal=e.sal+1000);
}
void main()
{
employee e[3];
for(int i=0;i<3;i++)
{
e[i].getdata();
account a;
cout<<“\nTotal Salary : “< }
}
[/sourcecode]



Leave a Comment

Notify me of followup comments via e-mail. You can also subscribe without commenting.


WordPress - Vaibhav Kanwal