Hello everyone.
So in the last session we learned about operator overloading and the overloaded manual but it does for
our fiction glass.
Now we want to create that last dynamic Gary.
So in this session what we will do we will create a class dynamic Gary and in that dynamic class.
But other functionalities.
So we want first of all we will take R and D ready.
And the function that we want is user can add as much number of elements as he wants.
Okay.
So the problem with the normal arrays we have to tell the size in front.
Okay.
We have to tell the size before then we declare our area of size five.
The size will be five.
The size can be 10 or the size will be N.
Okay.
We cannot extend the size on for example if we have an area of size 10.
So if we have an area of size 10 so this is index 9.
We have an idea of size 10.
So we cannot insert the eleventh element user cannot insert the eleventh element of the size of barest.
And so we will solve this problem by building our own class.
Okay.
So what we will do.
How can we solve this problem.
So our logic is internally what we will do.
So internally we will maintain energy.
Let's say the size of that is initially five.
So zero one two three and four.
So let's say that initially the size of that is five.
So what we will do as soon as the user will enter the sixth element as soon as the user will try to
enter the sixth element what we will do we will make a new every so this area will be double.
So the size was initially five.
Now we will create an array of size 10 and we will copy the data.
For example it was 10 20 30 40 and 50.
So we will create a new area.
Will you copy that data.
So 10 20 30 40 and 50 and now the user can insert the sixth element here.
Okay so use I will insert the sixth element here and now this Eddie.
This will point towards this.
Okay.
And we will D allocate the memory as soon as our array gets filled up.
What we will do we will create a new every copy day content from the already the allocated the already.
And now that it will point towards this.
Similarly if this at every Goldfield if the size if all the 10 elements are in that it will do look
we will create an area of size 20 we will copy the older 10 data fields or 10 data and then the new
it will point towards this area of size 20.
Okay.
So in this way we can solve this problem.
Okay.
So what will be our data members.
So what do your data members.
First of all we will need our dynamic Gary.
Okay.
So in Star data.
So this is our in beta I didn't need your pointer so we initialized decided dynamically in the constructor.
So in the constructor what we will do
in the constructor we will initialize this dynamic very let's say initially I am taking this I used
to be five so new in five.
Okay so this is how we will initialize that.
So what will happen
this is data and it is of size 5 inside the constructor.
Okay.
So what we will do now we will want a function.
So we will create a function add element.
So what this add element function will do.
It will take a element which is to be inserted inside the area.
So where the element will be inserted.
So that element will be inserted at the uh it will be inserted at the next index available for example
let's see this at a condensed 10 and 20.
So that element will be inserted here.
Okay.
So let's say the element is 30 and next element will be inserted here.
Similarly the next element will be inserted here.
Okay.
So next.
So the element will be inserted at the next index available.
So inside this function what we will do.
We will.
So that is the name of the array data and element if it is the first element.
So it will be zero.
If it is the second element.
Then it will be one.
And so on.
Okay.
So how can we know that at which next we have to insert.
Okay.
So how we can decide that we can next we have to insert the element.
So for maintaining this and next we will create one more data member with the name next to Nick's
so I will have one more data member and next to Nick's.
So next 16 points to that index which is empty.
And we can insert it idea idea.
Okay so initially next to index will be zero.
We will insert our first element at index 0.
Okay.
So what we will do we will write data off next index is element and then we will do next the next plus
plus.
Okay so this is how it will work again.
And now we will also take one more data member which is the size of the area okay.
So these are that really the members that we will take.
Okay.
Let us take an example to understand it better
so let us take an example so let's say initially my duty is of size 5.
And initially the arrays empty by empty I mean it contains a guide which value Okay so 0 1 2 3 and 4
maximum Dex will be.
And I am having an extra Nyx and size.
So these are the theory the members initially next and next will be zero.
And the size is five.
Okay.
We will call the function add 10.
Okay.
So 10 will be inserted here and the Nixon next becomes 1 then we will call add 20.
So 20 will be inserted next or next to insert 30 add today.
So 30 will be inserted.
Now then next becomes three add 40.
So this is add function.
Now add 40 so far they will be inserted at index 3.
And now the index becomes four.
So now add 50 so 50 will be inserted.
And now what will happen if I tried to insert one more element.
So if I write add 60.
Sorry this is this it next becomes 5.
So when we write add 60.
So there is not space to insert the element.
And what happened is next to next become equal to the size.
So as soon as the next makes in the size both are equal but they have to do.
We will create our area of size double so this area was of size 4.
Now we will create an area of size 10 and we will copy the 5 fields the 5 data fields and so then 20
30 40 and 50 and now the 60 will be inserted here.
Okay.
And so the next to next will become 6 and the size will get a better 2 10.
Okay.
So what we have to remember we have to remember if the next to next becomes equal to size what we have
to do we have to double Larry.
We have to create a new area of double size and then copy the old content.
Okay.
So now let's see in called we will understand with the help of God no.
So the class biomimicry.
So what I will do I will keep all my properties private.
So let's create our data let's create our Eddie with the name data and we will keep next index.
Next and next is the index at which our element will be inserted.
Initially it will be zero and capacity and capacity is the total size of the area total size of the
area.
Okay so now let's create a constructor so public
so in the name of the constructors we all know it the same as the name of the class.
So dynamic today and now we really initialize our data members.
So first initialize our dynamic array.
So the is its own digit area.
And let's say initially I am keeping the size of data to be 5.
Okay so next to index it will be 0 because the first element will be inserted to 0.
And I'm keeping the capacity that would have that it will be 5.
Okay.
Now let us create a function and function.
So void and it will take our element as input which is to be inserted.
So element we have to insert this element inside our area.
So we have to handle the case when the next size.
So even the next index becomes equal to the capacity of the area.
Now if this is not the case then the code is very simple.
We just have to go to the next or next and then put the element okay.
And what we have to do next or next plus plus.
Okay.
So now let's see.
Now let's see how can we handle this case when the next train makes it becomes equal to the capacity.
Okay.
So how can we handle this.
What we have to do
okay so now this is the case when everybody gets food.
So next size soda next or next contains five capacity contains five.
And this is our very so five elements have already been inserted in this area.
And 10 20 30 40 and 50.
Okay.
So this is my dear diary okay.
So now what I want to add I want to add a new element at 60.
Okay so the next X equals 2 the capacity.
So what I have to do I will create a new area of double size.
So this is a new area of double size so zero one two three four.
And it will go up to nine in next nine.
Okay.
So after creating the new idea let's name it new data so this is new layer to what we have to do.
We will copy the old content you have to copy these old content.
So 10 20 30 40 and 50.
So after copying the old content what we will do.
So we will look at this memory.
So since it's a dynamic memory we have to process this memory.
This area is created in the heat memory.
We have to delete it.
Okay.
So it is our responsibility.
So where delete this memory and this data will now points towards this area.
And now we will add the element here.
So 60 will be added here and the value of capacity is now 10 and the next index will become 6.
Okay.
So in this weird playbook.
Okay so let's write the code for this what we have to do we have to create a new array of double size.
So US neighbour New data the size will be double.
So new end to times of the current capacity okay.
So after creating the new idea what we have to do copy the old content.
So for copying the old content so I listen capacity I plus plus
so new data if I is your Delphi so the second mark is also done.
We have copied the what we have to do we have to delete the old lady Barry which was initially of size
five.
Okay so we have to delete that diary because it's our dynamic memory.
Okay.
We have to delete it.
It's a heap memory.
So we have to delete the world data and now that all data it will point towards the new data the newly
created Eddie and we have to double our capacity so capacity will also get better so capacity equals
capacity multiplied by two.
Okay so what will happen
so we are deleting the data.
So what will happen if we not glad that you're done.
It is called memory leak.
I think I've already told you before.
What does memory leak.
So now Daredevil is pointing towards an eddy which was of size five and then we created a new data so
this new data this was area of size 10.
So what I did I deleted this memory and then the new data points toward the data the old data point
towards the new data.
Okay.
So if you will or delete this memory then this phenomena is called memory leak.
Okay.
This is quite a memory leak.
Okay so one more thing by the data is pointing towards this new data.
Why.
Because if you see here the new data the scope of new data it is inside the F block only.
Okay.
So as soon as you reach line number 22 the new data is not like it will not exist anymore.
So we have to like point so that data will point towards the newly created data.
You look at it to Daddy.
Okay.
So what will happen.
The capacity will become twice.
Now we will come out of the if condition and then we will insert the element.
So if we want to insert element 16 so element will be inserted and we will do next to nix plus plus.
Okay so this was very simple.
Now let us create one more function.
So this function is get the element function.
Okay.
So get element function.
But it will take it will take I indexes input so usable so user will ask us what is that data present
at index 2 at next 3.
Okay.
So we are creating a get function.
So inside the good function.
So if the index is valued by value nobody means.
So if the index is good.
Then it goes to zero.
Obviously index cannot be negative.
So if the index is valid index is not negative.
And also the index is less than the next index next NICS means the number of elem inside the 80.
So if the index is less than an extra NICs and then only we can return the data.
Okay so they done data if I now.
Else what do we do.
So in the else part.
That means the index is not valid.
Okay.
So we will return.
We will are done our default value let's say our default values minus 1.
Okay.
So if the index is not valid let's say the index is minus 2.
So minus 2 like it means nothing.
Okay.
So we will return minus 1.
And similarly what is next.
Next next next is the number of elements that has been inserted inside the array.
So let's say then next year next we lose 5.
And I is like say 10.
So obviously we will return minus 1.
Okay.
So what I am saying here is so let's say I created our object of for Dynamic arrays or dynamic at a
given I'm creating an object.
So next the index is 0 initially capacities 5 initially and the data contains the address 7 entered
and this is our array of size 5.
So that does have the first element is 700.
Okay.
So 0 1 2 index 3 and index food.
Okay now let us try to add the elements Okay so add a hundred and ten so ten will be inserted next to
index becomes one at twenty so twenty inserted next.
Next becomes to add 30.
So to be inserted next to next becomes three add 40 so 40 inserted next year next becomes food added
50 so 50 inserted.
Next year next becomes five.
Okay so now if we will add 60.
So what happened is an extra index and the capacity they become equal again.
Very simple process.
Create a new every.
Now the size of this area will be the oil.
It will be 10.
So the capacity gourd separated to 10 copy the oil content.
So 10 20 30 40 50.
So next and next will become 6 and the element 60 will be inserted.
And let's say the address of this new year times eight under.
So this is new data that does of new data is 800.
So this data will get to be two to 800.
Okay.
Now let's see the good function.
So inside the good function if I write get 3 so index 3 is a positive index and also the index 3 is
less than 6 which is an extra index.
So that means index 3 is valid.
Okay so next 3 is valid.
If I write get 6 this is not valid because because the value of next index is 6.
That means that next element which is to be inserted it will get inserted at index 6.
It is empty.
So this is wrong.
That's why we write this condition.
Similarly if I write get 50 so obviously this is also wrong because we only have currently 6 elements
okay.
So this condition it is very important to write this condition okay.
Now let us create one more function which is the variation of this add function.
Okay so what we will do now this time we want to add an element.
So void Ed but we want to add an element but this time user will give us the index at which he wants
to add element.
So in.
So the meaning of this line is insert the element at index i.
Okay so we have to insert the element and at the next day let us take an example.
So let's say my dear diaries
let's say it is 10 20 30 and so my arrays of size 5 0 1 2 3 and 4.
Okay next indexes 3 and the capacities 5.
Okay.
Next nexus 3 capacity is 5.
Now let's see this function.
So the element is let's say a hundred and user wants to insert at the 2.
So we will go to index 2.
It is today we will make it hundred no problem.
Okay.
This is right index to element hundred.
It is right now let's say that element is 200.
And the indexes 3 so I use 3 which is equal to the next index.
So what will do we will insert 200 here.
No problem.
Okay now let's say the element is 300 and let's say the indexes then.
Okay so index 10 so index ten does not exist.
Okay so if you compare so index 10 is greater than next to index so if this is the condition that means
this is wrong.
Okay so this is right.
If this is what is the condition so I is less than next to nix and this is the condition when I equals
2 next.
Next.
So these two conditions are valid and this is not valid.
Okay so in this case we will not do anything we will not insert the element we will simply return okay.
In this case it will seem better done
so the first case when the element to be inserted at index i.e. is less than an extra next then it is
very easy.
We just have to override the value.
Okay we will we just have to override the current value element.
Now the second case was when the index at which the element to be inserted is it goes to the next index.
So in this case what we have to do we have to handle everything like if the edit gets filled.
We have to double the size copied all that a copy the content of the whole area and everything but for
that to be able to dictate the function add.
Okay so we will simply call the add function.
Okay.
So for this case when the element to be inserted is next to index.
So we simply will call this function.
And so this ad function will get called and this at a function will handle everything.
Okay.
And it is when will come to us when the main index eye is good then adding extra day.
So in this case we will not insert the element.
We will simply return.
Okay.
You can write a print statement that is the index is not valid or something like that or you can throw
at it.
Okay so you can do whatever you want.
I am simply returning okay.
So now let us also create a print function.
Okay.
So that we can print out any so let us create a function wide print.
So what this print function will do it will it will print the elements of the area.
Okay.
So for we just have to I read nothing much so forint equals zero.
I list an extra index I bless plus and we will print the value of the the content of the edit okay.
Okay so that's all.
So one thing here is I'll list the next index.
This is right or I should write I list in capacity so I should write I'll list an next year next why
next next because next index means these main number of elements has been inserted inside the area.
Okay.
Capacity music total size of the area.
So we have to write next to index.
Okay so now let us try to create some object and let's see how our code is working.
So we have included the file dynamic candidate CBB and the name of this file is dynamic at a used start
CPB.
Okay so let us create our object so dynamic at a Deven and let us add some elements.
So then
so we are inserting six elements.
Okay
so add this line what will happen.
Default constructively called default constructor that we have written that we have created so that
constructively because.
So the size or dynamic array was 5.
But here I am inserting 6 elements.
Okay so what will happen the size of that is 10 the size of the arrays then.
And because the new idea will be created.
Okay so now let us call the function print
also let us create a function.
Get capacity so that I can prove it to you that the size of that is 10.
Okay.
So get capacity function
so let's say I have a function return I will be in danger and the name of the function is.
Capacity.
So what it will do it will retain the capacity of the dynamic at a certain capacity.
OK so the size of the idea was initially five and then we we insert six elements.
So the size will be 10.
Okay.
So here we can see the size.
The size will be 10.
Okay.
So now let's.
In this file okay.
So what we have to do what is the problem
Sammy.
Oh
and also
okay.
So okay we have not printed.
So let's print the capacity.
So see old
deviantart to get capacity and line
so the content of that is 10 20 30 40 50 and 60.
And the size of the arrays then.
Okay.
So our code is working fine.
Okay so now let us create another object
so dynamic Gary V2 given.
So what will happen at this line.
We all know copy concept will be called.
Since we have not written the copy constructor.
So what will happen and will copy constructor will get called.
And now let us change the value of what I'm doing.
I am inserting the element hundred at index 0.
Okay so let me see first so first I am taking element and then index.
So I have to give element first and then I am giving the index.
Okay so now let's print Deven and we will print due to.
So what will happen
so at this language will happen copy constructor will be called and we will not copy constructor what
it does it create shallow copy.
Okay.
So what should be our output.
Let's see first
so let's say when we created when the default construct is called What is the scenario.
Let's say we have inserted only four elements.
Okay there are only four elements we have inserted four elements.
So Deven data let's say it contains the address 700 and the next index it will be we have inserted four
elements so next and next week before and its capacity is five okay.
And at this line we are creating a new object.
So do you do so due to data will also contain 700.
It will copy all the properties of the given object.
So the next index also forward.
And the capacity also five.
Okay.
So we can see here.
700 and 700.
So what copy constructor will do.
It creates a shallow copy or shallow copy will be created.
And what I am doing here is I am changing the first value of the I'm changing the zero element to hundred.
Okay.
So the idea was 10 20 30 40 50 and 60.
So this was the event every and I am changing the first index.
Now doing that it will become this under 20 30 40 50 60 and we are printing the 102 but we will see
when I print due to arguably be hundred at zero the index hundred will be printed.
Okay.
Why.
Because we all know copy gastric procreate shallow copy.
So let's see
okay so we can see here with changes with changes in the dynamic added even by the changes are also
done for dynamic error do you do.
Okay.
And the reason is we all know by the course copy gastric decorates shallow copy.
Okay so one more thing can we use copy assignment operator.
Okay so let's try to use copy assignment operator
so I am creating dynamic 83
so default consecutively record and then I am using copy assignment operator so add this line copy assignment
operator called Okay and then I will do the changes for Deven and then I am printing the event you do
and let's say we are also printing these three Okay so I am printing Deven data and data.
So at this line our default constructively we call okay our default constructively recalled and then
I am copying the values of Deven and do the three or add this line also I like call our default constructor
will be called.
So I am copying the values of the into D3.
So do you think after doing changes in Deven detail will not be affected.
So the point is copy assignment operator also create slow copy.
Okay.
It will also carry a cello copy.
So given due to the tree the oil will contain everything will get changed.
Okay I am doing changes in Deven so that changes will get reflected in due to because cello copy and
the changes will also get affected in the tree because again copies and not operator again creates cello
copy.
Okay so let's see
so we can see here is the changes individually but the changes got reflected for the 23.
Okay so here it is mandatory for us to create deep copy.
Okay.
So how to create deep copy so we have to create a copy constructor first.
So if w what I am doing here is I am creating my own copy constructor Okay I am creating my own copy.
So this index is very familiar so dynamic Gary and what it will we will take our dynamic Gary constraint
offense so constant reference by constant defense because you do not want to call copy against a better
ways in finite loop.
I hope you remember
so I have to copy everything.
So let us first copy the next index
did
next to nix so this error capacity is needed capacity
and this ATL data is the DOD data.
Okay so this line
this is shallow copy.
So this is the code which is that are done inside the inbuilt copy constructor.
Okay so now we will create deep copy so I hope everyone remember what is the procedure for creating
the deep copies of the procedure is very simple.
Create the new every copy the world will lose.
So we have to create a new area.
So let's say new and so what of the size size will be the large capacity
okay.
Here we do not need to do plus one in case of string.
We need to do placement because we want to copy the Democratic proposal.
So here no need to do plus one.
And now we have to copy the content.
So in case of string we have a function string copy.
But in case of an integer that we do not have such function.
So we have to copy the old content of dairy.
So the DOT I should write.
Do you draw an extra next or do you not capacity.
So the answer is next 16 y an extra X because next makes me use these main number of elements has been
inserted inside the area.
And what we have to do we have to copy the content.
So this raw data if I is the dart data off i okay So everything seems fine.
Let me check this.
Okay so according to me everything is fine.
Okay.
One more thing.
This is optional.
Okay.
Writing this is optional because uh do you draw next 16 and next and next.
We can easily separate out okay and constant offense I hope you know.
Okay so everything seems fine.
Later on this file and now we will run this fine.
Okay.
So what will happen when we run this file.
If we do changes in Deven so do two will remain unaffected.
Okay so let's see.
Okay.
So handed and the value of the two is pen only.
Okay.
So the code is working.
And now let us fix this problem also.
Copy assignment operator.
So it is our operator.
So what we have to do we have to overload this operator and we have started about operator overloading
a lot.
So what we have to do we have to adjust our load equal operator.
So we're dealing with the return type so certain types will be void.
We are not returning anything.
And operator keyword.
I won't overload equal operator what it will take as argument.
It will take our dynamic any and how it will take it will take by constant reference and let's say my
name is D.
Okay so what is happening here is.
So what do the code the code will be exactly the same.
Okay.
It will be exactly the same.
The code which was what copy constructor okay.
Exactly same code.
So inside the equal operator inside the copy assignment operator it also creates the shallow copy but
we have to create the copy.
So we overloaded the operator.
Okay.
So how it will call
Sylvia creating dynamic at a let's call it do to and then we are using copy assignment operators are
duty question given.
So this thing will go in this and it will move past this argument okay.
So argument means here.
Okay.
And this.
So this.
Okay.
I hope you know everything.
Okay.
So now let's send this file and now give it all to her and this file.
So now we can see Ben.
Okay.
So the tree is also not getting affected by doing changes isn't even.
So now even you and D.T. they are independent.
Okay.
They can work properly.
Now let us create one more constructor so this to him.
Let us create one parameter based constructor.
Okay so our dynamic array lets say the user wants to give the value of capacity.
Okay so let's say use I will give the value of capacity then issue the size of the array so data is
I will create the array of size see so the next index it will be simply zero and the capacity will be
C.
Okay.
So now let us.
So now what happens is user can create an array of the size he wants initially.
Let's say initially he wants array of size hundred.
So we will be able to create an array of size ended by using parameters constructors or default or to
get capacity
OK and here what we have better make price constructors what better met paramedics constructor there
is no need to write out because default.
That's my desk constructor does not exist.
So this is better my best constructor when we will do default.
Get capacity out all our output will be rendered.
Okay
okay.
So I think we've all got to run this file
all again silly mistake.
We forgot to print.
So now we can.
So in our diary.
So now the size of the default object is.
Hundred.
Okay.
The size of that is.
Hundred.
So in this way user can if user want he can give the size of the array initially.
Okay so now we have to read about constraint function.
Let's say let's see.
So can this function be a constraint function.
Yes it it qualifies for a constraint function because it is not changing the properties of the current
object.
Similarly print is also a constraint function.
It is not changing the properties of the current object.
Get this all to a constraint function it is not changing the properties of the current object.
So can this function add to be a constraint or question function.
No.
Why.
Because it is changing the properties of the current object.
Similarly this added function is also an order constraint function.
So operator No this is changing the property of this current object.
No no no.
Okay.
So only get capacity print and get function if you want you can write.
Name this get element.
Okay.
Okay so we forgot to test this function.
We forgot to test get element function.
So let's test this function also.
So let's get see out get element.
Okay so see our Deven not
get the element so which element do we want.
Now let's see the element present at index 0.
Okay
so the element has entered index 0 waste and and ready if I do element present at index 5.
So the output will be 60
so that would produce 60.
And what if the index is not valid let's say six so there is nothing present at index X or let's say
you make it then there is nothing present at index 10 so we'll get minus 1.
Okay.
So by default if you see here we are attending minus one so that it is done minus one.
Okay and what if the index is negative so minus 10.
Again we will get negative 1 Okay.
So I think we have tested everything.
Yes I think according to me we have tested everything okay so we have tested our code completely.
So if you have any doubt you can ask me whenever you want.
Okay so I hope you understood this really completely.
Okay.
Thank you.