Program To Illustrate The Concept Of Friend Function:
#include<iostream>
using namespace std;
class test;
class sample
{
int x;
public:
sample()
{
cout<<"\nEnter The Value Of X ";
cin>>x;
}
friend void max(sample &a,test &b);
};
class test
{
int y;
public:
test()
{
cout<<"\nEnter The Value Of Y ";
cin>>y;
}
friend void max(sample &a,test &b);
};
void max(sample &a,test &b)
{
if(a.x>b.y)
{
cout<<a.x<<" is Greater Than "<<b.y;
}
else
{
cout<<b.y<<" is Greater Than "<<a.x;
}
}
main()
{
sample c;
test d;
max(c,d);
}
#include<iostream>
using namespace std;
class test;
class sample
{
int x;
public:
sample()
{
cout<<"\nEnter The Value Of X ";
cin>>x;
}
friend void max(sample &a,test &b);
};
class test
{
int y;
public:
test()
{
cout<<"\nEnter The Value Of Y ";
cin>>y;
}
friend void max(sample &a,test &b);
};
void max(sample &a,test &b)
{
if(a.x>b.y)
{
cout<<a.x<<" is Greater Than "<<b.y;
}
else
{
cout<<b.y<<" is Greater Than "<<a.x;
}
}
main()
{
sample c;
test d;
max(c,d);
}