Hello everyone.
So in this video I will explain you a question that you guys need to implement.
Basically I want you guys to implement a Polynomial class.
OK so you have to implement the polynomial now.
So what is a polynomial.
So in maths the example of a pilot may lose for example three X squared plus four X cube for X to the
power five minus let's say extra the power seven.
So this is the example of a polynomial limit.
So a polynomial has two values.
So one is the coefficient so when is convenient and the second one is third degree.
Okay.
So in class in the upper limit class we will store two things degrees and the coefficient.
So this is the coefficient and this is the degree.
This is the coefficient and this is the degree.
So what we will do in the polemic class we will take.
We will maintain an 80.
Let's say the name of that is the coefficient.
So how we will store for example if we want to store this polynomial so how we will store.
Okay.
So the maximum degree is seven.
So it will do let's say we have an array of say seven
so let's do t for five six and seven.
Okay.
Just a random size you can take the size of the error to be five and or ten thousand.
Okay.
So the size of that is like you can take anything.
So let's say the size there is seven.
So how we will store this polynomial.
So what we will do for storing so go to index to and store three.
So will the index to install three go to index five.
And store food.
So index five will store food go to index 7 and store 1.
Sorry minus one.
So index 7 it will store minus 1.
The rest of the values will be 0 0 0 0 0 0.
OK.
So this is how we will store a polynomial.
Okay so what we'll use we will use indexes as our degrees and the values we will use them as our coefficient.
Okay so this will be our area.
So in this way we will store our polynomial.
Okay.
And we will take one more data member.
So just like the dynamic at glass we will keep a capacity also.
So in this case the capacity of our arrays it okay the size of that is it.
So in this way we will restore a polynomial.
Okay.
So let's discuss the function that you guys need to implement.
Okay so you guys need to implement some basic basic functions so functions that you guys need to implement
so the first function is default custom built again so first I'm writing the name of all the functions
that you guys need to implement.
So default then copy constructor
and copy assignment operator
Okay so what are default constructively to so we have to do the members as I work private field.
So it will initialize the degree coefficient dairy and it will initialize our capacity let's say if
you want the size to be 10 so that capacity will become 10 and degree of change will be new and then.
Okay so our work is not finished here we have to do one more thing here.
What we have to do we have to put zeros everywhere in the area we have to initialize this degree coefficient
array with all the zeros.
Okay so we have to initialized this degree of shown that if it all the zeros you can see here above.
So 0 0 0.
So initially this array initially this area will contain zero only.
Okay then what do we do.
We will read the polynomial and then we will feel the values.
Okay.
So initially that it contained zeros only okay.
So you have to do two things inside the default constructor first initialize the degree coefficient
that in the capacity area and then put all the zeros put zeros and the degree coefficient Daddy.
Okay so now let us talk about copy constructor.
Let us take an example so suppose my polynomial even looks something like this
let's head to one zero minus 2 0 so 0 1 2 3 4 OK.
So what does this polynomial.
How can we get this polynomial.
So three plus X minus two.
Excuse me.
Okay.
So this is our polynomial so if I write if I'm creating another polynomial polynomial b2 b1.
So what will happen.
Copy constructor will get called.
So essentially no copy constructor creates shallow copy.
So this bill 2.0 that will point towards this area only.
Okay so this is wrong.
So inside the copy constructor we will what we will do we will create deep copy.
We will create deep copy okay.
Now let us talk about copy assignment operator.
So if you will write B2 equals P1 what will happen.
This will do will point towards this area only so we know copy assignment operator also creates shallow
copy.
Okay.
So inside the copy assignment operator we will all audio operator equal.
Operator we will overlord and we will create deep copy exactly the same function as the copy constructor.
Okay.
So after creating these three function let us talk about some more function that you have to implement.
So brain function.
So how you will bring the polynomial that you can see in the equation.
I will explain in the question that in which format you have to like you have to print the polynomial.
Okay.
So after the brain function we we have to overload some operators which operator we will all load plus
operator minus operator and multiply operator.
Okay.
So four plus operator we will write something like this.
Tres B1 plus B2 okay.
So this will go in this it will going argument and the return type will be a polynomial.
So how do I do.
Well normally so let's say the problem will be when is just like we used to do in maths.
So for X minus two weeks the para five and we have another polynomial to so minus X plus let's say x
squared plus X cube plus extra the powerful.
Okay so B when plus Bill 2 What will happen if the degrees are c you can add them so simply three then
minus X then degrees the same.
So you can add them you can add the coefficient.
So 5 x squared plus X cube plus extra the powerful and minus two extra the power five.
Okay.
So this will be given plus B2 which is B3 man what do you have to do.
You have to like update this in the degree coefficient area off you have to update the degree coefficient
area of P3.
Okay so update the beginning of that of B3 accordingly and the same is the case.
The same is the case for negative subtract.
So be 3s B when minus B two.
I hope you know how to subtract two but let me leave their degrees I assume you can separate the confident
and for multiplication we have to do similar kind of stuff we will write B threes Bill and multiply
b do so the multiplication will be a little bit tough it will be like a little bit tricky.
So first like you can do it on pen and paper you can analyse how the things are working and then I will
suggest you to write the code.
Okay so this will be like a little more complex and a little bit complex.
And now let us talk about the last function that you have to create.
So we have to create.
We have to implement a set coefficient function.
So what this set conversion function really do.
So this set coefficient function.
Okay.
So how we will write this so even Dart said coefficient so this search coefficient function will take
two things as input.
First one is the degree and the second one is the coefficient.
So let's say B one is let's say the capacity is 6 0 1 2 3 4 and 5 and let's see the values are 1 0 minus
2 again 0 1 and 0.
Okay so we will call the function b one dart said coefficient and we will give degree and coefficient
as input Okay so let's say the degrees 3 and the coefficient is 5.
So what do we do.
Go to index tree and make the coefficient 5 and let's say the values are degrees 4 and the coefficients
then so go to index for make the coefficient and override the coefficient.
Okay we have to override the coefficient.
Now the problem will come here is let's say this size the capacity is 6.
So the valid degrees are 0 to 5.
Only okay if the capacity receives the value degrees of zero to five.
And now if I do degrees 7 and let's say the coefficient is 20 so degrees 7 is not a valid index.
Okay so what.
So what is happening here is degree is greater than the capacity okay.
Degrees good in the capacity.
So in this case what we will do we will double the size of the array we work double the size of the
area and then we will again check.
We will check if their degree is less than the capacity then it's okay.
Otherwise in the edit part we will again double the size of the 80 we will again double the size and
we will again check.
Then again double again check this will continue.
Okay daily until we reach this condition.
So in these two cases the degree is less than the capacity.
So this is fine but in this case the degrees are good at any capacity.
So just like in the dynamic at a class we will double the size.
But here after doubling the size you have to continuously check if a degree is less than the capacity.
Okay.
For example.
So initially the size is six.
So let's say my value is 14.
Let's say the value of degrees is 14.
So I will double the size the size will become cool then I will check to L is less than 14 so I will
again double the size.
So 24.
Now 14 is less than 24.
So now we can override the values.
We can override the coefficient.
Okay so.
And so these are all functions that you have to implement.
Okay so let's see where the less structure have a code.
So we have a class polynomial so inside the class polynomial we discussed that there will be two private
properties first the dynamic Gary.
So let's say the name is beginning coefficient and the second is the capacity.
Okay.
So these are the two private properties.
And now what we have to do we have to create some functions so the first function is default constructor
inside default constructor.
We have to do two things we have to initialize negative coefficient initialize capacity and put zeros
in the beginning coefficient Eddie then we need copy constructor why we need a copy constructor because
we want to create deep copy then we need to copy assignment operator we have to overload copy assignment
operator just like we did in the rich class in the dynamic edit class.
Okay so copy assignment operator you have to overload so after that you have to right print function
which would be very easy see the question to see what is the brand format and then you have to set competent
function that will take two arguments.
Sorry.
Yes it will take two arguments degree and coefficient then you have to overload some operator so plus
operator you have to overload negative operator you have to overload and then multiply operator you
have to overload.
So this will be our structure okay.
So these are all functions you have to implement.
Okay so I hope you understood this question.
Good luck.
Thank you.