OOP/CPP demonstrate how friend functions work

/*A C++ program to demonstrate how friend functions work
as a bridge between the classes.
Write a program to find the largest and smallest number
between two numbers of different classes.
*/
#include <iostream>
using namespace std;
class ABC; //forward declaration
class XYZ
{int x;
public:
void setvalue(int i) {x = i;}
friend void max(XYZ, ABC);
friend void min(XYZ, ABC);
};
class ABC
{int a;
public:
void setvalue(int i) {a = i;}
friend void max(XYZ, ABC);
friend void min(XYZ, ABC);
};
void max(XYZ m, ABC n) //definition of friend
{if(m.x >= n.a)
cout << “largest number is:”<<m.x<<endl;
else
cout << “largest number is:”<<n.a<<endl;
}
void min(XYZ m, ABC n) //definition of friend
{if(m.x <= n.a)
cout << “smallest number is:”<<m.x;
else
cout << “smallest number is:”<<n.a;
}
//————————————————————————–//
int main()
{
ABC abc;
abc.setvalue(10);
XYZ xyz;
xyz.setvalue(20);
max(xyz, abc);
min(xyz,abc);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>