In this lecture you will learn about the access controls in C plus plus.
So this is the same code as our in our previous lecture.
Right.
But we will we're going to add some access controls and methods here.
So access controls, restrict class member access, public and private.
Ah, there are two major access controls so anyone can access a public member, but only a class can
access its private members.
So all struct members are public by default.
So private members play an important role in encapsulation.
So consider here, for example, my date and time class.
So as it stands, the year member here can be accessed from anywhere.
So for both reading and writing.
So suppose you want to protect against the value of the year here being less than 2019.
So you can accomplish this in two steps you make here private and you require anyone using the class
consumers to interact with the year only through the structs methods.
So, for example, let's make the.
Year, Private.
Year.
And then we're going to add a method here named Oops.
Yeah, we're going to add the method.
Set year.
We will add this set here with pool because it will return us.
True or false?
And the set here will take an integer new year as a parameter.
So for example, if New Year here less than 2019, then return false here and.
Then.
Here.
He calls New Year.
And the return.
True.
And we're going to add integers integer set get year which will return just an integer and show will
return as the integer value of our year variable.
So let's add this integer get year here.
And return here.
And as you can see here, we made a private integer here.
Here.
So.
So let's I want to explain all of these codes again here.
As you can see here, we cannot use this anymore here.
So in order to use we we need to, for example.
So actually, let's delete this.
So if you want to print something on the screen, you need to use the here.
The variables here.
So first we declare the integer.
My year here and MD dot get year.
So this get year will return you a value that will show you here declared his private member of this
year.
So.
As you can see here, we will declare private here.
So in order to get this, we will make a function public for now.
So we need to make public function here and we will define the private at the end of the code.
So if we define the private in top of the code, then.
The other chords on the bottom here after private will make itself private as well.
So as you can see here, we cannot directly use here, as you can see here, here is private member
of my date and time so we can use here because here is private here.
So we define private members at the end of the line here.
So we need to in order to get here, we need to use function this function here.
So here, here, here, here, MD, dot, get here.
And we showed us the year here and let's change the year MD dot set year.
Let's set the year to.
For example, 2023.
And here we will get here.
And let's print our code here.
So as you can see here, we started here from zero because we didn't assign anything on it.
And then we assign 2023 to here and here.
Let's actually assign something on it set here.
So we don't see zero here.
2019 and let's run it.
As you can see here, we got the result here and we successfully assign 2019 integer on this on the
year variable here.
So let's try 2018 or for example, 2016.
As you can see here, we got the zero because our if statement, as you can see here, returned false.
And this code here did not execute.
So the if statement is triggered and the the other code is not executed in this function.
So that's why we didn't assign anything on it.