Home » Friend Function in C++

Friend Function in C++

by Online Tutorials Library

Friend Function in C++

As we already know that in an object-oriented programming language, the members of the class can only access the data and functions of the class but outside functions cannot access the data of the class. There might be situation that the outside functions need to access the private members of the class so in that case, friend function comes into the picture.

What is a friend function?

A friend function is a function of the class defined outside the class scope but it has the right to access all the private and protected members of the class.

The friend functions appear in the class definition but friends are the member functions.

Characteristics of a Friend function:

  • The function is not in the scope of the class to which it has been declared as a friend.
  • It cannot be called using the object as it is not in the scope of that class.
  • It can be invoked like a normal function without using the object.
  • It cannot access the member names directly and has to use an object name and dot membership operator with the member name.
  • It can be declared either in the private or the public part.

Why do we need a friend function in C++?

  • Friend function in C++ is used when the class private data needs to be accessed directly without using object of that class.
  • Friend functions are also used to perform operator overloading. As we already know about the function overloading, operators can also be overloaded with the help of operator overloading.

Characteristics of a Friend function

  • The friend function is declared using friend keyword.
  • It is not a member of the class but it is a friend of the class.
  • As it is not a member of the class so it can be defined like a normal function.
  • Friend functions do not access the class data members directly but they pass an object as an argument.
  • It is like a normal function.
  • If we want to share the multiple class’s data in a function then we can use the friend function.

Syntax for the declaration of a friend function.

In the above declaration, the friend function is preceded by the keyword friend. The function can be defined anywhere in the program like a normal C++ function. The function definition does not use either the keyword friend or scope resolution operator.

Let’s understand the friend function through an example.

In the above code, Distance is the class that contains private field named as ‘meters‘. The Distance() is the constructor method that initializes the ‘meters’ value with 0. The display_data() is a method that displays the ‘meters’ value. The addvalue() is a friend function of Distance class that modifies the value of ‘meters’. Inside the main() method, d1 is an object of a Distance class.

Output

Friend Function in C++

Friend function can also be useful when we are working on objects of two different classes.

Let’s understand through an example.

In the above code, we have defined two classes named as ClassA and ClassB. Both these classes contain the friend function ‘multiply()‘. The friend function can access the data members of both the classes, i.e., ClassA and ClassB. The multiply() function accesses the num1 and num2 of ClassA and ClassB respectively. In the above code, we have created object1 and object2 of ClassA and ClassB respectively. The multiply() function multiplies the num1 and num2 and returns the result.

As we can observe in the above code that the friend function in ClassA is also using ClassB without prior declaration of ClassB. So, in this case, we need to provide the forward declaration of ClassB.

Output

Friend Function in C++

Friend class in C++

We can also create a friend class with the help of friend keyword.

In the above declaration, Class1 is declared as a friend class of Class2. All the members of Class2 can be accessed in Class1.

Let’s understand through an example.

In the above code, we have created two classes named as ClassA and ClassB. Since ClassA is declared as friend of ClassB, so ClassA can access all the data members of ClassB. In ClassB, we have defined a function add() that returns the sum of num1 and num2. Since ClassB is declared as friend of ClassA, so we can create the objects of ClassA in ClassB.

Output

Friend Function in C++


Next TopicSnake Code in C++

You may also like