C++:: Friend Function
A Function Friendly To Two Classes
#include /iostream.h/
class class2; //forward declaration
class class1
{
int x;
public:
void setvalue(int i)
{
x=i;
}
friend void max(class1,class2); //common friend function
};
class class2
{
int a;
public:
void setvalue(int i)
{
a=i;
}
friend void max(class1,class2); //common friend function
};
void max(class1 m,class2 n) //definition of friend
{
if(m.x>=n.a)
cout<< "m is max:" << m.x;
else
cout<< "n is max:" <<>
}
int main()
{
class1 c1;
class2 c2;
c1.setvalue(10);
c2.setvalue(20);
max(c1,c2); //greater then
return 0;
}
0 comments:
Post a Comment