![]() |
code for friend class
what should be the code sketch for friendship of two classes.......:worried:
|
Re: code for friend class
We can declare function or class as friend.The friend function can access the private and protected members of that class.
we can also define a class as friend of another one, granting that first class access to the protected and private members of the second one. Refer the following example. Code:
// friend classYou may also see something new at the beginning of the program: an empty declaration of class CSquare. This is necessary because within the declaration of CRectangle we refer to CSquare (as a parameter in convert()). The definition of CSquare is included later, so if we did not include a previous empty declaration for CSquare this class would not be visible from within the definition of CRectangle. Consider that friendships are not corresponded if we do not explicitly specify so. In our example, CRectangle is considered as a friend class by CSquare, but CRectangle does not consider CSquare to be a friend, so CRectangle can access the protected and private members of CSquare but not the reverse way. Of course, we could have declared also CSquare as friend of CRectangle if we wanted to. Another property of friendships is that they are not transitive: The friend of a friend is not considered to be a friend unless explicitly specified. |
| All times are GMT +5.5. The time now is 18:31. |