How Learn Friend Function Tccicomputercoaching.com

In Object Oriented Programming non-member function can’t access private or protected data of class. This is good for security purpose but to follow this data hiding user has to write very lengthy and complex data, so to access this private and protected data there is one facility, that’s called friend function. So, the complier knows a given function is a friend function by the use of the keyword friend.

A friend function of a class is defined outside that class’ scope but it has the right to access all private and protected members of the class.

A friend function is declared by the class that is granting access, so friend functions are part of the class interface, like methods. Friend functions allow alternative syntax to use objects, for instance f(x) instead of x.f(), or g(x,y) instead of x.g(y). Friend functions have the same implications on encapsulation as methods.

Declaration of friend function in C++

classclass_name

{

    … .. …

friendreturn_typefunction_name(argument/s);

    … .. …

}

Definition of Friend Function in C++

classclassName

{

    … .. …

friendreturn_typefunctionName(argument/s);

    … .. …

}

return_typefunctionName(argument/s)

{

    … .. …

    // Private and protected data of className can be accessed from

    // this function because it is a friend function of className.

    … .. …

}

Here, now function can access the private and protected data of that class.

To make function friend we can use friend keyword before function name.

class B;

class A {

private:

int num1;

public:

A(): num1(12) { }

      // friend function declaration

friendint add(A, B);

};

class B {

private:

int num2;

public:

B(): num2(1) { }

       // friend function declaration

friendint add(A , B);

};

// Function add() is the friend function of classes A and B

// that accesses the member variables num1 and num1

int add(A objectA, B objectB)

{

return (objectA.num1 + objectB.num2);

}

int main()

{

A a1;

Similarly, like a friend function, a class can also be made a friend of another class using keyword friend. For example:

… .. …

class B;

class A

{

   // class B is a friend class of class A

friend class B;

   … .. …

}

class B

{

   … .. …

}

    B b1;

cout<<“Sum: “<< add(a1, b1);

return 0;

}

Output

Sum: 13

When a class is made a friend class, all the member functions of that class becomes friend functions.

Here, member function of class B can access all protected and private data of Class A.

If you like this post then please share and like this post.

To know more about Computer class in Ahmedabad, Computer course in Ahmedabad, TCCI

Call us @ 98256 18292.

To learn more visit us @ http://tccicomputercoaching.com/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s