Hello everyone.
So in this video we will learn about constrained functions.
So before starting question functions let us first to review our code of function class that we did
in the last session.
OK so we have this function class this has two properties numerator and denominator and both the properties
are private.
Then we have a default constructor and then we have a better met base constructor which takes numerator
and denominator as input.
Okay.
So we made these four functions these four functions were not present earlier but now we have made it.
Why.
Because the numerator and denominator property both add private field.
So if we want to exist the numerator and denominator we have to make good friends central functions.
So we have a function get numerator and only then numerator.
Similarly we have a function get denominator so dysfunction returns through the denominator.
Then we have a function set numerator.
It will set the numerator said denominator it will set the denominator.
Okay.
And then we have brain function simplify function add function and multiply function.
So all of these functions were killed in the last class we just created this for new functions.
Okay.
Get any set of functions.
Now we have two objects faction EF 1 and faction EF 2.
Okay so what is this index for creating AI and Deja constant.
So this index is quite familiar.
We are quite familiar with this index.
So in days 5.
Now if I want to create an digit constant and cause to be equal C so this is this index for creating
and data constraint.
Now similarly we can create object constraint we can create constant objects so this indexes fraction
then constant keyword F3.
Okay.
So this is just an index for creating a unconstrained object.
Now if I do B plus plus here this is wrong at everybody.
Because we cannot change its properties.
Similarly we cannot change the properties of f3 because it is a constant friction.
We cannot change the properties of f3.
Okay.
So now let's encode
so let us make a constant object so friction constant keyword F3.
Okay now let us try to print the values.
Let us try to printer's properties numerator and denominator so c out after dot get denominator Saudi
we will first print numerator so numerator
and now let us print denominator.
So I have to don't get a denominator okay.
Now let us try to change the value of numerator also so I have three not set numerator and let us give
the value then.
Okay so let's try to analyze what will be our output
so we are creating a constraint object F3 since it is a constant object.
We cannot change its properties.
Okay but in the get numerator sort of this function is a good numerator.
So in that get the numerator and get the number of function we are not changing the property.
These function are just pretending the properties.
Okay.
So I believe we can call these functions but see this line set numerator so this function will change
the value of numerator since this function change there enough numerator.
We cannot call this function okay we cannot call a function which changes the properties of the object.
So ideally these two will run fine and we will get error at this line we will get that at this thing.
Okay.
So what if I will come the.
Then what will happen at this line.
Default constructor will be called default constructor that we have created so our default constructor
will be called and numerator and denominator property will be said to the god which values.
And when we are printing the numerator the denominator we lose we will get garbage and garbage.
Okay.
Both numerator and denominator will contain language values.
Okay so let us run our file.
I believe we will get at the headline number two.
So now let's see that it
Okay so let's end this file so we are getting at it here because numerator user private property we
have to call function get nominated.
Okay so now let's send this file so I believe what should happen we should get that right line number
one but we are getting an headline number 10.
Now let us see data without that is
so we are getting at it.
Get numerator get dominated and certain numerator.
Basically we are getting error on all the three functions we are getting error for this function get
numerator.
We are getting error for this get denominator and we are getting error for said numerator function.
Okay.
We have discussed we should get better at this line.
But we are getting error at all the two functions.
So that means there is something wrong with our approach.
So our approach is wrong our way of thinking was wrong.
There is something else.
Okay let us try to understand what is wrong with our approach to actually what happens is when you create
a constraint object so friction constant F3.
So whenever you create a constraint object we cannot call any function through F3 we cannot call any
function through F3 because compiler is worried about the function that you have called what is inside
that function we some changes to our properties then it's a problem.
Okay.
So basically the compiler won't allow us to call any normal function through F3.
Okay so compiler will not allow us to call any normal function to a constraint object.
We can only call Constraint Function through constraint objects and not any normal functions.
So now the question came What is a constraint function
so what is the constraint function.
A constraint function that does function that do not change the properties of the object.
Okay so constrain functions
which does not
change which does not change any property
of the current object
Okay so constant functions are those functions which do not change the properties of the object.
Okay.
So I hope you have understood the problems.
The problem is compiler will not allow us to call any normal function because the object is constant.
Similarly we cannot call this function because compiler is worried about us.
Okay.
What if we inside the good denominator function which is the value of the denominator or numerator.
I'll get a compiler won't allow us to call any normal function through a constraint object.
Okay so we have to use constraint functions.
Now let's see how to make a function Constraint Function
so C we have a get numerator function.
So what this get numeral function is doing it is simply returning the numerator.
It is not changing the value of any of the properties of the object.
So I believe we should mark this function as question function.
So how do mark a function of constant function.
We simply have to write a keyword constant.
Now this function is a constraint function.
Okay.
And F3 can call this function similarly.
Get denominator function.
This function is only accessing their denominator property so we can also make this function as constant.
Now the constraint object which is F3 can call this function okay.
Now let's turn this file
and now we will run this file now if we will run this file we will only get it for set numerator function
and not error for getting the denominator end good no matter darn good denominator function.
Okay okay so let's wrap this file so this time we are getting an error at line number 10.
So if you will see that it so here that is only due to set numerator function.
Okay.
That is only due to set numerator function so this time we can call get numerator function and get the
number to function because both the function has been marked as Constraint Function.
So compiler will allow us to call these functions.
Okay.
So what compiler will do when we are calling this function get numerator.
So compiler will go to get numerator function.
So where is good numerator.
So get new compiler will reach here get numerator function and this function is constant.
So compiler will allow us to call this function.
Similarly when you call this function get denominator so compiler will reach get the number to function.
Since this function is a constraint function compiler will allow us to call this function and for set
numerator function compiler will reach said no other function since this function is not constraint.
Compiler won't allow us to call this function okay now.
So tell me whether it said normal the function qualified to a constraint function or not.
So the answer is set no matter the function cannot be a constraint function because it is changing the
property.
Okay.
Now what if I make this function of Constraint Function.
Okay.
Eileen this should not be a constraint function.
Now what if I make this function a constraint function what will happen.
Now let us turn our file C we coordinated why we got an error because here we are trying to change the
value of numerator and since the function is constant we cannot do this.
Okay.
So let's remove constraint here.
Now let's send this file again.
So this time our file is running.
There is no added okay now said delimiter function can be a constant function or not.
So same reason I said numerator side in order function doesn't qualify to be a constraint function because
it is because it will try to change the value of denominator not print function at this dog world brain
function can print function qualify for a question function.
Yes it is because it is only accessing the properties.
It is not changing the property.
It is not trying to change that property.
So brain function is a constraint function.
Now let us talk about simplifying can it be a constraint function.
The answer is No.
Why.
Because it is changing the value of because it is changing the properties so it cannot be a constraint
function and function.
It is also changing the properties numerator and denominator so it cannot qualify for a Constraint Function.
Multiply function.
It is also changing the numerator and denominator so it cannot be a constraint function.
Okay so only brain function so only brain function and these two functions are qualifying for the constraint
functions.
Okay.
So whenever we are working with the classes and objects whenever we are working with the functions we
should look carefully it'll function can qualify for a constraint function then I believe we should
make them a constraint function so that constraint object can call these functions.
Okay thank you.