Hello again! In this video, we are going to look at the friend keyword.
If we have a function which takes some object as argument, then the function can only access the public
members of the class. So it can call any public member functions of the class.
If that class has any public data members, it can access those as well.
But anything which is private is inaccessible.
When we are writing overloaded operators, we sometimes need to be able to access private data from outside
the class.
C++ provides a way of doing that, which I do not particularly like, but it is very convenient.
And that is for the class to declare the function as a friend.
For example, if we have a function which takes a Test object as argument, and the Test class has
declared the print() function as a friend by putting the friend keyword in front of its signature.
Then the print() function has full access to the class's members, so it can access the data members.
So here is that code.
I have added some members to the class with initializers. In the main function,
we create an object of this class.
And then we pass it to the print() function.
So there we are, the print() function can access the data members. As well as making a non-member
function a friend,
we can also make an entire class a friend.
So in this example, we have our class Test, which says that the class Example is a friend.
And that means that all the member functions of Example have unlimited access to the members of Test.
So here we have a class Example.
So this print() is now a member function of the Example class.
Example is a friend of Test.
So this means that all the member functions of Example have access to test's private data. And then
in the main() function, we create an object of Example as well as Test.
And then we call this member function.
So there we are, we can access the private data from a member function of the friend class. And some
C++ programmers, including me, are not specifically fond of "friend"!
The reason is that it reduces encapsulation.
If you have some external function which can access any data member of the class, then in theory
it can do anything to your class. So it can call member functions which are only meant for internal
use.
It can change the values of all your private data. And, either by design or by accident,
someone can modify this friend function to do all sorts of things and creates lots of problems.
So we can avoid this instead. Of having the external function as a friend, we can make its delegate
to a member function of our class.
So we would write a member function in our class, which does everything
that the external function needs to do.
And then the external function will just call this member function.
So we have something like this. In our Test class. We have a member function which does everything that
the print() function needs. And then the external print() function will just call this member function.
And that is much safer.
The external function has no access at all to the internal details of this class.
So here is that code now.
We have the member function, which does all the print functionality, and then we have this external
function, which just calls the member function.
The main() program is exactly the same as it was in the first example.
So I admit, these examples are rather artificial, but we are laying a foundation. When we start actually
writing overloaded operators,
We will need this, and we will do something that is a bit more useful.
Okay, so that is it for this video.
I will see you next time.
Until then, keep coding!