If class First is declaring class Second as a Friend, then

Options
- Both the classes can access each other’s data.
- Only class Second can access data of class First.
- Only class First can access data of class Second.
- None of these.


CORRECT ANSWER : Only class Second can access data of class First.

Discussion Board
C++ - Friend Class

If class First is declaring class Second as a Friend, then only class Second can access data of class First. It is also possible to make an entire class a friend of another class. This gives all of the members of the friend class access to the private members of the other class. Any of Second class’s members that use a First class object can access the private members of First class directly.

A few additional notes on friend classes:

1. Even though Second class is a friend of First class, Second class has no direct access to the *this pointer of First class objects.

2. Just because Second class is a friend of First class, that does not mean First Class is also a friend of Second class. If you want two classes to be friends of each other, both must declare the other as a friend. Finally, if class A is a friend of B, and B is a friend of C, that does not mean A is a friend of C.

Be careful when using friend functions and classes, because it allows the friend function or class to violate encapsulation. If the details of the class change, the details of the friend will also be forced to change.

Prajakta Pandit 01-27-2017 03:30 AM

shouldn't it be None of these

Because class First can access its data as well. But the statement says "ONLY class second" which excludes class first.

PW 03-25-2016 11:16 AM

Write your comments


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)


Advertisement