Friend Functions
A friend function is a non-member function which can access the private and protected members of a class. To declare an external function as a friend of the class, the function prototype preceded withfriend keyword should be included in that class. Syntax for creating a friend function is as follows:
1 2 3 4 5 6 7 8 | class ClassName { ... friend return–type function–name(params–list); ... }; |
Following are the properties of a friend function:
- Friend function is a normal external function which is given special access to the private and protected members of a class.
- Friend functions cannot be called using the dot operator or -> operator.
- Friend function cannot be considered as a member function.
- A function can be declared as friend in any number of classes.
- As friend functions are non-member functions, we cannot use this pointer with them.
- The keyword friend is used only in the declaration. Not in the definition.
- Friend function can access the members of a class using an object of that class.
Following program demonstrates using a friend function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include<iostream> using namespace std; class Student { private: string name; string regdno; int age; string branch; public: void set_name(string); friend void show_details(Student); }; void Student::set_name(string n) { name = n; } void show_details(Student s) { cout << "Name of the student is: " << s.name; } int main() { Student s1; s1.set_name("Mahesh"); show_details(s1); return 0; } |
Output of the above program is as follows:
1 2 3 | Name of the student is: Mahesh |
In the above program show_details() function was able to access the private data member name as it is a friend of Student class.
Friend Class
A friend class is a class which can access the private and protected members of another class. If a class B has to be declared as a friend of class A, it is done as follows:
1 2 3 4 5 6 7 8 | class A { friend class B; ... ... }; |
Following are properties of a friend class:
- A class can be friend of any number of classes.
- When a class A becomes the friend of class B, class A can access all the members of class B. But class B can access only the public members of class A.
- Friendship is not transitive. That means a friend of friend is not a friend unless specified explicitly.
Following program demonstrates a friend class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include<iostream> using namespace std; class A { friend class B; private: int x; public: void set_x(int n) { x = n; } }; class B { private: int y; public: void set_y(int n) { y = n; } void show(A obj) { cout << "x = " << obj.x << endl; cout << "y = " << y; } }; int main() { A a; a.set_x(10); B b; b.set_y(20); b.show(a); return 0; } |
Output for the above program is as follows:
1 2 3 4 | x = 10 y = 20 |
In the above program class B has been declared as a friend of class A. That is why it is able to access private member x of class A.