C++ Friend Function

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 of Student.

That’s why this works:

cout << student.marks;

even though marks is private.

Friend Function Is Not a Class Member

This is an important point.

A friend function is not actually part of the class.

For example:

class Student {
public:
    friend void showMarks(Student student);
};

The function is declared inside the class, but it is defined outside:

void showMarks(Student student) {
    // function code
}

So you don’t call it using the dot operator.

❌ Don’t write:

student.showMarks();

Instead, call it like a normal function:

showMarks(student);

Friend Function with Private Data

Let’s take another example.

#include <iostream>
using namespace std;

class Box {
private:
    int length;

public:
    Box(int l) {
        length = l;
    }

    friend void showLength(Box box);
};

void showLength(Box box) {
    cout << "Length: " << box.length;
}

int main() {
    Box box(10);

    showLength(box);

    return 0;
}

Output:

Length: 10

The length variable is private, but showLength() can access it because it is a friend function.

Friend Function with Two Objects

A friend function can also work with more than one object.

For example, let’s compare the ages of two students.

#include <iostream>
using namespace std;

class Student {
private:
    int age;

public:
    Student(int a) {
        age = a;
    }

    friend void compareAge(Student s1, Student s2);
};

void compareAge(Student s1, Student s2) {
    if (s1.age > s2.age) {
        cout << "First student is older.";
    } else if (s2.age > s1.age) {
        cout << "Second student is older.";
    } else {
        cout << "Both students are the same age.";
    }
}

int main() {
    Student student1(15);
    Student student2(14);

    compareAge(student1, student2);

    return 0;
}

Output:

First student is older.

Here, compareAge() can access the private age of both objects.

Friend Function vs Normal Function

A normal function cannot directly access private members of a class.

void showMarks(Student student) {
    cout << student.marks; // Error
}

But a friend function can:

friend void showMarks(Student student);

Now the function can access:

student.marks;

Important Things to Remember

  • A friend function is not a member function of the class.
  • Use the friend keyword to declare it.
  • A friend function can access private and protected members.
  • It is defined outside the class.
  • It is called like a normal function.
  • You don’t use the . operator to call a friend function.
  • Use friend functions only when they are actually useful; don’t make everything a friend.

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.

Scroll to Top