WEBVTT

0
00:00.660 --> 00:04.770
Hello again! In this video, we are going to look at printing out class member data.

1
00:06.730 --> 00:11.710
So far, we have been using a print() member function, which sends the data members to cout.

2
00:12.940 --> 00:16.960
We cannot use the left shift operator because that does not compile.

3
00:21.150 --> 00:24.030
And we get all these horrible templates error messages.

4
00:24.210 --> 00:29.250
The problem is there is no left shift operator which takes a Test object as one of its arguments,

5
00:29.250 --> 00:30.960
and there is no way to convert to it.

6
00:33.410 --> 00:39.050
So if we use the print() member function that works, but it is rather limited because we can only use it

7
00:39.050 --> 00:40.010
with cout.

8
00:43.200 --> 00:46.530
Perhaps we can make this more general, so we can use it with any output stream.

9
00:49.320 --> 00:55.470
So now we have the same class again, but this time we have added an output stream as the argument to the

10
00:55.470 --> 00:59.610
print() member function. So we can now send the data members to the output stream.

11
01:00.030 --> 01:04.440
And this will work for any type of output stream, regardless of whether it is iostream, fstream

12
01:04.440 --> 01:05.430
or stringstream.

13
01:06.530 --> 01:09.750
We need to pass the argument by reference for two reasons.

14
01:10.290 --> 01:14.550
First of all, because the function is going to modify the stream. It is going to push data onto it

15
01:15.000 --> 01:18.960
and change its state, and also because ostreams cannot be copied.

16
01:19.440 --> 01:21.090
So we cannot pass this by value.

17
01:21.810 --> 01:25.840
And then, in the main() function, we pass the stream as argument.

18
01:25.860 --> 01:27.030
So if we want to

19
01:27.540 --> 01:31.260
display data, we pass cout as the arguments.

20
01:31.950 --> 01:36.810
Or alternatively, we can open a file, and use the file stream as the argument to print().

21
01:39.280 --> 01:45.130
So there we are, we get the output and also... We get the file output as well.

22
01:50.000 --> 01:55.160
So that does work, but it would be nice if we could use the left shift operator, so our code would

23
01:55.250 --> 02:01.280
look like all the other code that uses the built-in and the library types. To do that, we need to understand

24
02:01.280 --> 02:05.360
how the library and built-in types work with the left shift operator.

25
02:05.690 --> 02:10.670
And the answer is that the core language and the standard library provide overloads of that operator,

26
02:11.180 --> 02:13.190
which take that type as the second argument.

27
02:13.910 --> 02:19.010
For example, if we send an int to standard output, that will result in the left shift operator being

28
02:19.010 --> 02:21.740
called with cout and i as the arguments.

29
02:22.460 --> 02:26.480
And this is a binary operator, so it cannot be a member function of ostream.

30
02:30.140 --> 02:35.960
We are allowed to chain together calls to this operator. So we can send multiple objects to the same

31
02:35.960 --> 02:38.510
stream in a single statement, like that.

32
02:39.230 --> 02:42.380
And this will actually result in a pair of nested function calls.

33
02:42.920 --> 02:48.200
So first, it is going to call the operator, with cout and i as the arguments, to push i into the stream.

34
02:48.920 --> 02:55.700
And then the return value from that will be used as the stream, and j will be pushed onto that stream.

35
02:57.970 --> 03:04.330
So this operator call must return cout. Again, that must be by reference, not by value.

36
03:04.750 --> 03:10.930
So the prototype is going to operator left shift, which takes an ostream by reference and the

37
03:11.020 --> 03:14.260
data type, and returns the stream by reference.

38
03:19.340 --> 03:22.790
I have implemented this operator for our class.

39
03:23.810 --> 03:24.930
This is outside the class,

40
03:24.950 --> 03:27.860
of course. It is not a member function of the class or the ostream.

41
03:28.550 --> 03:33.200
It takes the ostream and the Test object as argument, by reference.

42
03:34.100 --> 03:39.710
It calls the print() member function of the Test class, with the ostream as the argument, and then

43
03:39.830 --> 03:41.060
returns the same stream.

44
03:41.450 --> 03:45.170
So if we call it repeatedly, it will still work.

45
03:46.650 --> 03:52.260
And then in the main() function, we can use this just like we do with any other object of built-in types

46
03:52.350 --> 03:53.160
or library types.

47
03:53.940 --> 03:56.100
And it works with files as well.

48
04:02.330 --> 04:04.130
Okay, so that is it for this video.

49
04:04.610 --> 04:05.510
I will see you next time.

50
04:05.770 --> 04:07.850
Until then, keep coding!