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

wht is ditch

0 Answers  


#include <stdio.h> #include <alloc.h> #include <stdlib.h> #include <conio.h> void insert(struct btreenode **, int); void inorder(struct btreenode *); struct btreenode { struct btreenode *leftchild; struct btreenode *rightchild; int data; }; main() { struct btreenode *bt; bt=(struct btreenode *)NULL; int req,i=1,num; clrscr(); printf("Enter number of nodes"); scanf("%d",&req); while(i<=req) { printf("Enter element"); scanf("%d",&num); insert(&bt,num); i++; } inorder(bt); } void insert(struct btreenode **sr, int num) { if(*sr==NULL) { *sr=(struct btreenode *)malloc (sizeof(struct btreenode)); (*sr)->leftchild=(struct btreenode *)NULL; (*sr)->rightchild=(struct btreenode *)NULL; (*sr)->data=num; return; } else { if(num < (*sr)->data) insert(&(*sr)->leftchild,num); else insert(&(*sr)->rightchild,num); } return; } void inorder(struct btreenode *sr) { if(sr!=(struct btreenode *)NULL) { inorder(sr->leftchild); printf("\n %d",sr->data); inorder(sr->rightchild); } else return; } please Modify the given program and add two methods for post order and pre order traversals.

0 Answers  


What polymorphism means?

0 Answers  


what is difference between thread and programme.

1 Answers   NCC,


What is super in oop?

0 Answers  






There are two base class B1,B2 and there is one class D which is derived from both classes, Explain the flow of calling constructors and destructors when an object of derived class is instantiated.

0 Answers  


diff between Abstract class Interfaces?

4 Answers  


How to Increment the value of the empid E001 for each and every employee by using the programe?

1 Answers   Accenture,


define oops concept with example

1 Answers   Cap Gemini,


what is the difference between a package and a software?

3 Answers  


How to execute business logic for only once ..?even though user clicks submit button multiple times by mistake..? (i disabled JavaScript)

1 Answers  


what is ns string? what is ns array?

1 Answers  


Categories