C++ Friend Function

The C++ programming language offers various features and functionalities to the programmers. It also supports object-oriented programming which is very useful.

In C++, you have a very cool function which is Friend. The main purpose of the friend function is to help you to access the private and protected members of another class in which it is declared. You can use the keyword ‘friend’ to declare a friend function.

Types of Friend Function in C++

Friend Function is mainly of 2 types:-

1. A global function:- It will allow you to access all the private and protected members of the class.

2. Method of another class:- If you have more than one class and you want to access the non-public data members of a particular class then you can use Friend function.

Syntax of C++ Friend Function:-

Declaration of Friend Function

class name_of_the_class
{

Access specifiers // private or protected
.
.
friend return_type FunctionName(argument(s));
.
.

}

Definition of Friend Function in C++

class name_of_the_class
{

Access specifiers // private or protected
friend return_type FunctionName(argument(s));
}

return_type FunctionName(parameter(s))
{
// will help you to access the private and protected data members of the class!
}

Implementation of Friend Functions in C++

In two ways, you can implement a friend function in C++. One is implemented through a method of another class and the second is implemented through global friends.

Implementing through a method of another class

#include <iostream>
using namespace std;

class Tech
{
private:
int value;
public:
Tech()
{
value = 15;
}
friend class Exp;
};

class Exp
{
private:
int value1;
public:
void print(Tech& x)
{
cout<<"Base value using the friend class: " << x.value<<endl;
}
};

int main()
{

cout<<"TechVidvan Tutorial: C++ Friend Function!"<<endl<<endl;

Tech value;
Exp value1;
value1.print(value);
return 0;
}

Output:-

TechVidvan Tutorial: C++ Friend Function!

Base value using the friend class: 15

Implementing Friend Functions through Global Friends

#include <iostream>
using namespace std;

class Tech
{
string data;
public:
friend void print( Tech value );
void input( string s1 );
};

void Tech::input( string s1 )
{
data = s1;
}

void print( Tech value )
{
cout<<"A message to you: "<<value.data<<endl;
}

int main()
{

cout<<"TechVidvan Tutorial: C++ Friend Function!"<<endl<<endl;

Tech value;

value.input("Good Day");
print( value );
return 0;
}

Output:-

TechVidvan Tutorial: C++ Friend Function!

A message to you: Good Day

Characteristics of Friend Functions in C++

  • You cannot invoke a friend function like a normal function without using the object.
  • You can declare a friend function either in public or in private.
  • With the friend function, you cannot access the member names directly. To access it, you need to use an object name and dot membership operator.
  • It cannot be called using the object. Because it is not in the scope of that class.

Why to use friend function in C++

It is used in special cases like when you want to access the class’s private data directly without using objects of that class. It is also used in operator overloading.

Summary

This was all about the Friend function in C++. We talked over the implementation of the friend function in C++. Then we discussed the characteristics of the friend function. We also told you why you should use the friend function in C++.