Dremendo Tag Line

Friend Function in C++ Programming

OOPs in C++

In this lesson, we will understand what is Friend Function in C++ Programming and how to create them along with some examples.

What is Friend Function in C++?

The friend function is a function that is defined outside the class but can access the instance variables of the class. It is declared inside the class with the keyword friend. Though it is declared inside the class, it is not considered as the instance method of the class.

It can be declared either in the private or public section of the class.

video-poster

Example

#include <iostream>
#include <string.h>

using namespace std;

class student
{
  private:
    int roll;
    char name[30];

  public:
    // Constructor
    student()
    {
        roll=1;
        strcpy(name,"Martin");
    }

    void display()
    {
        cout<<"Roll: "<<roll<<endl;
        cout<<"Name: "<<name<<endl;
    }

    // friend function declaration
    friend void updatedata(student &y, int r, char n[]);
};

// defining the friend function
void updatedata(student &y, int r, char n[])
{
    y.roll=r;
    strcpy(y.name,n);
}

int main()
{
    student x;
    x.display();
    updatedata(x, 12, "Perter");
    x.display();
    return 0;
}

Output

Roll: 1
Name: Martin
Roll: 12
Name: Perter

In the above example, we have created a class and declared a friend function inside the class with an object reference of the class student, an integer variable, and a character array as its formal arguments.

Further, we have defined the friend function outside the class, updating the data of the object's instance variables with the data received in the formal arguments of the friend function.

In the main function, we created an object x of the class student and displayed its values on the screen. After that, we update its data with the new one by using the friend function and again display its new value on the screen.

Let's see another example where we will create two different classes and will use the friend function to add their object's value.

Example

#include <iostream>
#include <string.h>

using namespace std;

class data2;

class data1
{
  private:
    int x;
    float y;

  public:
    // Constructor
    data1()
    {
        x=10;
        y=20.5;
    }

    void display()
    {
        cout<<"x="<<x<<endl;
        cout<<"y="<<y<<endl;
    }

    // friend function
    friend void sum(data1 &ob1, data2 &ob2);
};

class data2
{
  private:
    int a;
    float b;

  public:
    // Constructor
    data2()
    {
        a=30;
        b=50.62;
    }

    void display()
    {
        cout<<"a="<<a<<endl;
        cout<<"b="<<b<<endl;
    }

    // friend function
    friend void sum(data1 &ob1, data2 &ob2);
};

void sum(data1 &obj1, data2 &obj2)
{
    int i;
    float j;

    i=obj1.x+obj2.a;
    j=obj1.y+obj2.b;
    cout<<"x+a="<<i<<endl;
    cout<<"y+b="<<j<<endl;
}

int main()
{
    data1 x;
    data2 y;
    x.display();
    y.display();
    sum(x,y);
    return 0;
}

Output

x=10
y=20.5
a=30
b=50.62
x+a=40
y+b=71.12

On line number 6, we have declared the class data2 because, in class data1, we have created a friend function using class data2 as a formal argument. If we don't declare the class data2 above class data1, then the friend function inside the class data1 can not find the class data2 and will give a compilation error.