Monday, February 7, 2011

Friend Function & class

FRIEND FUNCTIONS
If we want to declare an external function as a friend of class
thus allowing this function to have access to the private and protected
members of this class,we do it by declaring a prototype of this external
functions within the class and preceding it with keyword 'friend'.
For example, we could define an operator that multiplies a Matrix by a Vector . Naturally,
Vector and Matrix each hide their representation and provide a complete set of operations for
manipulating objects of their type. However, our multiplication routine cannot be a member of
both. Also, we don’t really want to provide lowlevel
access functions to allow every user to both
read and write the complete representation of both Matrix and Vector .
To avoid this, we declare the operator  a friend of both:

c l a s s M a t r i x ;
c l a s s V e c t o r
 {
f l o a t v [4 ];
f r i e n d V e c t o r o p e r a t o r *(c o n s t M a t r i x &, c o n s t V e c t o r &);
};
c l a s s M a t r i x
{
V e c t o r v
f r i e n d V e c t o r o p e r a t o r
};


reference(bjaurne.S book)


FRIEND CLASS
 we can define a class as friend of another class,by granting
that first class access to the protected and private members of
the second one.

No comments:

Post a Comment

Which Programming language you prefer?