C++ Friend Functions
C++ Friend Functions
A class friend function is defined from outside class, but it does have access to all members of the private and protected class. With the definitions for friend functions occurring in the explanation of class, friends are not features of members.
The friend may be an attribute, a function template, or a member function, or a class or class framework, in which case friends are the entire class itself and its members.
To declare a function as a class friend, precede the function prototype with keyword friend in class definition as follows –
class Box { double width; public: double length; friend void printWidth( Box box ); void setWidth( double wid ); };
To declare all ClassTwo member functions as ClassOne mates, place the following declaration in the ClassOne description
friend class ClassTwo;
Consider the following program –
#include <iostream> using namespace std; class bag { double width; public: friend void printWidth( bag bag ); void setWidth( double wid ); }; // Member function definition void bag::setWidth( double wid ) { width = wid; } // Note: printWidth() is not a member function of any class. void printWidth( bag bag ) { /* Because printWidth() is a friend of bag, it can directly access any member of this class */ cout << "Width of bag : " << bag.width <<endl; } // Main function for the program int main() { bag bag; // set bag width without member function bag.setWidth(25.0); // Use friend function to print the wdith. printWidth( bag ); return 0; }
Output:
Friend Function Special grant for access to private and protected members can be given to a friend function like friend class. Could be a friend's function:
- A method of another class
- A global function
class Node { private: int key; Node* next; /* Other members of Node Class */ friend int LinkedList::search(); // Only search() of linkedList // can access internal members };
Following are some important points regarding the functions and classes of friends:
- Friends are only to be used for a specific time. Too many functions or external classes are clarified as friends of a class with protected or private data, decreasing the value of encapsulation in object-oriented programming of separate classes.
- Friendship is not mutual. When Class K is a P friend, then P won't become a P friend immediately.
- Friendship is not hereditary (see more details here)
- In Java, there isn't the concept of friends.
A simple and complete C++ program to demonstrate friend Class
#include <iostream> class B { private: int b; public: B() { b = 10; } friend class A; // Friend Class }; class A { private: int a; public: void showA(B& x) { // Since B is friend of A, it can access // private members of A std::cout << "B::b=" << x.b; } }; int main() { B b; A a; a.showA(b); return 0; }
Output:
A easy and complete C++ program to explain other class friendly function
#include <iostream> class T; class K { public: void showT(T&); }; class T { private: int t; public: T() { t = 05; } friend void K::showT(T& y); // Friend function }; void K::showT(T& y) { // Since showT() is friend of T, it can // access private members of T std::cout << "T::t = " << y.t; } int main() { K k; T y; k.showT(y); return 0; }
Output:
A simple and comprehensive C++ program to prove a global friend
#include <iostream> class K { int k; public: K() { k = 0; } // global friend function friend void showK(K&); }; void showK(K& r) { // Since showK() is a friend, it can access // private members of K std::cout << "K::k=" << r.k; } int main() { K k; showK(k); return 0; }
Output: