Hello everyone.
So in the last session we created a friction class and now we will discuss some important concepts that
were not covered in the last session.
Okay so first let us analyze this friction class.
So in this friction glass we have two private properties numerator and denominator.
Then we have a better match.
I used constructor a brain function and I'll add a function.
Okay.
And then we created simplify a function at the last.
Okay.
So this is all about the function glass okay.
Now let's see.
Now let's analyze the function and what is happening in that function
so we have two fractions so the numerator and denominator for first reaction was tending to this is
faction f 1 and numerator ended on the default affection 2 was 15 food and this was our affection to.
So how we are calling this added function f one dot at and b bus F2 US barometer.
Okay.
So what is happening here is we are making a copy of F2 head.
Okay.
So a copy is being generated.
So what is happening here is we can we can write this line is fraction F2 is it to fraction F2 from
the mean.
Okay.
So this barometer reflection F2 disparagement affection F2 is equals to the fraction from the mean.
So what is happening here is we are creating we are creating a new object and all the values are being
getting copied.
Okay.
So what is happening here is copy construct Baz being getting called copy construct that is correlated.
Okay.
So at Line number 17 at line number 17 copy constructor is being getting called a copy constructor is
creating an object and then it copies the value from the fraction which is present in mean okay.
So what our aim is.
So our aim is our aim is very simple we want to avoid the copy we don't want to copy okay.
We want to avoid this copy.
So why do we want to avoid this copy.
Because if you look in the if you look inside the added function we are only accessing the properties
of fraction F2.
We are only using the properties of friction F2.
So why do copy if we will copy then there is time in memory waste because first and object will be created
then all the values will be copied.
Okay but we only need to access its properties.
So there is no need to copy.
So ultimately our aim is we have to avoid this copy.
Now how can we avoid this copy is the question.
So how can we avoid this copy.
So we all know we have started about our offense is variable and passed by it offense in function.
Okay so what we will do we will pass by reference to avoid copy.
So let us take an example.
So when we write in days 5 and into G is I.
What happened at this line.
This is the scenario and at this line a memory block will be created G.
And the value will be copied but we want new blocks should not be there so we will use a reference variable.
So what will the effect of this line the effect of this line is is very simple K okay.
The boat points to the same memory block I n Cape belongs to the same memory block.
So if I were Lou Gabler surplus then this value will become 6.
Okay so if you do see out okay or if you see out I output will be 6.
Order will be 6.
Both AI and key points to this memory block.
Okay I can change.
I can read and write.
Data can also read and write data so we will use a reference variable.
So this scenario will look something like this faction reference variable is main guard F to okay.
So in this way we will be able to avoid copy.
Okay so let's see.
So what we have to do we will avoid copy using the reference variable.
Now there is still a problem.
What is the problem.
So suppose if I do something like this after dot numerator is minus 1 I am changing the values of F2
I'm changing the numerator property of F2.
So my aim is my aim is to avoid this to avoid this.
No one can change the values of numerator.
No one can change the values of F2.
So how can we do this.
So how can we achieve our goal.
So it's very simple.
What do we do
so when we write in day is five and end at events where DeBakey is I.
What happened is this is I.
And this is key.
I can I can read and write data.
Key can also read and write data.
But there should be a V if I do get plus plus then add I should be dead I want cake and only read data.
Gays should not be able to write data gay can only read data.
So what to do.
So we have a constant reference variable.
Where do we.
This index will look something like this and constant reference variable key is I.
So gays a reference variable to won't be the constant gays at offensive Eddie Baldwin Deja constant
so gay is a constant preference.
So if we will use this then what will happen.
So this is five I am gay.
Both points here.
I can read and write data but gay can only lead you down gay cannot write data.
Okay.
Gay Gay cannot write data.
So we need something like this in our program.
So what do we do.
We will create a constant defense so constant reference Okay so whenever we are working on a project
we should keep all these things in our mind.
Now get these small small things matters.
Okay so you should always think that by that I should pass up constraint or offense or simply pass by
value.
Okay so these things matters.
These things really matters a lot.
Okay now let us create one more function a function called Multiply.
Just like that function we are creating one more function which is called multiply so multiply.
We do.
So multiply function will multiply two fractions and how we will call if one dart multiply F2.
Okay.
We will pass one argument.
So our aim is same as the aim which was for our function.
We will multiply F1 and F2 we will multiply if an F2 end store that is right in F1 okay.
F2 will remain unchanged.
It is true that is worked in F1.
So how to multiply two factions.
Suppose F1 is ten by two and we have another function f 2 which is 15 by 4.
So multiplying numerator with the numerator and multiplying denominator with denominator so 150 by 8.
And then we will call simplify function then we will call simplify function.
Okay simple easy.
Now let's write the code
so the return type will be void.
Let's say the name of the function is multiply it will take a function as input.
We will take a constant reference because we do not want our copy construct to be called here because
there is no need to copy okay.
There is no need to copy is a way to call copy constructor and waste our time in memory so we have to
multiply the numerator with the numerator.
So numerator of F1 is numerator of F1 multiply numerator of F2.
So we have to not numerator.
Okay so using this is optional here.
This is optional
so we have to make the better denominator with debt limit as our denominator of F1 is the number of
F1.
Multiply denominator of F2 and then after multiplication is done we have to call simplify function.
Okay.
After multiplication we have to do we have to call simplify function now after degradation let's multiply
a finite F2 so F1 God multiply.
We have to pass off to his argument and now let us spend the value of f1 enough to so F1 not print F1
dart print and similarly large print F2.
So after dot print OK so that's all
so these are the values of our finite F2 after the reason and these are the values of F1 I have two
of them multiplication so we can see here the value of F2 remains unchanged.
And when these two are multiplied we will get this result five twenty five by sixteen.
Okay so go to this code and try to understand each and every line of the code so that's all from my
side.
Thank you.