A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other class. For example a LinkedList class may be allowed to access private members of Node.
class Node
{
private:
int key;
Node* next;
/* Other members of Node Class */
/*Now class LinkedList can access private members of Node */
friend class LinkedList;
};
Friend - Function:-
Like friend class, a friend function can be given special grant to access private and protected members. A friend function can be:
a) A method of another class
b) A global function
* Full Description About Friend - Function:-
In C++, A non- member function can not have an access to the private data of a class. In such situations , C++ allows to declare that function as a Friend Function so that it can access the private data of a class.
To make an outside function friendly to a class, we have to simply declare that function as a Friend Function as shown below-
class ABC
{
------
public:
-------
friend void xyz(); // Declaration
};
* A Friend Function possesses certain special characterstics:-
1. it is not in the scope of the class to which it has been declared as Friend.
2. since , it is not in the scope of a class , it can not be called using the object of the class.
3. it can be invoked like normal function without the help of any object.
4. Unlike member functions , it can not access the member names directly and has to use an object name and dot membership operator with each member name.
5. it can be declared either in the public or the private part of a class without affecting its meaning.
6. Usually, it has the objects as arguments.
// C++ program to demonstrate usage of friend function.
#include<iostream>
using namespace std;
class sample
{
int a,b;
public:
void setvalue()
{
a=25;b=40;
}
friend float mean(sample s);
};
float mean(sample s)
{
return (s.a+s.b)/2.0;
}
int main()
{
sample x;
x.setvalue();
cout<<"mean value="<<mean(x);
return 0;
}
input/output:-
mean value=32.5
Following are some important points about friend functions and classes:
1) Friends should be used only for limited purpose. too many functions or external classes are declared as friends of a class with protected or private data, it lessens the value of encapsulation of separate classes in object-oriented programming.
2) Friendship is not mutual. If class A is a friend of B, then B doesn’t become a friend of A automatically.
3) Friendship is not inherited.
4) The concept of friends is not there in Java.3) Friendship is not inherited.
Can we access private data members of a class without using a member or a friend function?
The idea of Encapsulation is to bundle data and methods (that work on the data) together and restrict access of private data members outside the class. In C++, a friend function or friend class can also access private data members.
Is it possible to access private members outside a class without friend?
Yes, it is possible using pointers. See the following program as an example.
Yes, it is possible using pointers. See the following program as an example.
include<iostream>
using namespace std;
class Test
{
private:
int data;
public:
Test() { data = 0; }
int getData() { return data; }
};
int main()
{
Test t;
int* ptr = (int*)&t;
*ptr = 10;
cout << t.getData();
return 0;
}
input/output:-
10
// C++ Program to swap two numbers using friend function
#include <iostream>
using namespace std;
class Swap {
// Declare the variables of Swap Class
int temp, a, b;
public:
// Define the parametrized constructor, for inputs
Swap(int a, int b)
{
this->a = a;
this->b = b;
}
// Declare the friend function to swap, take arguments
// as call by reference
friend void swap(Swap&);
};
// Define the swap function outside class scope
void swap(Swap& s1)
{
// Call by reference is used to passed object copy to
// the function
cout << "\nBefore Swapping: " << s1.a << " " << s1.b;
// Swap operations with Swap Class variables
s1.temp = s1.a;
s1.a = s1.b;
s1.b = s1.temp;
cout << "\nAfter Swapping: " << s1.a << " " << s1.b;
}
// Driver Code
int main()
{
// Declare and Initialize the Swap object
Swap s(4, 6);
swap(s);
return 0;
}
input/output:-
Before Swapping: 4 6
After Swapping: 6 4