Thursday, October 16, 2014

friend function in c++

friend function.

#include<iostream.h>

  #include<conio.h>

  class xyz;

  class abc

  {

public:

int x;

void getdata(int a)

  {

x=a;

}



};

  class xyz

  {

friend int max(abc,xyz);

int y;

public:

void getdata(int b)

  {

y=b;

}

friend int max(abc,xyz);

};

int max(abc a1,xyz x1)

{

if(a1.x>x1.y)

return a1.x;

else

return x1.y;

}

void main()

  {

  clrscr(); abc a2; xyz x2;

  a2.getdata(5);

  x2.getdata(10);

  cout<<"\n the  maximum  value is "<<max(a2,x2);

  getch();

}

Output:

the maximum value is 10

No comments: