Why use friends?

I don't see the point in using friends in classes, they are not members, their scope is outside of the class, but they have access to the private data.

So why not just use a regular member?

The stream operators << and >> can't be defined as a members because the first argument should be the stream, but these operators could be considered part of the class implementation. It is not unthinkable that << and >> needs access to the private members of the class so making them friends makes perfect sense.
A class e.g School: has member data students and member functions teachers.
The functions teach the students.
Another class e.g Industry: has its own data members and member functions

For the student to work for the industry, there need to be a relation between them.

why not just use a regular member?
The regular member cannot provide the services need by the industry.

There are several ways of creating this relationship.
a:: composition:: build the industry class to be composed of the School class. So it has regular access to the student data member.
b:: inheritance:: let the industry inherit all the public functions of the school class. So that via the get member functions of the school, the industry can have access to the students data member.
c::friendship:: In the last 2 approaches, we create the industry to be composed entirley of the School class(case a) or atleast all public functions of School (case b).
This makes the The Industry to access some other parts of School that are not important for them like payment records and other stuff.

For the industry to access solely student data, you would get a friend.
Topic archived. No new replies allowed.