Curriculum
Course: Complete C++ Programming Course
Login

Curriculum

Complete C++ Programming Course

Text lesson

Introduction to Friend Function in C++

In this lesson, you will learn

  • Need for a Friend Function
  • Friend Function
  • Examples
  • Features of a Friend Function

 

Need for a Friend Function

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.

 

Example: Doctor-Patient Confidentiality

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.

 

What is the Friend Function

  • A friend function is a function that is not a member of a class but is granted special access to the private and protected members of that class.
  • Friend functions are declared using the friend keyword within the class definition, but defined like a normal function outside the class.

Syntax 

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
}

Explanation

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).

 

Example 1: Accessing Private Data using Friend Function

//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;
}

Output

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.

 

Example 2: Accessing Private Data

//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);
}

Output

Average marks: 73.3333

 

Important Point

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.

 


End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

5.0
5.0 out of 5 stars (based on 3 reviews)
Excellent
Very good
Average
Poor
Terrible

 

 

 

16 September 2025

nice explanation

16 September 2025

good

27 November 2024

nice explanation

Submit a Review