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 is overloading in oop?

0 Answers  


just right the logic of it 1--> If few people are electing then every time ur candidate should win 2--> arrange books in box, if box carry weight == books weight then take another box..... find the no of box required.

0 Answers  


What is variable example?

0 Answers  


When is it necessary to use member-wise initialization list in C++?

2 Answers   Adobe,


what is namespace? what are the uses of namespace?

1 Answers  






Write an operator overloading program to write S3+=S2.

2 Answers  


Can you inherit a private class?

0 Answers  


How to improve object oriented design skills?

0 Answers  


What is the full form of oops?

0 Answers  


What is a friend function & its advantage?

2 Answers   TCS,


Base class has two public data members. How can i derive a new class with one datamember as public and another data member as private?.

2 Answers  


Define a class to represent a bank account. Include the following members: Data Members: Name of the Depositor Account Number Type of Account Balance amount in the account Member Functions: To assign the initial values. To deposit an account. To withdraw an amount after checking the balance. Write a C++ main program to display account number, name and balance.

6 Answers  


Categories