1 00:00:05,150 --> 00:00:08,750 In this video, we'll learn about friends of a class in c++. 2 00:00:09,740 --> 00:00:13,100 Friends have always been a controversial topic in c++. 3 00:00:13,600 --> 00:00:16,260 The controversy is about encapsulation. 4 00:00:16,260 --> 00:00:19,260 And whether friends violate encapsulation or enhance it. 5 00:00:19,620 --> 00:00:21,620 You can decide where you are on this issue. 6 00:00:21,620 --> 00:00:25,980 But friends are indeed part of the c++ language and it's important to understand them. 7 00:00:26,380 --> 00:00:28,380 So what is a friend of a class? 8 00:00:29,040 --> 00:00:32,040 A friend of a class is a function or another class 9 00:00:32,040 --> 00:00:34,400 that has access to private class members. 10 00:00:35,000 --> 00:00:39,200 And that other function or class is not a member of the class that it's accessing. 11 00:00:40,190 --> 00:00:42,090 In the case of friend functions, 12 00:00:42,090 --> 00:00:45,390 these can be standalone functions or member methods of another class. 13 00:00:45,750 --> 00:00:47,350 And in the case of another class, 14 00:00:47,600 --> 00:00:51,900 the entire class will have access to the private information of the class granting friendship. 15 00:00:52,560 --> 00:00:55,560 So as you can see, friends have unfettered access 16 00:00:55,560 --> 00:01:00,260 to both public and private data members of a class, but they're not members of the class. 17 00:01:01,860 --> 00:01:05,660 It's important to understand how c++ treats friendship in the big picture. 18 00:01:06,160 --> 00:01:09,360 First, friendship must be granted, not taken. 19 00:01:09,720 --> 00:01:13,820 A class must explicitly declare its friends in its class declaration using 20 00:01:13,820 --> 00:01:17,380 the friend keyword. Friendship is not symmetric. 21 00:01:17,380 --> 00:01:21,080 So if a is a friend of b, that doesn't mean that b is a friend of a. 22 00:01:21,080 --> 00:01:22,780 This must be explicitly granted. 23 00:01:23,580 --> 00:01:25,280 Friendship is not transitive. 24 00:01:25,530 --> 00:01:28,230 if a is a friend of b and b is a friend of c, 25 00:01:28,230 --> 00:01:31,790 then a is not a friend of c, again unless specifically granted. 26 00:01:32,190 --> 00:01:34,390 And finally, friendship is not inherited. 27 00:01:35,990 --> 00:01:38,690 So what does declaring a friend look like in c++. 28 00:01:39,350 --> 00:01:43,150 Here you can see the case of declaring a standalone non-member 29 00:01:43,150 --> 00:01:44,950 function as a friend of the player class. 30 00:01:45,610 --> 00:01:49,840 Notice that the player class grants the friendship by declaring the function prototype 31 00:01:49,840 --> 00:01:51,840 preceded by the friend keyword. 32 00:01:52,640 --> 00:01:56,240 That's it. Now the display player function has access to 33 00:01:56,240 --> 00:01:57,440 everything in the player class. 34 00:01:59,240 --> 00:02:01,140 So in the display player function, 35 00:02:01,140 --> 00:02:04,740 we don't have to go through public getters or setters to display the player information 36 00:02:04,740 --> 00:02:06,100 or set player information. 37 00:02:06,100 --> 00:02:09,100 We can directly access the private class attribute. 38 00:02:09,600 --> 00:02:12,000 In fact, we can change them as well if we wanted to 39 00:02:12,000 --> 00:02:15,360 since the object is passed by reference and in this case non-const. 40 00:02:16,660 --> 00:02:20,020 Let's see how we would declare a method in another class as a friend. 41 00:02:20,680 --> 00:02:23,680 In this case, we're declaring the display player method 42 00:02:23,680 --> 00:02:25,480 of the other class as a friend. 43 00:02:26,080 --> 00:02:29,080 That method will now have access to everything in the player class. 44 00:02:30,580 --> 00:02:32,940 And that method might be implemented as follows. 45 00:02:32,940 --> 00:02:35,940 Notice that it is indeed a member of the other class. 46 00:02:36,300 --> 00:02:39,900 But you can use the player class's private attributes directly within it. 47 00:02:41,260 --> 00:02:44,460 Finally, we can declare an entire separate class as a friend. 48 00:02:45,230 --> 00:02:49,690 In this case, we declare the class other class as a friend of player. 49 00:02:50,130 --> 00:02:54,730 So all the methods in the other class will have access to the player classes private attributes. 50 00:02:55,230 --> 00:02:57,830 As you can see friendship is very powerful, 51 00:02:58,190 --> 00:03:01,850 it can bypass function call overhead associated with getters and setters 52 00:03:01,850 --> 00:03:05,650 setters and other class methods and allow direct access to private attributes. 53 00:03:06,200 --> 00:03:09,000 Sometimes we have classes that use other classes 54 00:03:09,000 --> 00:03:11,500 and writing code using getters and setters becomes long 55 00:03:11,500 --> 00:03:14,000 tedious and sometimes gives you overhead. 56 00:03:14,200 --> 00:03:17,200 However, that shouldn't be the only reason for granting friendship. 57 00:03:17,200 --> 00:03:21,630 Friendships should be granted only when it makes sense in the design of your system 58 00:03:21,630 --> 00:03:24,990 and then only the minimal necessary friendship should be granted. 59 00:03:25,540 --> 00:03:29,900 We'll see friend functions again when we use operator overloading in the next section.