Hello everyone.
So in the last session we learned what operate our loading is and we'll order three of those plus multiply
equal operate that for our reflection class.
So I hope now everyone understand what is Oprah downloading.
And how can you overload operators.
So in this session we will overload few model printers.
OK so first of all we will overload.
Plus plus operators so in this session we will overload plus plus operator.
So there are two versions of plus plus operator.
One is pre increment
and another one is post increment so I hope everyone knows what is there difference between pre and
post increment.
So in this class it will focus on being element.
Okay.
So in this video we will cover bringing equipment in the next we do we will try to cover post increment.
Okay.
So let's start today's class so when I write let's take an example.
So when I write in days 5 and a blessed place and if I tried to print the value of a So what will be
our output Sorry I will loop plus plus item because in this video we are trying to cover increment.
So our output will be six.
Okay.
So what will happen at this line.
I will get a memory block I.
And it contains the value five.
So plus plus I means make the value six and then we will print the value six.
Okay so now we want to overload the pre increment operator for our section class.
So let us take a fraction so that this trick affection EF 1 so with affection EF 1 the value of numerator
is 10 and the value of denominators 2.
So when I write plus plus EF 1 So what will happen then by 2 plus 1 which is 10 plus two my do which
is trial by two and then we will call the simplify function.
So it will become 6 by 1 OK.
So if you remember in the last session we all did plus operator.
So EF 3 is EF 1 plus EF 2.
OK.
So if you look carefully this plus operator is a binary operator.
So what is a binary operator are by NATO operators operator which needs to operate to do this work.
OK.
Which need to print to do its work.
So in today's class we are covering being called many operators of pre increment operator is the unity
operator.
So are you NATO operator needs only one or brand to do its work.
So in this case what will happen.
EF 1 will go in this and F2 will be passed this argument okay.
But for the case of plus plus operator since it's a unity operator F1 will go in this and we do not
need to pass anything in the arguments.
Okay.
So let us try to overload let us try to order the being current operator okay.
So let the scene code.
So what is the return type.
So we are overloading increment number two the return type of the function.
So we are not returning anything.
So for now let us assume the return type is void.
Okay.
We will do changes in direction only.
So for now let us assume the return type is void.
Okay then we have to write operate a keyword and without space the operator which won't overload C++
as we discussed we will not take any friction as argument.
And now let us change the numerator.
Okay.
So when we do plus plus everyone only the numerator will change.
So the numerator will become numerator
plus denominator.
Okay and then determinative will remain the same.
And then we have to call simplify function.
Okay.
We have to call simplify function so simplify.
Okay so let's see how it will work.
So I will call it a fiction.
I will call this function.
Plus everyone.
So let's say numerator and denominator is ten by two then in two.
And this is my friction F1 and the addresses 700.
Okay so at this lane what will happen.
This will contain at best seven hundred.
And when I write numerator is numerator plus Newman a denominator.
So it will become ten plus two and denominator will remain the same which is two and then we are calling
the simplified function.
So if we call the simplified function on twelve by two.
It will become six by 1.
Okay.
Travel code is working fine.
Now let us best have called uh let's first comment out everything.
Now let us first try to print the value of f1.
So I found out print and now we will do being driven to plus plus F1 and then we will again print the
value of f1.
OK so let's send our file.
So first we have to run this and now we will run this
so the output produce six by one.
Initially it was 10 by two.
Okay.
So then by two plus 1 it will become twelve by two.
And then the simplify function.
So our output is six by one.
Okay.
So our code is working fine.
Now let's analyze it a bit more.
Okay.
So what will happen if I write if I write in day is five.
Then can I write into J is plus plus I.
Okay.
And then if I tried to print the value of i n g c I would die and c LG.
So what will our output our output will be six and 6.
So at this line which will happen I will get a memory block.
And it contains the value 5.
Then we are doing plus plus 8.
So it will become 6 and then we are creating a memory block J.
So the 6 value will be will get copied here and then if we print the value of I enjoy.
So 6 and 6 will be our output.
Okay.
So currently can we do like we have all audited the plus operator for a fraction class go.
So can we do this function F two is plus plus F one cannot do this okay.
So let's try to understand whether we can do this or not
so let's come on down this code.
So what I want is fiction F3 which is plus plus everyone.
Okay.
So can we do this.
And after doing it for nought point let's also print F3 okay.
So now let's send this file.
So we are getting an error.
This line and that is let's see that it so that it is the return type of the function operator plus
plus is void.
And on the left hand side we have written a fraction.
Okay.
So this is wrong.
So if you cant see the return type of the function operator plus blesses void and what I would rank
do here is this function is not attending anything and in the left hand side we have written object
of type function.
Okay.
So obviously this will throw us at it okay so what I want to do is I want to make this thing work.
Okay so what will happen.
I will write code in such a way that first F when we get updated and then the value of f1 will get copied
in F3 Okay so what we want is so when we ride in the G is less plus a so.
If I was initially five So this is I.
And it contains a value five.
So what will happen.
First I will get a bullet so it will become six then a memory block.
Jay will be created and then the value 6 will get copied.
Okay so I will do the same.
First I will update the function F1 so the function F1 if it was initially 10 and denominator was 2.
So first the friction F1 will get updated so it will become 6 by 1 and then we will copy the value and
2 friction F3.
Okay so the return type of the plus plus operator plus both function has to be a fraction.
Okay so let's see.
So initially with hard return type will be void but now we know that it and will be affection.
Okay so then babies friction.
Now after updating the current friction we have to return this friction.
So return this.
Return.
This is this line.
Correct.
Should we return this or should we return something else.
So let's see.
So as you look carefully when I write in the J is blessed plus a.
So what I'm returning.
So the value of i really initially was 5.
Now it becomes 6.
So I am returning 6 I am returning the content.
Okay.
Now let's say the friction F1 the friction F1 has memory address 7 700.
So this contains the operator plus plus this contains an address so intended and the numerator and denominator
gets updated.
Initially it was trained by two.
So now it's become 6 by 1.
Okay.
So I have what I'm doing here is I am returning this.
This contains the address this contains 700 but we do not have to return 7 and we have to return the
content we have to return the content.
Okay so for returning the content what we will do we will then start this.
Okay.
We have two right return stop this because we have to return the content.
Okay so we have to right start this because we want to down the content.
Okay so let's end this file and now I believe there should be no error at this line.
Okay.
So I am printing the will of F1 and F3 both will be same.
Okay so let us try to run our code
so we can see initially the value of friction F1 is ten by two then we are printing the value of f1
after doing plus plus F1.
And this is the value of action F3.
Okay so our code is working fine.
Okay.
So now let's try to analyse one more thing.
So now we will analyse one more thing.
Okay.
So what happened is when I write and I know if I do plus plus plus plus I and if I tried to print the
value of i let's say they initially initially the value of i was 5 then c out a bit will you add output.
So an output will be 7 by 7 because at this line I will get a memory block i.e. this contains the value
5 then first this operator will get work this operator will work.
So it becomes six then again plus plus.
So it becomes seven.
So it output is seven.
Okay.
So can we do that same thing on fiction.
So what I want to do is the list called work plus plus plus plus F1.
So will it work.
So first plus plus we'll work on F1 and we know this function returns a fraction desolate and affection
and then this plus plus will work on distraction.
So ideally it should work.
Okay.
I later work.
So let's try.
Whether it will work or not.
Okay
so let's comment out of the thing
so what we want is we want nested.
So we are doing we are using nested operators we are doing nesting here.
So plus plus plus plus F1.
So will it look.
So after doing this let us try to print the value of f1 and let's see whether it will work or not.
So we have found out print Okay and before doing this nesting thing we also print the value of f1.
So F1 not print
OK so what should be our output.
So initially the numerator and denominator contains 10 by two.
So this is my reflection F1.
So first I will do plus press F1.
So it will become do by two then simply have when got cold.
So it becomes six by one then we are again calling plus plus on six by one so six by one plus one.
So output should be seven by one.
Okay so this is the expected output.
This is expected output.
So let's see what will add output
so output is coming out to be 6 by 1.
Okay.
So this nesting thing is not working.
Output is still coming out to be 6 by 1 whereas the expected output was 7 by 1.
That means there is some problem in our understanding.
Okay so there is something which is going wrong.
Okay so far before analyzing it what went wrong.
Let us try to move one more thing.
Okay so what we will do
let's try to write in this function so a function of three is plus plus plus plus F1 okay.
And now let us spend the value of a finite after you.
So if one dot Brent and F two dark and
okay so what should be our output
so as we see this value if we died to try to print this value it was coming out to be 6 by 1.
So ideally what will happen both will be six by one F1 and F3 both will be 6 by 1.
Okay.
So what will happen actually.
F1 will be 6 by 1 and F3 will become 7 by 1.
Okay.
So these two values will be different if one is 6 by 1 F3 is 7 by 1.
Let's see.
See I told you in F1 is 6 by 1 and have to use 7 by 1.
Okay so why this is weird.
So why this weird behaviour is coming out.
So let us try to understand what happened.
Okay
so what happened is when we write into AI is 5.
So what will happen.
First of all there is a system before.
These are temporary memory.
This is a temporary memory system before it will contain the value 5.
Okay then we will get memory allocated to variability.
Okay.
Then very well I will be created and this 5 value will be copied and then this 5 value will be copied
and then this memory will get deleted.
Okay so this is how it works let us try to understand it a bit more so suppose I have a function let's
see I have a function and fun this function takes an integer variable as input then it will do a plus
plus.
And then it does it.
DUNNING I
and let's say this is our main program and we are calling the function from here.
So we are doing Entergy and we are calling the function.
I and let's say the value of IEEE is five.
And then we are trying to print the value of G.
Howard will work so when we call the function find five.
So what will happen.
This I will contain five.
Okay so this I contains five.
Then we do a plus plus.
So it will become six and then we are returning a.
Okay.
So when we return what will happen.
So when we come here what will happen.
Our system memory again before we will created that buffer will contain the value 6.
Okay.
Similarly like this.
What will happen.
A system buffer system buffer which is a temporary memory will be created.
And it contains the value 6.
And then what will happen in the G so a box g a variable g the variable JS allocated memory and then
the value 6 will get copied here and then the value 6 will get copied here and then it will see algae
then 6 or liberated.
Okay so this system buffer this temporary memory is created by the computer.
Okay.
So we are not doing anything this is created by the compiler or we can say the system.
Okay so what will happen instead of writing this line.
If I try to directly print if I will call the function using this using this line.
What will happen.
So what will happen this function.
We will reach here again.
I will contain 5.
So then it will become 6 and then we are returning a.
So when we are returning the value what will happen again.
We will get a system before the system before.
Let's call it x.
So the system buffers contain the value 6.
And now here we are not receiving it.
Okay.
Now we are not receiving it.
So what the old will do.
It will go to the system before and it will print this value.
So 6 will get printed.
Okay so c out will go to system before and it will read its value and it will print its value.
So this way 6 will get printed Okay so we are not receiving anything here.
We are not receiving anything.
We are directly printing.
Okay so for direct printing words the order will do.
It will reach the system before it will reach the temporary memory and then it will print its value.
Okay.
So now let us see the same thing which is happening here.
Okay.
So how this will work.
What is the edit here and how we can correct this error.
So what is happening here is.
So let's say our numerator is 10 and their denominator is 2.
So this is our function F1 and let's say that at this is 700 and this is 700.
So we are calling.
So what we are doing here is we are writing something like this.
So friction F3 is s plus plus plus F1.
Okay so first of all we will operate on first plus plus.
So what will happen.
We will reach the function operator plus plus.
So when we reach the function of ridiculous plus this will contain an address seven hundred and then
the value of the numerator will get updated.
So numerator will become six and the denominator will become one.
So this is after the simplified function.
And then if you see the code we are turning this friction.
So this is currently at the 700 we Dunning distraction and I told you when we re done the friction what
will happen it will go into the system before.
Okay.
So we are returning a fraction but we are not receiving it.
So we are returning a fraction.
I will reach the mean.
Then what will happen.
A system buffet will be created.
It contains numerator is 6 and denominator is 1.
So when we reach the mean.
So the output of this is this one.
So when we reach the mean.
This is our temporary memory or we can say the system before.
Let us call it X and let's say it s addresses 800.
Okay then what we are doing here is plus plus since this function is returning something.
But we are not receiving it.
So what this plus plus will do.
It will operate on this instead of operating on this one my blessed place will operate on this on the
temporary memory.
So what it will do now this time might this will convene address 800 and again the numerator will become
numerator plus denominator so numerator and denominator will get updated.
So it will become 7 and it will become when after the simplified function.
Okay.
So this is after this simplified function and then we will return this friction so we will return this
friction to mean and when we are returning what will happen first of all a system memory or temporary
memory will be created.
So that temporary memory contains numerator is 7 and the denominator is 1.
Let's say the name of this temporary memory is right.
And let's say it's addresses 900.
Okay.
So after this what will happen a fraction EF 3 will be created a fraction EF 3 will be allocated to
memory numerator and denominator.
And then this value will get copied so numerator contains 7 and the denominator condensed 1.
So when we do EF 1 dark print what will happen and when we do EF 2 dark print.
So when we do EF underpinned so this is our F1 so it contains 6 by 1 and this is our EF 3 so it contains
7 by 1.
Okay so that is how intimately this is working.
Okay.
So that is how internally this is working.
So how can we like how can we correct this method.
Okay so what is happening it is the main problem is this thing this blessed place is getting operated
on this system memory this temporary memory.
Okay.
And we want this plus plus short book on this address seven and eight only.
Okay so we want this memory this temporary memory should not be created we want this temporary memory
should not be created.
Okay.
Let us try to understand a bit more so what is happening it is initially the value of numerator Houston
and then due to my dates to so we are doing plus plus plus plus F1.
So first of all this plus plus will work on this one and its value will become 6 by 1 Okay.
Then what will happen are temporary memory when we return the friction are temporary memory will be
created a system before will be created so when we write return start this and when we reach the mean
program a system memory will be created let's say it let's say the name is X and it contains the absolute
hundred.
So this time this plus plus is working on this one and we want this plus plus should work here only.
Okay so what we can do is we will write code in such a way that this copy is not created.
So if this copy is not good what will happen.
My system memory my system before it will point here only.
Okay so cannot live in the memories created the system before it points here.
But if the system buffer is not created the copy is not created and the system buffer will point here
only.
Okay so before I will point here only and then this plus plus will bug on again work on this.
And then this value will become 7 by 1.
Okay.
So what we want.
We want copy should not be created copy should not be there.
Okay.
So whenever we want that copy should not be created.
What we do.
There is something called the reference.
So we know passed by reference.
We have to read about pass by reference.
There is also with something.
There is also which is called a return by reference
Okay so we know what is passed by reference.
There is something called written by Defense.
So reference is used whenever we don't want to create a copy.
So we will return the friction biodefense.
So when we're done the function by defense the copy will not be created.
Okay so the copy will not be created.
So let's see one more time when we pass by value.
So when we pass when we pass biodefense what happen is let's say this is function it contains an AI
and there then type is in danger.
So there is some code here and let's say I'm calling from the mean.
And let's say this is I have and geez one and I am passing G.
So what will happen.
This is biodefense.
So I N G boathouse him the boat contains one so the new copy will not be created.
Okay J.
Points towards this.
And I also points toward this.
So whenever we re done by defense if it will done by reference then then we will reach the mean a temporary
memory will not be created.
Okay so their temporary memory will point towards the memory which is inside this function.
Okay so we have to pass by it a fence here what we have to do we have to pass by a reference okay.
So now let's send this file and now when we'll run this file in front of the empty bottle you contain
the value 7 by 1
so see this time both contains the value 7 by 1.
So now our code is working.
Okay.
So let us try to understand one last time so we are doing plus plus plus plus F1 okay.
So let's say the address of F1 is 700 so numerator and denominator so that address is seven hundred
and the values ten by two.
So this will contain
700 so it will become to by 2.
After calling the simplified function it will become 6 by 1 then we are returning biodefense since we
are returning biodefense the system before it will point towards here it will not create a separate
memory system before it will point towards this only.
Okay then we are agreeing calling plus plus.
So this time this plus place will get called land system buffer.
So this plus place will get called land system buffer.
Now system the forest pointing towards this.
So ultimately this plus place will be called on this only.
So now the value will become so.
This contains this again contains 700 now the value was 6 by 1 Okay so this is 700.
So this contains 700.
Now it will become 7 by 1.
After calling after the addition and calling the simplify function then we will then we will done by
biodefense since we are attending biodefense.
So the system before will point towards this and then this value will get copied into fiction entry.
Okay so F3 and F1 both contains the same value.
Okay so that inbuilt plus plus the inbuilt plus plus that being element operator works like this only.
Okay then build operator works like this only.
So initially what was happening but was our mistake.
So this plus plus we know this plus this will work on the system before.
This is for sure.
So initially I only what was happening this system before it was pointing towards a copy.
Our temporary memory visual which was containing the value six by one but this damage to our bus was
different.
Let's say 800.
So I deleted the system before it was pointing towards a copy.
Okay.
And plus plus was working on system before and system the firewall is pointing towards this.
So that's why this value will become 7 by 1.
And this will literally get copied into F3 and F1 remains 6 by 1.
Now what we did is we passed my defense so this copy is not created and the system buffer will point
towards 6 by 1 only at the 7 and 8 only.
And plus this will work on this 700.
Okay.
So that's how this work.
Okay so they invoked incremental data works like this only inheritance biodefense.
Okay so in this way we can do as many levels of nesting as we want and it will work properly.
Okay.
Now the lasting can we make this function a constraint function.
So what is our definition of a constraint function.
A constant function is the one which will not change the properties of the current object since it is
changing the numerator.
So this function does not qualify for a constraint function.
Okay so in this way you'll be all loaded up with increment operator and like this was a little bit complicated.
Okay so what did a little complication is always fine.
So I hope you understood this we.
Okay.
Thank you.