Friend function in a class, friend function, Program of Friend function
How can a friend function common to two different classes be declared in a C++ program?

In Object Oriented Programming (OOP's), friend functions are not considered class members; they are considered as normal external functions that are given special access privileges for class data members and member functions. Friends are not in the class scope and are not called using the member-selection operators (. and ->) unless they are members of another class. 

Any data members which is declared private inside a class is not accessible from outside the class. So a function which is not a member or an external class can never access such private data members. But there may be some cases in real life, where a programmer will need access to the private data from non-member functions or from external classes. C++ offers some exceptions in such cases. 
A class in C++ can allow non-member functions and other classes to access its own private data, by making them as friends. This part of C++ tutorial essentially gives two important points. Once a non-member function is declared as a friend, it can access the private data of the class similarly when a class is declared as a friend, the friend class can have access to the private data of the class which made this a friend.


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: