Friend Function / Class in C++
In principle, private and protected members of a class cannot be accessed from outside of the same class in which they are declared.However, this rule does not apply to "friends".
Friend Functions :
-
A friend function is a function that is not a member of a class but has access to the class's private and protected members.
-
Friend functions are not considered class members; they are normal external functions that are given special access privileges.
-
Friends are not in the class's scope, and they are not called using the member-selection operators (. and –>) unless they are members of another class.
-
A friend function is declared by the class that is granting access.
-
The friend declaration can be placed anywhere in the class declaration. It is not affected by the access control keywords.
-
To declare a function as friend of a class, we use friend keyword.
Code Example:
#include<iostream> using namespace std; class Test { //declaration of friend function friend void display(Test & ); public: void show() { cout<<"a = "<< a<< endl; } private: int a; }; //definition of friend function void display (Test &ob) { /*display is friend of Test so,can access private 'a' */ ob.a = 20; ob.show(); } int main() { Test ob; /*ERROR :main is not friend of Test, can't access private data memeber 'a' */ ob.a = 30; return 0; }
Class members as friends :
Class member functions can be declared as friends in other classes. Consider the following example:#include<iostream> using namespace std; class Second; class First { public: int show( Second& b ); private: int display( Second& b ); }; class Second { private: int a; // A::show is a friend function to class B // so A::Func1 has access to all members of B friend int First::show( Second& ); }; int First::show( Second& b ) { return b.a; // OK } int First::display( Second& b ) { return b.a; //ERROR }
In the preceding example, only the function First::show( Second& ) is granted friend access to class Second. Therefore, access to the private member 'a' is correct in show of class First but not in display.
Friend Class :
A friend class is a class all of whose member functions are friend functions of a class, that is, whose member functions have access to the other class's private and protected members. Suppose the friend declaration in class Second had been:
friend class First;In that case, all member functions in class First would have been granted friend access to class Second.
#include<iostream> using namespace std; class Test { // Declare a friend class friend class NewClass; public: Test() { a = 0; } void show() { cout << a << endl; } private: int a; }; class NewClass { public: void change( Test& ob, int x ) { ob.a = x; } }; int main() { Test ob; NewClass ob1; ob.show(); ob1.change( ob, 5 ); ob.show(); return 0; }
Friendship is not mutual unless explicitly specified as such. In the above example, member functions of Test cannot access the private members of NewClass.
Next topic is static in C++
Training For College Campus
We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies
like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.
Please mail your requirement at info@prowessapps.in
Projects For Students
Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.
Students can mail requirement at info@prowessapps.in