Sometimes a class keeps its data private, but we still want a particular function outside the class to access that private data. C++ gives us a way to do this using a friend function. A friend function is a function that is not a member of the class, but the class gives it permission to access its private and protected members.
It may sound a little strange at first, so let’s understand C++ Friend Function with an example.
Why Do We Need a Friend Function in C++?
Normally, a private variable cannot be accessed from outside the class.
For example:
class Student {
private:
int marks;
};
We cannot do this:
student.marks = 90;
But sometimes we may want a particular outside function to access marks. That’s where a friend function can help.
How to Create a Friend Function in C++?
We use the friend keyword inside the class.
class Student {
private:
int marks;
public:
friend void showMarks(Student student);
};
The function is declared inside the class using friend, but the function itself is written outside the class.
Simple C++ Friend Function Example
#include <iostream>
using namespace std;
class Student {
private:
int marks = 90;
public:
friend void showMarks(Student student);
};
void showMarks(Student student) {
cout << "Marks: " << student.marks;
}
int main() {
Student student;
showMarks(student);
return 0;
}
Output:
Marks: 90
Look at this line:
friend void showMarks(Student student);
It tells C++:
showMarks()is allowed to access the private members ofStudent.
That’s why this works:
cout << student.marks;
even though marks is private.
C++ Friend Class
A friend class is a class that is allowed to access the private and protected members of another class. We use the friend keyword to give another class this permission.
Simple C++ Friend Class Example
#include <iostream>
using namespace std;
class Student {
private:
int marks = 90;
friend class Teacher;
};
class Teacher {
public:
void showMarks(Student student) {
cout << "Marks: " << student.marks;
}
};
int main() {
Student student;
Teacher teacher;
teacher.showMarks(student);
return 0;
}
Output:
Marks: 90
Look at this line:
friend class Teacher;
It tells C++ that the Teacher class is allowed to access the private and protected members of Student.
That’s why Teacher can access:
student.marks
even though marks is private.
Friend Function vs Friend Class
The difference is simple:
- Friend function → gives access to one specific function.
- Friend class → gives access to all member functions of another class.
Mini Quiz
Mini Project
Mini Project: Student Result Checker
Question:
Write a C++ program that uses a friend function to check the result of a student.
- Create a
Studentclass with a privatemarksvariable. - Ask the user to enter the student’s marks.
- Create a constructor to store the marks.
- Declare a friend function named
checkResult(). - Use the friend function to access the private
marks. - If marks are
40or more, display"Student Passed". - Otherwise, display
"Student Failed". - Display the marks and result on the screen.
View Answer Code
#include <iostream>
using namespace std;
class Student {
private:
int marks;
public:
Student(int m) {
marks = m;
}
friend void checkResult(Student student);
};
void checkResult(Student student) {
cout << "Marks: " << student.marks << "\n";
if (student.marks >= 40) {
cout << "Student Passed";
}
else {
cout << "Student Failed";
}
}
int main() {
int marks;
cout << "Enter marks: ";
cin >> marks;
Student student(marks);
checkResult(student);
return 0;
}
What’s Next?
In the next chapter, we will learn about C++ Encapsulation and see how data can be protected inside a class while allowing controlled access through public functions.
Frequently Asked Questions (FAQs)
Q1. What is a friend function in C++?
A friend function is a non-member function that is given permission to access the private and protected members of a class.
Q2. How does the C++ friend keyword work?
The friend keyword is used inside a class to give a specific outside function access to its private and protected members.
Q3. Can a C++ friend function access private members?
Yes. A friend function can directly access the private members of the class that declares it as a friend.
Q4. Is a friend function a member function in C++?
No. A friend function is not a member of the class. It is defined outside the class and called like a normal function.
Q5. How do you call a friend function in C++?
A friend function is called like a regular function, such as showMarks(student). You do not use the dot (.) operator.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
