1 00:00:05,450 --> 00:00:08,740 In this video, we'll learn how the class member access 2 00:00:08,740 --> 00:00:10,549 modifiers work with inheritance. 3 00:00:11,509 --> 00:00:14,930 We'll also learn about the protected class member access modifier. 4 00:00:16,199 --> 00:00:18,980 We've already seen the public and private class member access 5 00:00:18,990 --> 00:00:20,890 modifiers, and we know how they work. 6 00:00:21,410 --> 00:00:25,140 While c++ provides a third access modifier named protected. 7 00:00:26,020 --> 00:00:29,460 We define protected data members and methods exactly 8 00:00:29,460 --> 00:00:30,960 as we do public and private. 9 00:00:31,710 --> 00:00:34,360 We provide the protected keyword followed by a colon 10 00:00:34,369 --> 00:00:35,810 within the class declaration. 11 00:00:36,870 --> 00:00:39,950 All data and method declarations that follow the protected block 12 00:00:39,970 --> 00:00:41,379 will have protected access. 13 00:00:42,010 --> 00:00:45,180 So protected class members are accessible to all 14 00:00:45,180 --> 00:00:46,900 methods of the class itself. 15 00:00:47,300 --> 00:00:51,080 Protected class members are also accessible by any derived classes that 16 00:00:51,080 --> 00:00:52,799 have been derived from the base class. 17 00:00:53,920 --> 00:00:57,460 However, protected class members act like private members. 18 00:00:57,630 --> 00:01:01,069 So they're not accessible directly from objects of either the base 19 00:01:01,070 --> 00:01:02,899 class or of the derived class. 20 00:01:04,218 --> 00:01:07,050 So you can see the protected class members act very much like 21 00:01:07,059 --> 00:01:10,310 private class members unless there's inheritance involved. 22 00:01:10,930 --> 00:01:14,279 Let's see the syntax once more so we can discuss exactly what's 23 00:01:14,280 --> 00:01:16,100 available to the derived class. 24 00:01:17,870 --> 00:01:21,930 Here you can see that we have a base class and that base class has public, 25 00:01:21,990 --> 00:01:24,250 protected and private class members. 26 00:01:25,230 --> 00:01:28,689 We'll use three integers: a b and c each within a different 27 00:01:28,709 --> 00:01:32,080 access modifier to understand how inheritance works with access 28 00:01:32,080 --> 00:01:34,340 modifiers in the next few slides. 29 00:01:36,130 --> 00:01:39,810 Since c++ provides three types of inheritance, we have to 30 00:01:39,810 --> 00:01:43,000 look at each type individually since the access depends on the 31 00:01:43,000 --> 00:01:44,429 type of inheritance involved. 32 00:01:45,250 --> 00:01:49,039 In the case of public inheritance, public class members are 33 00:01:49,039 --> 00:01:52,490 inherited, and they're also public in the drive class. 34 00:01:53,440 --> 00:01:56,330 Protected class members are inherited, and they're also 35 00:01:56,330 --> 00:01:58,020 protected in the drive class. 36 00:01:58,630 --> 00:02:02,929 Finally, private class members are also inherited, however, the derived 37 00:02:02,970 --> 00:02:04,919 class does not have access to them. 38 00:02:05,690 --> 00:02:10,070 Any attempt to access base class private class members from the derived 39 00:02:10,070 --> 00:02:11,830 class will result in a compiler error. 40 00:02:12,360 --> 00:02:14,819 This is the most common form of inheritance used in c++. 41 00:02:15,920 --> 00:02:17,690 You can see that the rules make sense. 42 00:02:17,950 --> 00:02:18,960 Public is public. 43 00:02:19,150 --> 00:02:22,150 Protected is protected, which means that no matter how deep 44 00:02:22,160 --> 00:02:25,450 the class hierarchy goes, the protected class members will be 45 00:02:25,450 --> 00:02:27,559 accessible by all derived classes. 46 00:02:28,470 --> 00:02:32,079 Finally, something marked as private in the base class implies 47 00:02:32,080 --> 00:02:34,570 that the developer of the base class doesn't want to grant 48 00:02:34,599 --> 00:02:36,209 access to those class members. 49 00:02:38,240 --> 00:02:40,079 Now let's look at protected inheritance. 50 00:02:40,430 --> 00:02:43,100 Remember, this is not is a inheritance. 51 00:02:43,870 --> 00:02:47,560 The public-based class members are protected in the derived class. 52 00:02:48,280 --> 00:02:51,470 Protected base class members are protected in the derived class. 53 00:02:51,860 --> 00:02:55,649 And private-based class members are not accessible from the drive class. 54 00:02:56,299 --> 00:02:58,379 Finally, let's look at private inheritance. 55 00:02:59,779 --> 00:03:02,890 Again, private inheritance is not is a inheritance. 56 00:03:03,530 --> 00:03:06,440 So in the case of private inheritance, the base class public 57 00:03:06,440 --> 00:03:11,020 and protected class members are private in the derived class, and 58 00:03:11,020 --> 00:03:14,030 the private class members are not accessible from the drive class. 59 00:03:15,000 --> 00:03:17,810 This may seem confusing and a lot to remember, but it's 60 00:03:17,810 --> 00:03:20,620 really a very logical approach to inheriting class members. 61 00:03:21,220 --> 00:03:25,730 As mentioned previously, inheritance in c++ is a very complex topic. 62 00:03:26,360 --> 00:03:29,559 We'll concentrate on single public inheritance in this course. 63 00:03:30,179 --> 00:03:32,840 Now let's head over to the ide and walk through an example, 64 00:03:32,990 --> 00:03:36,190 using access modifiers and public inheritance and live code. 65 00:03:37,960 --> 00:03:39,310 Okay, so I'm in the IDE. 66 00:03:39,310 --> 00:03:44,570 I'm in the section 15 workspace in the ProtectedMembersClassAccess project. 67 00:03:45,020 --> 00:03:48,359 And what I've got here is a real simple class similar to the one 68 00:03:48,360 --> 00:03:51,560 that we created on the slides where I've got a base class 69 00:03:51,560 --> 00:03:53,020 that I'm declaring right here. 70 00:03:53,230 --> 00:03:56,760 And it's got a public integer a, which I'm initializing to 71 00:03:56,760 --> 00:03:59,110 0 and a method called display. 72 00:03:59,670 --> 00:04:03,690 I've also got a protected integer b and a private integer c, 73 00:04:03,719 --> 00:04:05,350 both of them initialized to 0. 74 00:04:05,800 --> 00:04:09,200 Now the first thing we'll look at is this line right here, line 11. 75 00:04:09,770 --> 00:04:13,660 Obviously, member methods have access to everything in the class, so there's 76 00:04:13,660 --> 00:04:19,160 no problem here accessing a b or c regardless of their access modifier. 77 00:04:19,339 --> 00:04:20,480 So this will be just fine. 78 00:04:20,889 --> 00:04:23,490 Something else I wanted to note was that if we have a friend of 79 00:04:23,490 --> 00:04:26,739 this base class, right, a friend function or a friend class. 80 00:04:27,020 --> 00:04:30,159 Then we'll have access to public protected and private 81 00:04:30,160 --> 00:04:31,470 information in this class. 82 00:04:32,250 --> 00:04:36,350 Okay, so let's look at this first at the object level before 83 00:04:36,350 --> 00:04:37,620 we even get into inheritance. 84 00:04:37,860 --> 00:04:39,630 Here we have a base class. 85 00:04:39,940 --> 00:04:43,690 Why don't we create a base object, and it's right here on line 31, I'm 86 00:04:43,690 --> 00:04:48,089 creating an object called base and its class is base right up here. 87 00:04:48,580 --> 00:04:52,430 Okay, we've got those three integers: a b and c. 88 00:04:53,050 --> 00:04:54,660 Now let's access them all. 89 00:04:54,670 --> 00:05:04,030 So let's say base.a is 100, base.b is 200, and base.c is 300. 90 00:05:05,289 --> 00:05:06,339 Now what do we expect? 91 00:05:06,359 --> 00:05:09,820 Well, we've been through this before with public and private. 92 00:05:09,830 --> 00:05:11,340 The only difference here is protected. 93 00:05:11,630 --> 00:05:14,750 We expect base.a to be okay, right. 94 00:05:15,090 --> 00:05:18,750 It's public in base, so the objects have access to it. 95 00:05:19,240 --> 00:05:23,570 And we expect base.c to give us a compiler error, 96 00:05:24,029 --> 00:05:25,510 right because it's private. 97 00:05:25,600 --> 00:05:28,330 We don't have access to private data from our objects. 98 00:05:28,570 --> 00:05:29,440 This is nothing new. 99 00:05:29,450 --> 00:05:30,719 This is what we've learned all along. 100 00:05:31,390 --> 00:05:34,680 Now there is no inheritance in here, right, because we're only dealing 101 00:05:34,680 --> 00:05:36,250 with that base class right now. 102 00:05:36,309 --> 00:05:37,990 There's no derived class yet here. 103 00:05:38,280 --> 00:05:43,090 So in this case, I also expect the compiler error because as far as the 104 00:05:43,090 --> 00:05:48,010 compiler is concerned, the protected and private access modifiers really 105 00:05:48,010 --> 00:05:49,760 are the same when it comes to objects. 106 00:05:50,070 --> 00:05:53,729 So this object base has no business messing with b or 107 00:05:53,730 --> 00:05:56,139 c because they're protected and the other one is private. 108 00:05:56,509 --> 00:05:59,940 So if I compile this -- if i try to build and run this, I should get two 109 00:05:59,940 --> 00:06:03,380 compiler errors and you can see them right there on the red arrows right 110 00:06:03,380 --> 00:06:07,890 here: one says b is protected and the other one should say c is private. 111 00:06:08,980 --> 00:06:10,800 Okay, so that makes sense, right. 112 00:06:10,849 --> 00:06:13,039 That's working with objects. 113 00:06:13,299 --> 00:06:17,000 So let me comment these two lines, and we'll move on now. 114 00:06:17,400 --> 00:06:19,920 Okay, so now let's talk about inheritance. 115 00:06:20,740 --> 00:06:23,720 Right here we're create -- we've got our base class just as before. 116 00:06:23,970 --> 00:06:25,870 Now I'm creating a derived class. 117 00:06:27,000 --> 00:06:29,570 I'm deriving this class publicly from base. 118 00:06:29,580 --> 00:06:32,600 So I'm inheriting everything in base and I'm using those rules that 119 00:06:32,600 --> 00:06:34,419 we saw with public inheritance. 120 00:06:34,459 --> 00:06:38,520 So a is public in my parent, so it's public in me. 121 00:06:39,110 --> 00:06:42,630 B is protected in my parent, so it's protected in me. 122 00:06:43,100 --> 00:06:47,450 C is private in my parent, so I inherit it, but it is not accessible. 123 00:06:48,320 --> 00:06:52,650 Okay, so it's important to understand that c is indeed inherited, and we'll 124 00:06:52,650 --> 00:06:55,539 look at that when we walk through the debugger when we step through it. 125 00:06:56,010 --> 00:06:58,710 You just don't have access to it from within derived. 126 00:06:59,160 --> 00:07:03,030 It makes sense that it's part of you since it is part of your parent. 127 00:07:04,150 --> 00:07:07,780 Okay, so now what do we have access to here. 128 00:07:07,969 --> 00:07:11,500 Well, we should have access to a and b, right, within our derived. 129 00:07:11,980 --> 00:07:13,750 We should not have access to c. 130 00:07:14,000 --> 00:07:16,030 So why don't we write a real simple method here. 131 00:07:16,030 --> 00:07:20,510 We'll just call it void access base members, let's say, 132 00:07:22,480 --> 00:07:23,880 and this won't do anything. 133 00:07:23,880 --> 00:07:26,880 We'll just access those things and see when we get compiler errors. 134 00:07:27,160 --> 00:07:30,950 So we're going to say a equals 100, let's say, right here. 135 00:07:31,200 --> 00:07:34,570 Now again, we expect this to be okay because a is public in 136 00:07:34,570 --> 00:07:36,000 my parent, it's public and me. 137 00:07:36,900 --> 00:07:43,139 We also expect b equals 200 to be okay because it's protected in 138 00:07:43,270 --> 00:07:45,030 my parent, it's protected in me. 139 00:07:45,240 --> 00:07:46,420 This is a member function. 140 00:07:46,449 --> 00:07:47,670 It's got access to it. 141 00:07:48,850 --> 00:07:49,990 This is the problem here. 142 00:07:50,000 --> 00:07:54,090 If I say c is 300, c is not accessible. 143 00:07:55,490 --> 00:07:58,029 So we expect a compiler error here. 144 00:07:58,540 --> 00:08:00,619 Okay, again, remember c is there. 145 00:08:00,620 --> 00:08:04,770 If you need to access c or modify c, you can't do it directly from derived. 146 00:08:04,779 --> 00:08:07,810 You'll have to go through some getter function or setter function 147 00:08:08,100 --> 00:08:11,320 provided in the base class, but we'll talk about that later. 148 00:08:11,790 --> 00:08:15,000 So let's compile this now, and we expect to get a compiler error 149 00:08:15,000 --> 00:08:18,170 right there on line 28, and that's exactly what happens, right. 150 00:08:18,599 --> 00:08:20,150 C is not accessible. 151 00:08:20,340 --> 00:08:22,300 So you can see how these rules apply. 152 00:08:22,609 --> 00:08:23,870 Now I'll comment that out. 153 00:08:25,270 --> 00:08:28,820 And we'll do one last thing is we'll come down here now. 154 00:08:29,860 --> 00:08:33,750 And remember, so what we did was we created base object and tried 155 00:08:33,750 --> 00:08:36,289 to access those three data members. 156 00:08:36,808 --> 00:08:41,190 Then we created a derived class and saw what the access was here. 157 00:08:41,469 --> 00:08:44,310 Now we're going to do it from a derived object, so let's do that. 158 00:08:44,320 --> 00:08:49,640 Let's say derived, let's just call it d, and we'll do the same thing. 159 00:08:49,640 --> 00:08:55,660 We'll say d.a is 100, d.b is 200 and d.c is 300. 160 00:08:55,660 --> 00:09:03,630 Okay, so take a look at a. 161 00:09:04,630 --> 00:09:08,829 I'm accessing the a data member in the derived class. 162 00:09:08,840 --> 00:09:10,560 Well, let's move up here just a little bit. 163 00:09:11,090 --> 00:09:14,790 A is going to be public, so i should expect that to work. 164 00:09:15,160 --> 00:09:18,399 But look in b and c, protected and not accessible. 165 00:09:18,959 --> 00:09:22,349 I don't have access to protected data, and I certainly don't have access 166 00:09:22,380 --> 00:09:24,089 to something that's not accessible. 167 00:09:24,349 --> 00:09:29,849 So we expect this line to be okay, and we expect both of these to 168 00:09:29,860 --> 00:09:32,029 give us compiler errors right here. 169 00:09:34,110 --> 00:09:35,959 All right, so let's give this a build and run. 170 00:09:35,959 --> 00:09:38,870 And there you see everything else is clean. 171 00:09:38,870 --> 00:09:40,100 I'll scroll up just a little bit. 172 00:09:40,410 --> 00:09:42,300 And there you can see the compiler errors. 173 00:09:42,620 --> 00:09:45,739 And it says just like you would expect, this is protected in this 174 00:09:45,740 --> 00:09:50,099 context, and this guy is -- oops sorry, I forgot a semicolon, let 175 00:09:50,179 --> 00:09:52,940 me do that one more time, make sure we get the right compiler errors. 176 00:09:53,080 --> 00:09:53,770 There we go. 177 00:09:53,820 --> 00:09:55,990 So we still got the same two compiler errors, but there 178 00:09:55,990 --> 00:09:56,960 are different errors now. 179 00:09:57,180 --> 00:10:00,390 So in this case, b is protected, so we don't have access to it. 180 00:10:00,820 --> 00:10:04,449 And down here, c is private, so we certainly, don't have access to it. 181 00:10:05,340 --> 00:10:09,179 Okay, all right, So that's really what I wanted to show you in this video. 182 00:10:09,450 --> 00:10:11,619 You can see that this makes sense. 183 00:10:11,719 --> 00:10:15,740 All these protected data members will be inherited, inherit, inherited 184 00:10:15,740 --> 00:10:19,160 all the way down the hierarchy so you've got access to them throughout. 185 00:10:20,360 --> 00:10:23,990 Okay, so the last thing I want to do is I want to put a break point 186 00:10:24,000 --> 00:10:28,759 right here on line 42, right, where we're creating that derived object d. 187 00:10:29,400 --> 00:10:32,420 And I want to debug this and step through it, mainly just so 188 00:10:32,420 --> 00:10:33,910 we can see what d looks like. 189 00:10:34,990 --> 00:10:36,630 Okay, so I'm right there at line 42. 190 00:10:36,630 --> 00:10:40,060 I'm going to step, and I've just created that object. 191 00:10:40,480 --> 00:10:42,020 And let's refresh this over here. 192 00:10:42,020 --> 00:10:43,379 And first, let's look at base. 193 00:10:44,230 --> 00:10:46,419 Our base object, which you can see right here on line 194 00:10:46,430 --> 00:10:51,010 36, has an a b and a c in it, that's exactly what we expect. 195 00:10:51,340 --> 00:10:56,060 B and c are 0, and a is 100 because I set that to 100 on line 196 00:10:56,070 --> 00:10:57,870 37, as you can see right here. 197 00:10:58,469 --> 00:11:00,829 Okay, but now what about the derived object. 198 00:11:01,070 --> 00:11:03,280 What would you expect that to look like internally. 199 00:11:03,690 --> 00:11:06,920 Well, it is a base, right, because we're using public inheritance. 200 00:11:06,920 --> 00:11:10,829 You expect a b and c to be in there, and that's exactly what you have. 201 00:11:10,830 --> 00:11:13,620 There's your base part, and there's a b and c. 202 00:11:13,620 --> 00:11:18,480 So that derived object d has a base object within it. 203 00:11:19,290 --> 00:11:22,250 It seems obvious right, but a lot of students get this confused 204 00:11:22,250 --> 00:11:26,290 because of that one rule that says private elements are not accessible. 205 00:11:26,530 --> 00:11:29,600 Somehow they sometimes think that not accessible means you didn't 206 00:11:29,640 --> 00:11:31,990 inherit it, and that's not the case. 207 00:11:31,990 --> 00:11:33,609 You can see, it's really here, right. 208 00:11:33,610 --> 00:11:35,160 You can see that that c is there. 209 00:11:35,460 --> 00:11:38,449 It has to be there since you are one of your parents. 210 00:11:39,020 --> 00:11:41,080 So we're not deleting stuff. 211 00:11:41,090 --> 00:11:42,599 We're not disinheriting stuff. 212 00:11:42,600 --> 00:11:44,370 So we're getting everything from our parent. 213 00:11:44,590 --> 00:11:47,740 So just because we don't have access to, it doesn't mean it's not there. 214 00:11:48,150 --> 00:11:50,060 I wanted to be really clear on that because I see students 215 00:11:50,060 --> 00:11:51,309 misunderstanding that a lot. 216 00:11:51,820 --> 00:11:55,070 All right, so in the next video, we'll talk about constructors and 217 00:11:55,080 --> 00:11:58,930 destructors and the order that stuff happens in with inheritance.