In this lesson, you will learn
We know that the private members can not be accessed outside the class, i.e., a non-member function can not have access to the private data of a class.
However, there is a situation where you would like to provide access to unrestricted(private) data of a class to an outside member (non-member).
This can be achieved using friend functions or friend classes.
Imagine two classes representing a Doctor and a Patient. The Patient class contains sensitive medical information as private data members, which should not be accessible directly. However, the Doctor class needs access to this information to provide proper medical care.
Therefore, we would like to use the ExaminePatient() function to operate on classes. In such a scenario, C++ allows the common function to be made friendly with both classes and allows the function to access their private data.
friend keyword within the class definition, but defined like a normal function outside the class.
class ClassName {
private:
// Private members
public:
// Public members
friend return_type functionName(parameters); // Declaration of friend function
};
// Definition of the friend function (outside the class)
return_type functionName(parameters) {
// Function body with access to ClassName's private/protected members
}
The function is declared inside the class with the keyword friend. It is defined outside the class like a normal function (no ClassName:: scope resolution).
//FriendFuncEx1.cpp
#include <iostream>
using namespace std;
class Sample {
private:
int data;
public:
Sample(int d) : data(d) {}
friend void DisplayData(Sample& obj);//friend function declaration
};
void DisplayData(Sample& obj) {
cout<< "Private Data: " <<obj.data <<endl;
}
int main() {
Sample s(42);
DisplayData(s); // Access privateData using a friend function
return 0;
}
Private Data: 42
In this example, the DisplayData function is a friend of the Sample class, allowing it to access and display the private data member data.
Note: Like member functions, a friend function cannot access members directly and must use an object name and the dot operator to access each member. So, usually it takes an object as an argument.
//FriendFuncEx2.cpp
#include <iostream>
using namespace std;
class Student {
private:
int marks1,marks2,marks3;
public:
void SetMarks(){
marks1=75;marks2=80;marks3=65;
}
//friend function declaration
friend float average(Student alice);
};
float average(Student alice){
return float(alice.marks1+alice.marks2+alice.marks3)/3.0;
}
int main(){
Student s;
s.SetMarks();
cout<<"Average marks: "<<average(s);
}
Average marks: 73.3333
1. A member function of one class can be a friend of another class. Here, int AccessData() is a member of a class B and a friend of class A. Now, it can access all the private data of class A.
class B {
---------
public:
int AccessData(); // Member function of B
---------
};
class A {
private:
int data;
---------
public:
friend int B :: AccessData(); //AccessData() of Class B
--------- //is friend of Class A
//Class B can access private data of Class A
};
2. We can also declare a complete class as a friend class if you want to declare all member functions of a class as friend functions.
class ClassA
{
-----------
public:
friend class ClassB; // ClassB is a friend of ClassA
-----------
}
Note: Here, ClassB is a friend of ClassA. Since Class B becomes the friend class, it will have access to all the public, private, and protected members of Class A
However, the opposite will not be true. Hence, ClassA will not have access to private members of ClassB.
nice explanation
good
nice explanation
You must be logged in to submit a review.