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