Hello everyone.
So in this video we will learn about operator overloading operator overloading means that we can extend
the functionality of the existing operators such that it will work for our user defined classes also.
So in the last session we created this friction class.
Now what I want is.
So our aim is very simple.
What we want is EF 3 is EF 1 plus EF 2.
I want to do something like this.
So currently if we will ride this treatment then this treatment is wrong.
Why.
Because the plus operator is not defined for our affection class but if I will write in C is A plus
B.
Then this line is right.
Why.
Because this plus operator is defined internally for doubles and for integers.
Okay.
So this line is wrong.
Why.
Because the plus operator is not defined for our fictional class.
Salary is very simple.
We wanted to make this line book.
We wanted to make this treatment work.
Okay.
So what we want is we want to extend the functionality of this plus operator.
We want to extend the functionality of this plus operator so that this press operator can work for our
function class.
Now this process is called operator overloading.
Okay.
So now we have to learn how to overload operator.
So let us see this added function.
So how we would call this added function we will call it with the help of an object.
So if one dot Ed and then the argument will pass affliction F2.
So what it will do it will add two numbers.
One it will add two fictions f1 plus F2 and it will store that is our then F1.
Okay.
So this is what this code will do.
Now let us first change the functionality of this add function.
So what this area functions will do.
It will add two fractions f1 plus F2 and it will stored the desert in New Fiction.
It will create the new function and it will return new friction.
So how we will call this function friction F3 we will call this add function f dot add and it will big
F to his argument.
Okay so this is our aim currently.
Let us change let's first change the functionality of the add function.
Okay so what desired function will do it will create a new function F3 and it will return the new friction
F1 and F2 both will remain unchanged.
Okay.
So how can we do this.
So let's see.
So first of all let us copy this function okay.
So let's make change in this function only.
So what will the return type.
Now the return type will be a fraction because we will done a new fraction okay.
So currently what we are doing here is I am changing the numerator and denominator affection F1.
So let's come on down this called.
So what we have to do we have to create a new friction and then we will return that friction so friction
f new let's name it f new.
So what will be the numerator.
So numerator will be and new him and the denominator will be LC.
Okay.
So what will happen at this line Batam at base constructively be called and now object will be created.
Okay.
So Batman tries to construct that called and now our object is ready okay.
So after doing this we have to call simplify function.
So currently if so currently the simplify means this it'll simplify.
So this simplify function is being called on F1 so we have to change it.
What we will do we will write f new DOT.
Simplify.
Okay so the new objective new.
So the new function if new will get simplified and then we will have done this new friction so redone
done.
F new.
Okay.
So this is all that we have to do.
Okay.
So let's test our code.
So currently we have these two factions friction F1 and friction F2.
Okay so let's make a friction F3
so this friction F3 is the addition of F1 and F2.
So F1 not add and we will give F too okay.
So now let us Sprint F1 F2 150 so F1 dot print
after dark print
and similarly F three dart print.
Now let's turn our program.
So first we will compile this file.
And now we will run this file okay.
So there is something wrong here.
And it was not declared in the scope.
Okay
okay.
So now let us on this file
and now we will run this way
so we can see here the value of have fun and F2 remains unchanged.
And this is how it is and F3 which is the addition of friction F1 and F2.
Okay so now what I want.
So our aim is very simple what I want to do is I will create a new function function f word.
So I want to create a new friction friction effort and I will write F1 plus F2.
And the result will be theme is friction F3.
Okay so I want the same functionality.
So this plus operator will do exactly the same work which the add function needs to do.
Okay.
So the functionality will be same but I want to use plus operator instead of this add function.
Okay so let's see in code how to do this.
Okay.
So let's copy their function
so what I did is I only just copied their function.
Now how to use operator overloading.
So first we have to write the keyword operator.
So operator is the keyword.
And then you have to write the operator which you want to overload.
So we want to overload plus operator.
So without space.
Right.
Plus okay.
So that's all.
You only have to do this much work.
Everything the functionality will remain the same.
You just change the function name to operate a keyword and then plus.
Okay.
So no need to do anything extra so that it's cleared and the friction function effort
to function F4 will be EF 1 plus 2.
So if one plus EF 2.
Okay now let's turn our file.
So first we will run this one.
And now we will win this fight.
OK.
So we forgot to print before.
So let's print a foot F4 dot print now do it's and this will
so we can see here F4 is also five by four solver code is working so we can see here it is very easy
to overload operator.
Now let's see how it is working.
Okay.
So we'll see this line.
What will happen if when dot Ed EF 2 so this EF 1 will go in this this keyword of the add function and
this EF 2 is posties argument
OK.
Now see this line EF 1 plus EF 2.
Now a brand on the left hand side of the operator.
This will go in this function.
This keyword of the dysfunction and F2 will go in the argument okay.
So the left hand side or brand will go in this and the right hand side argument will go in the argument
and which function it will call it will call the function operator plus operator plus function will
be called.
So this will contain F1 and F2 will be biased s argument to this function operator plus function.
Okay so now let us overlord multiply operator also.
So what I want is I will create a new fraction function EF 3 and this friction EF 3 will be the multiplication
of 2 friction F1 and F2.
So we will overload this much space operator.
So this EF 1 will go in this and EF 2 will be passed is argument similar to this similar to our function.
Okay.
We do not need to parse two arguments we just need to pass one argument because the better one will
go in this.
Okay so let this overlord multiply operator
so this is want to pay operator so what we let it down type it and type will be fraction because we
will create a new friction and then we will return that friction so we don't have really friction then
the keyword.
Operator And then the operator which we want to overload.
So we want to multiply operator so multiply what it will take it will take a function as input function
F2 and then what we will do we will calculate the numerator and denominator so in the numerator which
is f one numerator or we can write this at a numerator multiply the numerator of f one Saudi multiple
numerator of F2.
So after nought numerator similarly it describes with their denominator so denominator is.
Do I mean it drove F1 or we can write this letter denominator multiply by the denominator of F2.
Okay so now let us create new friction which we will have done.
So friction let's name it f new so the numerator will be N and their denominator is d again copy constructor
sorry again parameters construct that will be called here and then we have to call simplify function.
Okay.
So the functionality will remain the same.
We are falling this only.
Okay so the function level doing the same.
Now we have to call simplify function.
We have to call simplify function on the new object.
F new.
So we have to write f new DOT.
Simplify if you will simply write simplify then that means this to simplify simplify function we got
called on F1 but we have to call on f new so after simplifying we will have done f new.
Okay so that's all we have to do.
So what will happen
when we will write fiction F3 is F1 multiply F2.
So the left hand open and the first print will go in this.
So this numerator means F one dart numerator what we can write this at a numerator and this F2 will
go in the argument okay.
So this is how we overload multiply or pretend so this is how we overload multiply Roberta.
Now let us try to overload one more operator which is equal to equal to so I know what we want is I
want to overload this operator equal equal operator.
So what this equal equal operator will do.
I will give two fractions F1 and F2 and if the two factions are equal I will print equal if the two
factions are not equal then I will print not equal.
So in the main function in the main program the syntax will be if friction F1 equals equals friction
F2 then we will see out equal otherwise they are not equal.
So in the S and the else part we will write not equal okay.
So where do the return type.
Okay so first of all this thing will go into this function.
This key word of the function and F F2 will go in the argument
and which function we will call we will call the function with the name operator equal equal.
Okay so let's say in code.
So first of all what will the return type return type will be bool because we will return true or false.
So bool what will the name first of all we have the right operator keyword then the name of the operator
which we want to overload.
So we want to hold equal call operator.
So it will take one function as input function F two because the first function will go in this and
then we have to return either true or false.
So render two functions are equal when the numerator and denominator both Harry Kewell then we can say
the functions are equal Okay so numerator and denominator both will be equal then we will say the two
factions are equal so when numerator of f one consequence numerator of f F2
and denominator has to be equal so denominator of f one equals equals denominator of F.
So afterward denominator OK.
So so this condition will either be true or this condition will either be false.
So accordingly we will return true or false.
Okay so that's all.
Now let's end this way okay.
So there is something wrong
okay.
So there's a spelling mistake.
Now this is right.
Let's compile the file okay.
So now we will test our multiplying operator.
And now what.
Equal equal operator.
Okay so we have to test two things so let's make let's create a friction friction a five which is the
multiplication of F1 friction and F2 friction.
Okay.
And F five.
BROADBENT
And now we have the best outward equal equal or greater.
So if F1 equals equals F2
C out equal
it's not equally
okay.
So let's all let's say now fighting.
So this is our safety and this is I want a full.
And this is our EF 5 so EF 5 is no duplication of a friend of 2.
So I hope Don said is right.
And then and finally off to an article.
So that's why our output is not equal.
So if I write F when equal equal F1 then that would put will be equal.
Okay so let's see.
So this time output is equal because F1 is different.
Both sides seem okay.
So that's how V O Lord our operator.
So our work is not done here.
Our work is not done here yet because we have to do some optimization.
Okay.
So what kind of optimization.
So if you remember this add function this add function is taking F2 by reference and it will took constant
reference.
So first of all biodefense why we need to biodefense.
So to avoid calling copy construct but because we do not need to call copy constructor and write constant.
So we need a constraint to avoid illegal changes in F too.
Okay.
This is our plus operator.
Okay.
So here we have to read and optimization question defense.
Now can we do optimization so this is multiply.
And operator multiply.
So here we should pass by reference.
Okay.
Constant reference because we do not need to call copy constructor here and also we want to avoid illegal
changes.
Okay.
So that's why constant offense.
And similarly we want to avoid calling copy constructor and we want to avoid any legal changes so question
reference.
Okay so no one more thing.
Now we have now we know about constraint functions also.
Okay.
So what that constraint functions constraint functions are does function that will not change the property
of the current object.
Okay.
That will not change the property of the current object.
So let's say this function equal equal.
So will this function change the property of f 1.
Okay.
So no it is not changing the property.
So this function is a constraint function.
So Mark this function is a constraint function.
Okay.
Now let's say this function and multiply function.
So this multiply function changes the property of the current object.
So the current object is F1 no object which is calling the function.
So numerator and denominator.
No it is not changing.
It is creating a new friction.
Okay.
So this function is qualified to be a constraint function.
Now let's see this multiply function.
So this multiplied function is changing the numerator and denominator of the current object.
So this multiply function is not a constant function.
Let's see the plus function the operator plus function.
So this function is creating a new function and it is changing the properties of the new fractions.
It is not changing the properties of the current object.
Okay.
We have commented it out.
It is not changing the properties of the current object since it is not changing the properties of the
current object.
This is also a constant function and similarly this is also not changing the properties.
So this is also a constraint function
so everything is working fine.
Okay.
So in this video we only ordered three operators plus multiply and equally call operator in the next
session we will overload somewhat operators.
Okay.
Thank you.