Home > DeveloperSection > Forums > How to create friend function in c ?
marcel ethan
marcel ethan

Total Post:105

Points:735
Posted on    June-14-2013 11:57 PM

 Visual C++ Visual C++ 
Ratings:


 1 Reply(s)
 819  View(s)
Rate this:
Hi Expert,

How to create friend function in c++?

Thanks in advance. 


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    June-15-2013 4:41 AM

you can create friend function from follow syntex
friend type function_name(classname object);
you can try following example of friend function

class myclass{
int n,d;
public:
myclass(int i,int j){ n = i; d = j;}
//declare a friend of myclass
friend int isfactor(myclass ob);
};
int isfactor(myclass ob)
{
 if(!(ob.n % ob.d)) return 1;
else return 0;
}
int main( )
{
myclass ob1(10,2),ob2(13,3);
if(isfactor(ob1)) cout<<"2 is a factor of 10\n";
else cout <<"2 is not a factor of 10\n";
if(isfactor(ob2) cout<<"3 is a factor of 13\n";
else cout<<"3 is not a factor of 13\n";
return 0;
}

Don't want to miss updates? Please click the below button!

Follow MindStick