source file name: friend2.cpp

#include<stdio.h>

#include<conio.h>

#include<iostream.h>

class Y;

class X

{

int a;

public:

void setno(int m)

{

a=m;

}

friend void max(X,Y);

};

class Y

{

int b;

public:


void setno(int n)

{

b=n;

}

friend void max(X,Y);

};

void max(X x, Y y)

{

if(x.a>y.b)

cout<<"\n Maximum value is:   "<<x.a <<" from X class ";

else

cout<<"\n Maximum value is:  "<<y.b << " from Y class;

}


void main()

{

clrscr();

cout<<"Friend function program: ";

X ob1;

Y ob2;

ob1.setno(100);

ob2.setno(50);

max(ob1,ob2);

getch();

}

output:

1. Friend function program: 

Maximum value is: 100 from X class 

2. call ob1.setno(10); and ob2.setno(150); 

Friend function program: 

Maximum value is: 150 from Y class