AppDividend
Latest Code Tutorials

C++ Friend Function Example | Friend Function In C++

0

C++ Friend Function Example | Friend Function In C++ is today’s topic. A 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 declaration of the friend function is inside the class of whose private and protected data members we want to access.

C++ Friend Function

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.

#How To Declare Friend Function

See the following syntax.

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

See the following example.

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

#Properties

  1. It must be defined outside to class to which it is a friend.
  2. It has no caller object as it is not a member function and it should not be defined with the membership label or with the friend keyword.
  3. It is not in a scope of the class it has been declared in so it cannot directly access the private or protected data members. 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 private or public part of the class.

A friend function is declared with a friend keyword.

#Programming Example

See the following example program is for creating a friend function and access 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 the examples we have used 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 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 members of the class to which it is declared as a friend.

If we want to access the private and protected data members of the class in friend class, we must pass 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’re going to compare two different private data members of two different class, then friend class can be very useful.

Friend function and class both are considered as a loophole if we compare it with the concepts of object-oriented programming, but it is beneficial if we want to apply some logical concept that requires access of 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 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 class one is Data which contains the length and breadth of a rectangle as their private member and the other class which 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 have to explicitly pass the object of the class in which we have declared friend 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.

Finally, C++ Friend Function Example | Friend Function In C++ is over.

Leave A Reply

Your email address will not be published.

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