Sunday 29 March 2015

Specific Member Function of one class is friend to another class

When only specific member function of one class should be friend function of another class, it must be specified explicitly using the scope resolution operator as shown below.

Friend friend-classname :: friend-functionname(object as argument)

Sample Program

#include <iostream.h>
class B;
class A
{
int a;
float b;
public:
void show1(void)
{
cout<<a;
}
void show2(void)
{
cout<<b;
}
void print(void)
{
cout<<a<<b;
}
friend B::getdata(A ob);
};
class B
{
public:
void getdata(A ob1, A ob2)
{
ob1.a = 5;
ob2.b = 5.5;
}
void print(A ob1, A ob2)
{
return(ob1.a+ob2.b);
}
};
void main()
{
A x;
B y;
y.getdata(x);
y.print(x);
x.show1();
x.show2();
x.print();
}

No comments:

Post a Comment