WELCOME IN THE SEA OF C

Here you find some good quality programs.
If any one want any type of help in C and C++ then you can mail me.
Google

Wednesday, February 11, 2009

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: