Friend Function in C Plus Plus
Friend Function in C Plus Plus
FRIEND FUNCTION
#include<iostream.h>
#include<conio.h>
class sum
{
inta,b;
public:
voidget_ab()
{
cout<<"enter the values of a and b:";
cin>>a>>b;
}
friend void add(sum s);
};
void add(sum s)
{
cout<<"\n sum of a and b is:"<<s.a+s.b;
}
void main()
{
clrscr();
sum s1;
s1.get_ab();
add(s1);
getch();
}
FRIEND FUNCTION
#include<iostream.h>
#include<conio.h>
class testing
{
inta,b;
public:
voidget_ab()
{
cout<<"enter values of a and b:";
cin>>a>>b;
}
friendint max(testing t1);
};
int max(testing t1)
{
if(t1.a>t1.b)
{
return t1.a;
}
else
{
return t1.b;
}
}
void main()
{
clrscr();
testingobj;
obj.get_ab();
cout<<"MAXIMUM VALUE IS:"<<max(obj);
getch();
}