This time, we're going to the center before the geter.
The array field contains no values, so right now there's nothing to really get.
So in this lesson, we're going to add a letter that updates the cars field.
The dealership has one field, so we need to add one setar, I told you before that a seller starts
with the word set and ends with the full name.
So should our set to be called set cars?
No, said cars implies updating every object at once.
And that doesn't make any sense.
Currently, the car's array only has room for three cars.
But what if we made room for one hundred cars or a thousand cars and you only want to update one of
them?
Surely you're not going to pass in an array of a thousand objects for the sake of updating one?
The screaming cat doesn't approve.
The senator should update one object at a time and let the user choose which one.
This approach offers more flexibility.
So when your dealership class at the Sutter said car.
And it's going to take one car object as a parameter.
All right, now our dealership object can call set car and when called set car is going to receive a
car object.
But which index in the car, Zoraida, we add it to we need to add another parameter that takes an index
value int index.
And at the index chosen by the user, we're going to set that element in the car's array.
Equal to a new car object.
And we're going to call the copy constructor by passing in a source object.
Also, notice that I'm setting the area equal to a copy of the car object, and I'm not just setting
them equal to each other.
You don't want your field to get caught in the reference trap.
Get in the habit of creating a new object and using the copy constructor to copy every value from a
source.
Right after we're creating a new car object, we're passing in a source object down the nose to run
the copy constructor that receives one as a parameter.
And then it's going to copy every field value from the source object into the one we just created.
And remember that this refers to the current object that's called the A constructor or method.
All right, now we can update the cars right in Maine, we're going to update the first index in the
cars field with the Nissan Object dealership that said car.
Nissan.
At Index zero.
And now update index one with the Dodge object.
And indexed to what, the second Nissan?
And they're now your dealership has three cars.
First, you're setting the element at index zero equal to a new car object, and after creating the
new object, you're passing in a source.
So have a nose to run the copy constructor, which copies every field from the source object into the
current object.
You did the same thing for index one and two.
And now your dealership is full of cars.
Now, before we wrap up, you can clean up our code a little, instead of defining three separate car
variables, we can define an array of cars.
Currys.
Is equal to a new array of car objects.
The first element in the array is a car object with fields that describe a Nissan.
The second element in the array is going to be another car object with fields that describe a dodge.
And the third element of the car object is also going to be a Nissan, but with a color of yellow.
And a new set of parts.
Also, by the way, this.
Is the same thing, is this.
And you know what, we can also pass this array directly.
And now we can pass in the car object at index zero.
Index one.
An index to.
And now you might be wondering, isn't there a better way to do this and you would be right to assume
so instead of writing the same code three times a week can create a far loop that runs through the length
of the car's array.
By the way, since you're familiar with loops at this point, I don't mind showing you a trick to autocomplete
A for loop.
Just click on Fawry.
And what this does is it creates a loop that runs through the length of the nearest Thouret, pretty
convenient.
I didn't want to show it to you before just because I wanted you to practice creating loops.
But right now, I know you're an expert.
Anyways, during each run, we're going to call a set car.
And we're going to index a car object and pass it in as the first argument and in the second argument
pass in the index, that's it.
Ultimately, the Sloup is going to update the cars filled with car objects, that index is zero one
and two.
In this lesson, you added a Seder.
The Seder set car receives two values, an object and an index, and then it updates the array at an
index of your choice.
You updated index zero of the cars field with the Nissan object.
One would dodge.
And indexed to what, the second Nissan?