--%>

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 Downcast with example Explain

    Explain Downcast with example: It is a cast towards an object's dynamic kind - that is, `down' the inheritance hierarchy. For illustration:        // Downcast from Object to String

  • Q : What is Bounded repetition Bounded

    Bounded repetition: The Repetition where statements within a loop's body are executed a fixed number of times and the number of times is established whenever the loop began. There is no control structure in Java which guarantees the bounded repetition

  • Q : Write the benefits of DLR Write the

    Write the benefits of DLR?

  • Q : What is an Overriding for chaining

    Overriding for chaining: It is a form of method overriding in which the sub-class version of a method verifies to see whether it can react to the message on its own and just calls the super-class version of the method.

  • Q : What is Binary operator Binary operator

    Binary operator: It is an operator taking two operands. Java has numerous binary operators, like the arithmetic operators +, -, *, / and %, and the Boolean operators &&, || and ^, among others.

  • Q : Describe File system File system : The

    File system: The operating system makes it possible to utilize space on a computer's disk drives by imposing a structured file system on disk storage. Each and every file system contains its own conventions for the manner in which the files are named,

  • Q : Self-created What is the difference

    What is the difference among a self-created certificate, Symbian Signed and a Symbian developer certificate?

  • Q : Define Aliases Aliases : It is a

    Aliases: It is a multiple references to a single object. Messages might be sent to the object through any of its aliases. The resultant state modifications will be detectable by all.

  • Q : What is Carriage return Carriage return

    Carriage return: The \r character. It is also used as a synonym for the `Return' or `Enter' key employed to terminate a line of text and the name derives from the carriage on mechanical typewriter.

  • Q : Business Process Management Describe

    Describe what is Business Process Management (or BPM) in brief.