Friend Function
Friend
function:
A friend function is a non member function
which can access the private, protected members of the class.
Syntax:
friend return_type
function_name(arguments);
Characteristics of Friend Function.
· Friend function is a non member function
which can access private, protected members of the class.
· Friend functions are declared inside the
class by using keyword friend.
· This function is defined outside the class
without using keyword friend and scope resolution operator.
· Friend functions are invoked like a non
member function.
· Arguments of these functions are objects.
· A class can have more than one friend
function.
· A friend function can friend to more than
one class.
Example:
class simple
{
private:
int
a;
public:
void
input();
void
display();
friend
void square(simple);
};
void simple::input()
{
cin>>a;
}
void simple::display()
{
cout<<a;
}
void square(simple x)
{
a=x.a*x.a;
}
void main()
{
simple
m,n;
m.input();
n=add(m);
n.display()
}
Comments
Post a Comment