Friend function in C++

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

Ans.

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.

Example

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.;

else

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 : What is Pattern Pattern : It is a

    Pattern: It is a recurring theme in class usage or design. Interfaces like Iterator encapsulate a pattern of admission to the items in a collection, whereas freeing the client from the requirement to know details of the way in which the collection is

  • Q : Define White space White space :

    White space: Characters employed to make visual spacing in a program. White spaces comprise space, tab, carriage return and the line feed characters.

  • Q : Define Inner class Inner class : A

    Inner class: A class defined within an enclosing method or class. We use the word to refer to non-static nested classes.

  • Q : State Cout and Cin Cout: This is an

    Cout: This is an object of ostream_withassign class stated in iostream.h header file

  • Q : Reading Algorithms Assignment 1:

    Assignment 1: Algorithms Rules: See the Assignment Rules file on the class Moodle site. 1 Reading Algorithms (20 points) Input: A nonempty string of characters S1S2 . . . Sn, and a positive integer n giving the number of characters in the string. Output: See t

  • Q : Define the reasons of Process Handle

    Define the reasons of Process Handle Table.

  • Q : Define Property Specifications Property

    Property Specifications: Users can specify assertions using the assert(expr) statements. An assert statement is used to check if the property specified by the expression expr is valid within a state. If expr evaluates to 0, this implies that it is not

  • Q : Define Inconsistent state Inconsistent

    Inconsistent state: A state which an object must not be in. A class requires to be carefully designed in order to make sure that none of its examples can get into a conflicting state. An illustration of an inconsistent state may be a football team wit

  • Q : State the term XHTML State the term

    State the term XHTML?

  • Q : Virtual memory used in Windows

    Explain why the virtual memory is to be used in Windows architecture?

©TutorsGlobe All rights reserved 2022-2023.