WEBVTT

00:00.690 --> 00:04.260
Hello again! In this video, we are going to look at the friend keyword.

00:05.760 --> 00:11.940
If we have a function which takes some object as argument, then the function can only access the public

00:11.940 --> 00:17.220
members of the class. So it can call any public member functions of the class.

00:17.910 --> 00:21.960
If that class has any public data members, it can access those as well.

00:22.470 --> 00:25.200
But anything which is private is inaccessible.

00:27.140 --> 00:33.260
When we are writing overloaded operators, we sometimes need to be able to access private data from outside

00:33.260 --> 00:33.740
the class.

00:34.790 --> 00:40.640
C++ provides a way of doing that, which I do not particularly like, but it is very convenient.

00:41.210 --> 00:44.420
And that is for the class to declare the function as a friend.

00:45.050 --> 00:51.290
For example, if we have a function which takes a Test object as argument, and the Test class has

00:51.290 --> 00:56.690
declared the print() function as a friend by putting the friend keyword in front of its signature.

00:57.470 --> 01:04.190
Then the print() function has full access to the class's members, so it can access the data members.

01:04.880 --> 01:06.410
So here is that code.

01:06.410 --> 01:11.870
I have added some members to the class with initializers. In the main function,

01:11.870 --> 01:13.490
we create an object of this class.

01:13.820 --> 01:16.400
And then we pass it to the print() function.

01:19.650 --> 01:26.670
So there we are, the print() function can access the data members. As well as making a non-member

01:26.670 --> 01:27.720
function a friend,

01:28.050 --> 01:30.450
we can also make an entire class a friend.

01:30.900 --> 01:36.120
So in this example, we have our class Test, which says that the class Example is a friend.

01:37.200 --> 01:42.840
And that means that all the member functions of Example have unlimited access to the members of Test.

01:44.220 --> 01:46.530
So here we have a class Example.

01:46.890 --> 01:49.650
So this print() is now a member function of the Example class.

01:49.650 --> 01:53.280
Example is a friend of Test.

01:54.180 --> 02:00.730
So this means that all the member functions of Example have access to test's private data. And then

02:00.740 --> 02:04.680
in the main() function, we create an object of Example as well as Test.

02:05.220 --> 02:06.690
And then we call this member function.

02:10.920 --> 02:17.880
So there we are, we can access the private data from a member function of the friend class. And some

02:17.880 --> 02:22.200
C++ programmers, including me, are not specifically fond of "friend"!

02:22.500 --> 02:24.630
The reason is that it reduces encapsulation.

02:25.260 --> 02:31.140
If you have some external function which can access any data member of the class, then in theory

02:31.140 --> 02:36.600
it can do anything to your class. So it can call member functions which are only meant for internal

02:36.600 --> 02:36.930
use.

02:37.290 --> 02:43.680
It can change the values of all your private data. And, either by design or by accident,

02:44.190 --> 02:49.170
someone can modify this friend function to do all sorts of things and creates lots of problems.

02:49.920 --> 02:55.350
So we can avoid this instead. Of having the external function as a friend, we can make its delegate

02:55.360 --> 02:56.820
to a member function of our class.

02:57.690 --> 03:01.320
So we would write a member function in our class, which does everything

03:01.320 --> 03:03.960
that the external function needs to do.

03:04.500 --> 03:07.590
And then the external function will just call this member function.

03:09.950 --> 03:14.900
So we have something like this. In our Test class. We have a member function which does everything that

03:14.900 --> 03:22.280
the print() function needs. And then the external print() function will just call this member function.

03:22.640 --> 03:23.680
And that is much safer.

03:24.620 --> 03:29.930
The external function has no access at all to the internal details of this class.

03:32.210 --> 03:34.110
So here is that code now.

03:34.130 --> 03:39.860
We have the member function, which does all the print functionality, and then we have this external

03:39.860 --> 03:42.140
function, which just calls the member function.

03:43.100 --> 03:46.280
The main() program is exactly the same as it was in the first example.

03:51.800 --> 03:57.740
So I admit, these examples are rather artificial, but we are laying a foundation. When we start actually

03:57.740 --> 03:59.120
writing overloaded operators,

03:59.480 --> 04:02.450
We will need this, and we will do something that is a bit more useful.

04:03.180 --> 04:05.180
Okay, so that is it for this video.

04:05.700 --> 04:06.580
I will see you next time.

04:06.830 --> 04:08.510
Until then, keep coding!
