1 00:00:00,920 --> 00:00:05,330 In this lecture you will learn about the access controls in C plus plus. 2 00:00:05,330 --> 00:00:09,050 So this is the same code as our in our previous lecture. 3 00:00:09,050 --> 00:00:09,440 Right. 4 00:00:09,440 --> 00:00:13,940 But we will we're going to add some access controls and methods here. 5 00:00:13,940 --> 00:00:19,460 So access controls, restrict class member access, public and private. 6 00:00:19,460 --> 00:00:26,540 Ah, there are two major access controls so anyone can access a public member, but only a class can 7 00:00:26,540 --> 00:00:27,920 access its private members. 8 00:00:27,920 --> 00:00:34,580 So all struct members are public by default. 9 00:00:34,580 --> 00:00:40,040 So private members play an important role in encapsulation. 10 00:00:40,040 --> 00:00:44,600 So consider here, for example, my date and time class. 11 00:00:44,600 --> 00:00:50,510 So as it stands, the year member here can be accessed from anywhere. 12 00:00:50,510 --> 00:00:52,940 So for both reading and writing. 13 00:00:52,940 --> 00:00:59,820 So suppose you want to protect against the value of the year here being less than 2019. 14 00:00:59,970 --> 00:01:09,270 So you can accomplish this in two steps you make here private and you require anyone using the class 15 00:01:09,270 --> 00:01:14,640 consumers to interact with the year only through the structs methods. 16 00:01:15,480 --> 00:01:19,800 So, for example, let's make the. 17 00:01:20,650 --> 00:01:22,570 Year, Private. 18 00:01:23,010 --> 00:01:23,620 Year. 19 00:01:25,960 --> 00:01:30,190 And then we're going to add a method here named Oops. 20 00:01:31,690 --> 00:01:34,720 Yeah, we're going to add the method. 21 00:01:40,710 --> 00:01:41,700 Set year. 22 00:01:43,100 --> 00:01:49,760 We will add this set here with pool because it will return us. 23 00:01:49,760 --> 00:01:50,810 True or false? 24 00:01:50,810 --> 00:01:58,670 And the set here will take an integer new year as a parameter. 25 00:01:58,670 --> 00:02:14,480 So for example, if New Year here less than 2019, then return false here and. 26 00:02:15,660 --> 00:02:16,590 Then. 27 00:02:16,590 --> 00:02:17,390 Here. 28 00:02:17,400 --> 00:02:18,030 Here. 29 00:02:19,650 --> 00:02:21,870 He calls New Year. 30 00:02:25,230 --> 00:02:26,310 And the return. 31 00:02:26,310 --> 00:02:26,970 True. 32 00:02:27,720 --> 00:02:37,110 And we're going to add integers integer set get year which will return just an integer and show will 33 00:02:37,110 --> 00:02:41,990 return as the integer value of our year variable. 34 00:02:42,000 --> 00:02:47,370 So let's add this integer get year here. 35 00:02:48,390 --> 00:02:50,880 And return here. 36 00:02:54,010 --> 00:02:59,920 And as you can see here, we made a private integer here. 37 00:02:59,920 --> 00:03:00,520 Here. 38 00:03:00,550 --> 00:03:01,540 So. 39 00:03:04,640 --> 00:03:09,050 So let's I want to explain all of these codes again here. 40 00:03:09,050 --> 00:03:14,960 As you can see here, we cannot use this anymore here. 41 00:03:15,200 --> 00:03:19,490 So in order to use we we need to, for example. 42 00:03:19,490 --> 00:03:22,400 So actually, let's delete this. 43 00:03:22,700 --> 00:03:28,190 So if you want to print something on the screen, you need to use the here. 44 00:03:29,240 --> 00:03:30,710 The variables here. 45 00:03:30,710 --> 00:03:32,990 So first we declare the integer. 46 00:03:33,590 --> 00:03:38,090 My year here and MD dot get year. 47 00:03:38,090 --> 00:03:49,380 So this get year will return you a value that will show you here declared his private member of this 48 00:03:49,400 --> 00:03:49,540 year. 49 00:03:49,640 --> 00:03:50,630 So. 50 00:03:51,780 --> 00:03:55,500 As you can see here, we will declare private here. 51 00:03:55,950 --> 00:04:01,890 So in order to get this, we will make a function public for now. 52 00:04:01,890 --> 00:04:08,730 So we need to make public function here and we will define the private at the end of the code. 53 00:04:08,760 --> 00:04:13,260 So if we define the private in top of the code, then. 54 00:04:15,070 --> 00:04:22,140 The other chords on the bottom here after private will make itself private as well. 55 00:04:22,150 --> 00:04:29,320 So as you can see here, we cannot directly use here, as you can see here, here is private member 56 00:04:29,320 --> 00:04:34,930 of my date and time so we can use here because here is private here. 57 00:04:34,930 --> 00:04:38,710 So we define private members at the end of the line here. 58 00:04:39,310 --> 00:04:45,790 So we need to in order to get here, we need to use function this function here. 59 00:04:45,790 --> 00:04:50,470 So here, here, here, here, MD, dot, get here. 60 00:04:50,470 --> 00:04:57,550 And we showed us the year here and let's change the year MD dot set year. 61 00:04:57,580 --> 00:05:00,970 Let's set the year to. 62 00:05:01,750 --> 00:05:03,910 For example, 2023. 63 00:05:05,230 --> 00:05:08,410 And here we will get here. 64 00:05:09,630 --> 00:05:12,030 And let's print our code here. 65 00:05:13,010 --> 00:05:20,630 So as you can see here, we started here from zero because we didn't assign anything on it. 66 00:05:20,750 --> 00:05:26,630 And then we assign 2023 to here and here. 67 00:05:26,630 --> 00:05:32,060 Let's actually assign something on it set here. 68 00:05:32,060 --> 00:05:34,460 So we don't see zero here. 69 00:05:35,950 --> 00:05:40,090 2019 and let's run it. 70 00:05:41,200 --> 00:05:49,810 As you can see here, we got the result here and we successfully assign 2019 integer on this on the 71 00:05:49,810 --> 00:05:51,300 year variable here. 72 00:05:51,310 --> 00:05:55,480 So let's try 2018 or for example, 2016. 73 00:05:55,930 --> 00:06:02,260 As you can see here, we got the zero because our if statement, as you can see here, returned false. 74 00:06:02,260 --> 00:06:05,490 And this code here did not execute. 75 00:06:05,500 --> 00:06:15,010 So the if statement is triggered and the the other code is not executed in this function. 76 00:06:15,010 --> 00:06:18,430 So that's why we didn't assign anything on it.