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 : Explain Polymorphism Polymorphism : It

    Polymorphism: It is the ability of an object reference to be employed as if it referred to an object with various forms. The polymorphism in Java outcomes from both class inheritance and interface inheritance. The actually different forms frequently o

  • Q : What is Dotted decimal notation Dotted

    Dotted decimal notation: The notation employed to symbolize the 4-byte values of an IP address. Each and every byte is symbolized as a value between 0 to 255, for instance 129.12.0.1. The most noteworthy byte is written at first.

  • Q : What is Reflection Reflection : It is

    Reflection: It is the ability to find out what fields, methods, constructors, and so forth, are stated for an object or class. Reflection is supported by the Class ‘class’, and other classes in the java.lang.reflect package. Reflection mak

  • Q : Components of ASP.NET AJAX architecture

    Name the components of ASP.NET AJAX architecture?

  • Q : Differentiate overriding and

    Differentiate overriding and overloading method?

  • Q : What is an Arithmetic expression

    Arithmetic expression: It is an expression comprising numerical values of integer or floating point kinds. For example, operators like +, -, *, / and % get arithmetic expressions as their operands and generate arithmetic values as their outcomes.

  • Q : Program to controls a quiz show in C++

    Foundations of Computer Science In this project, we will write a program that controls a quiz show, much like the many popular TV shows. The program will read in a group of questions and thei

  • Q : Scripting Language Programming

    Hi Assignment Team, Please find attachment of my assignment. Please advise if there is any unclear information Regards, King

  • Q : Define the term Toggle Define the term

    Define the term Toggle: To alternate among two values, like true and false, on and off, or 1 & 0.

  • Q : When does a name clash take place in

    When does a name clash take place in programming?

©TutorsGlobe All rights reserved 2022-2023.