Friend function in C++

Q. What is friend function in C++? What are the risks associated with the use of friend function?


As we know that the private members of the class cannot be assessed from outside the class, a non member function cannot have access to the private data of that class. Though there could be a condition where we would like two classes to distribute a particular function.


For instance:

Consider a case where two classes' manager and employee have defined. We could like to use a function to operate on the both objects of both classes. In such situation C++ allows the common function to be made friendly with both the classes, thereby allowing the function to be access to the private data of these classes. Such members need not be members of these classes. A member function of one class can be friend function of another class in such a case. They are defining using the case scope resolution operator.

Syntax: Friend return _ type class name : : function name (args) we can also declare all the members functions of one classes as the friend function of another class, in such class the class is called a friend class.

Syntax: Friend class name: A friend function has certain special characteristics:

1.      It is not in scope of the class, it cannot be called by the object of that class.

2.      As it is not in the scope of the class, it cannot be called with the object of that class.

3.      It can be invoked as a normal function without the help of some object.

4.      Unlike member functions, it cannot be access the member names directly and has to be used an object name and dot membership operator with each member name.

5.      It can be stated either in the public or private part of a class without disturbing its sense.

6.      Usually it has the objects as the arguments.


Class X, Y, Z;

Int n;

Public :

 Void value (Int s)


 Int x;

X = s;


Friend void max (X, Y, Z; ABC);


Class ABC


Int a;

Public :

Void value (Int s)


a = s;


Friend void max (XYZ, ABC);


Void max (XYZ m, ABC n)


If (m. X. > = n. a)

 Cout << m. x.;


cout << n. a.;


Int main ();


A, B, C a, b, c;

A, b, c value (1);

X, Y, Z x, y, z;

 X, y, z; value (20);


Max (X, Y, Z; a, b, c);

Return 0;

   Related Questions in Programming Languages

  • Q : Define Factoring Problem Factoring

    Factoring Problem: Factoring is the action of dividing an integer into a set of smaller integers (or factors) which, when multiplied altogether, form the unique integer. For illustration, the factors of 15 are 3 and 5; the factoring trouble is to find

  • Q : What is session What is meant by the

    What is meant by the session?

  • Q : Explain Look-and-feel Look-and-feel :

    Look-and-feel: The visual impression and interaction style given by a user interface. This is mainly the responsibility of the window manager (that is, in collaboration with the fundamental operating system) running on a specific computer. This refers

  • Q : Directory and filename Explain

    Explain directory and filename?

  • Q : Function of windows-dot-h header file

    What is the function of <windows.h> header file ?

  • Q : State the term URN State the term URN?

    State the term URN?

  • Q : Write a program using simple loop

    Objective:  The purpose of this problem is to gain experience with the principles necessary to write a program using simple loop, decision processing, counters and accumulators Save the Barns, a bi-partisan po

  • Q : What is Behavior Behavior : It is the

    Behavior: It is the methods of a class which implement its behavior. A particular object's behavior is a mixture of the method definitions of its class and the present state of the object.

  • Q : Explain Virtual memory Virtual memory :

    Virtual memory: It is computer will comprise a limited amount of real memory accessible to it. Programs frequently need more memory than the quantity of real memory. Moreover, in a multiprogramming system, various processes will be competing for simil

  • Q : What is an Attribute Attribute : It is

    Attribute: It is a particular usage of an instance variable. The set of attribute values held in a specific instance of a class state the current state of that instance. A class definition might impose specific constraints on the valid states of its i

©TutorsGlobe All rights reserved 2022-2023.