why freind function takes more parameter than normal member
function in c++?



why freind function takes more parameter than normal member function in c++?..

Answer / jeremiah

A friend function needs to know the object on which it must
operate. Class member functions have an implicit "this"
pointer which define the object on which it must operate.

Example:
---------------------------------------------------------
class Number;

// A function that adds two numbers.
// This function must be a friend of the Number class
// because it operates on a private member variable of the
// Number class.
Number Add( const Number& lhs, const Number& rhs )
{
Number result;
result = lhs.m_value + rhs.m_value;
return result;
}

// The Number class
class Number
{
public:
// Constructor - Default to 0.
explicit Number( int value = 0 )
: m_value( value )
{}

// Copy constructor
Number( const Number& copy )
: m_value( copy.m_value )
{}

// Add this number with another and return the result.
// This operator only needs 1 argument because the
// Left-hand side is assumed to be the class itself.
Number operator+( const Number& rhs ) const
{
Number result;
result.m_value = m_value + rhs.m_value;
return result;
}

private:
// Allow the Add function to access the private members
// of this class by making it a friend function.
friend Number Add( const Number&, const Number& );
int m_value;
};
-----------------------------------------------------------

As you can see from the example, the "Add" function takes
two arguments (a left-hand side operator, and a right-hand
side operator), whereas the Number::operator+ member
function only needs to know the right-hand side operator
because the left-hand side operator is assumed to be the
class instance itself.

Is This Answer Correct ?    4 Yes 0 No

Post New Answer

More OOPS Interview Questions

what do you mean by static member variable?

2 Answers  


What are properties in oop?

0 Answers  


Can we have inheritance without polymorphism?

0 Answers  


What do you mean by variable?

0 Answers  


What is encapsulation in oops?

0 Answers  






Question: Write a program that prints a paycheck. Ask the program user for the name of the employee, the hourly rate, and the number of hours worked. If the number of hours exceeds 40, the employee is paid “time and a half”, that is, 150 percent of the hourly rate on the hours exceeding 40. Be sure to use stepwi se refine ment and break your solution into several functions. Use the int_name function to print the dollar amount of the check.

0 Answers  


What is polymorphism explain its types?

0 Answers  


State what is encapsulation and friend function?

0 Answers   BirlaSoft,


Why is it so that we can have virtual constructors but we cannot have virtual destructors?

2 Answers  


difference between class and object

10 Answers   Chandan, IBM, Magic Soft,


what is difference between String s=new String("vali"); String s="vali"

1 Answers  


what type of question are asked in thoughtworks pair programming round ?

0 Answers   Thought Works,


Categories