C++ Friend Function: The Complete Guide

0
315
C++ Friend Function Example | Friend Function In C++

A friend function of the class is defined outside that class’ scope, but it has a right to access all the private and protected members of a class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.

What is the C++ Friend Function?

C++ Friend Function is used to access the private and protected members of the class. Private and Protected data members of the class can only be accessed through member functions of that particular class, but through the friend function, we can access those data members. The friend function declaration is inside the class of whose private and protected data members we want to access.

How to Declare Friend Function in C++

To declare a friend function in C++, prepend the friend keyword.

See the following syntax.

class class_name
{
	friend  datatype function_name(parameters) ;
};

See the following example.

class A
{
	friend  void  fun1(A);
};

Friend function properties

  1. It must be defined outside of class to which it is a friend.
  2. It has no caller object as it is not a member function and should not be defined with the membership label or the friend keyword.
  3. It is not in the scope of the class it has been declared in, so it cannot directly access the private or protected data members. Instead, we have to pass the object of that class as an argument of the friend function and access data members through the object and the dot membership operator with the member name.
  4. It calls to be called/invoked without any object like a normal function.
  5. A friend function is most preferably declared in the private or public part of the class.

A friend function is declared with a friend keyword.

Programming Example

See the following example program for creating a friend function and accessing the private values of the class and display as a result.

#include <iostream>
using namespace std;
class A
{
private:
  int a, b;

public:
  void getdata(int x, int y)
  {
    a = x;
    b = y;
  }
  friend void fun1(A);
};
void fun1(A obj)
{
  cout << "The value of 1st private member is-" << obj.a << endl;
  cout << "The value of 2nd private member is-" << obj.b << endl;
}
int main()
{
  A obj1;
  obj1.getdata(4, 5);
  fun1(obj1);
  return (0);
}

See the following output.

C++ Friend Function

Now, see the example program adds two private data members of the class 

through the friend function and shows the sum as the output.

#include <iostream>
using namespace std;
class A
{
private:
  int a, b;

public:
  void getdata(int x, int y)
  {
    a = x;
    b = y;
  }
  friend void add(A);
};
void add(A obj)
{
  cout << "The first private number:-" << obj.a << endl;
  cout << "The second private number:-" << obj.b << endl;
  cout << "Sum-" << obj.a + obj.b << endl;
}
int main()
{
  int q, r;
  cout << "Enter two values to make it a private member of the class:-";
  cin >> q >> r;
  A obj1;
  obj1.getdata(q, r);
  add(obj1);
  return (0);
}

See the following program.

Friend Function In C++

In both examples, we have used the object of the class as the parameter, which is the most important thing, while defining a friend function as the friend function is not a member function of the class, so it cannot directly access the private members of the class.

So to access the private member of the class, we use the object name with a dot membership label and the private data member example( obj_name.private_data_member). 

Friend function should be used for a limited purpose only as it can lead to our valuable data being misleading as it is accessing our private data. It also hampers data encapsulation.

C++ Friend Class

A friend class can access all the private and protected data of members of the class to which it is declared a friend.

If we want to access the private and protected data of members of the class in the friend class, we must pass the objects of the class to the member functions of the friend class.

Friendship doesn’t have an inheritance property.

Declaration of Friend Class

class class_name
{
	private:
		data_type  variable_1,variable_2;
	public:
	friend class class_name;
};

Friend class is beneficial if we want to access the private and protected part of the other class. For instance, if we compare two private data members of two different classes, then a friend class can be beneficial.

Friend function and class are both considered a loophole if we compare them with object-oriented programming concepts. Still, it is beneficial if we want to apply some logical concept that requires access to private data members, but it should be used for a limited purpose only.

Example programs for Friend Class

See the following program compares the value of private data members of one class to the private data members of the other class and shows the output if they are equal or not with the help of the friend class.

#include <iostream>
using namespace std;
class A
{
private:
  int a, b;

public:
  void setdata(int x, int y)
  {
    a = x;
    b = y;
  }

  friend class B;
};
class B
{
private:
  int k, t;

public:
  void fixdata(int q, int r)
  {
    k = q;
    t = r;
  }
  void compare(A obj)
  {
    if (obj.a == k && obj.b == t)
    {
      cout << "Both the private members of the class are equal" << endl;
    }
    else
    {
      cout << "Both the private members of the class are not equal" << endl;
    }
  }
};
int main()
{
  int y, u, z, i;
  A obj1;
  B obj2;
  cout << "Enter two values which will be private of class A:";
  cin >> y >> u;
  cout << "Enter two values which will be private of class B:";
  cin >> z >> i;
  obj1.setdata(y, u);
  obj2.fixdata(z, i);
  obj2.compare(obj1);
  return (0);
}

See the following output.

Example programs for Friend Class

See the following program contains two classes one is Data which includes the length and breadth of a rectangle as its private member, and the other class is Rectangle which is used to calculate the area and perimeter of the Rectangle using the private data members of the class Data using friend class.

#include <iostream>
using namespace std;
class Data
{
private:
  int length = 5, breadth = 6;

public:
  friend class Rectangle;
};
class Rectangle
{
public:
  void area(Data d1)
  {
    cout << "Area of the rectange is=" << d1.length * d1.breadth << endl;
  }
  void perimeter(Data d2)
  {
    cout << "Area of the rectange is=" << 2 * (d2.length + d2.breadth) << endl;
  }
};
int main()
{
  Data obj1;
  Rectangle obj2;
  cout << "Length=6" << endl;
  cout << "Breadth=5" << endl;
  obj2.area(obj1);
  obj2.perimeter(obj1);
  return (0);
}

See the following output.

Declaration of Friend Class

In both examples, we must explicitly pass the object of the class we declared friend to class. It can be taken like we are calling a function with the object of that particular class, and in the function parameter, we are passing the object of the other class from where we have to fetch the private data members.

That’s it for this tutorial.

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.