1 00:00:00,580 --> 00:00:02,860 This time, we're going to the center before the geter. 2 00:00:06,160 --> 00:00:10,150 The array field contains no values, so right now there's nothing to really get. 3 00:00:12,730 --> 00:00:15,760 So in this lesson, we're going to add a letter that updates the cars field. 4 00:00:19,250 --> 00:00:24,590 The dealership has one field, so we need to add one setar, I told you before that a seller starts 5 00:00:24,590 --> 00:00:26,490 with the word set and ends with the full name. 6 00:00:27,050 --> 00:00:29,800 So should our set to be called set cars? 7 00:00:30,320 --> 00:00:34,340 No, said cars implies updating every object at once. 8 00:00:34,340 --> 00:00:35,760 And that doesn't make any sense. 9 00:00:36,140 --> 00:00:39,100 Currently, the car's array only has room for three cars. 10 00:00:39,530 --> 00:00:45,290 But what if we made room for one hundred cars or a thousand cars and you only want to update one of 11 00:00:45,290 --> 00:00:45,480 them? 12 00:00:46,160 --> 00:00:51,230 Surely you're not going to pass in an array of a thousand objects for the sake of updating one? 13 00:00:52,340 --> 00:00:54,020 The screaming cat doesn't approve. 14 00:01:04,550 --> 00:01:09,770 The senator should update one object at a time and let the user choose which one. 15 00:01:10,800 --> 00:01:12,890 This approach offers more flexibility. 16 00:01:17,380 --> 00:01:20,140 So when your dealership class at the Sutter said car. 17 00:01:27,430 --> 00:01:30,040 And it's going to take one car object as a parameter. 18 00:01:32,310 --> 00:01:37,830 All right, now our dealership object can call set car and when called set car is going to receive a 19 00:01:37,830 --> 00:01:38,670 car object. 20 00:01:39,480 --> 00:01:45,540 But which index in the car, Zoraida, we add it to we need to add another parameter that takes an index 21 00:01:45,540 --> 00:01:47,220 value int index. 22 00:01:52,050 --> 00:01:56,940 And at the index chosen by the user, we're going to set that element in the car's array. 23 00:01:59,720 --> 00:02:01,790 Equal to a new car object. 24 00:02:04,940 --> 00:02:08,570 And we're going to call the copy constructor by passing in a source object. 25 00:02:10,710 --> 00:02:15,420 Also, notice that I'm setting the area equal to a copy of the car object, and I'm not just setting 26 00:02:15,420 --> 00:02:16,410 them equal to each other. 27 00:02:16,830 --> 00:02:19,460 You don't want your field to get caught in the reference trap. 28 00:02:19,500 --> 00:02:24,000 Get in the habit of creating a new object and using the copy constructor to copy every value from a 29 00:02:24,000 --> 00:02:24,540 source. 30 00:02:25,050 --> 00:02:30,300 Right after we're creating a new car object, we're passing in a source object down the nose to run 31 00:02:30,300 --> 00:02:32,730 the copy constructor that receives one as a parameter. 32 00:02:33,030 --> 00:02:37,440 And then it's going to copy every field value from the source object into the one we just created. 33 00:02:38,070 --> 00:02:42,720 And remember that this refers to the current object that's called the A constructor or method. 34 00:02:45,480 --> 00:02:50,100 All right, now we can update the cars right in Maine, we're going to update the first index in the 35 00:02:50,100 --> 00:02:54,120 cars field with the Nissan Object dealership that said car. 36 00:02:55,410 --> 00:02:56,150 Nissan. 37 00:02:58,640 --> 00:02:59,750 At Index zero. 38 00:03:03,650 --> 00:03:06,350 And now update index one with the Dodge object. 39 00:03:12,040 --> 00:03:14,200 And indexed to what, the second Nissan? 40 00:03:26,830 --> 00:03:29,350 And they're now your dealership has three cars. 41 00:03:35,840 --> 00:03:41,030 First, you're setting the element at index zero equal to a new car object, and after creating the 42 00:03:41,030 --> 00:03:43,030 new object, you're passing in a source. 43 00:03:43,400 --> 00:03:48,260 So have a nose to run the copy constructor, which copies every field from the source object into the 44 00:03:48,260 --> 00:03:49,160 current object. 45 00:03:50,450 --> 00:03:52,300 You did the same thing for index one and two. 46 00:04:07,870 --> 00:04:09,760 And now your dealership is full of cars. 47 00:04:11,430 --> 00:04:16,410 Now, before we wrap up, you can clean up our code a little, instead of defining three separate car 48 00:04:16,410 --> 00:04:18,990 variables, we can define an array of cars. 49 00:04:21,040 --> 00:04:21,700 Currys. 50 00:04:23,610 --> 00:04:26,220 Is equal to a new array of car objects. 51 00:04:32,370 --> 00:04:36,300 The first element in the array is a car object with fields that describe a Nissan. 52 00:04:46,300 --> 00:04:51,130 The second element in the array is going to be another car object with fields that describe a dodge. 53 00:04:52,650 --> 00:04:57,210 And the third element of the car object is also going to be a Nissan, but with a color of yellow. 54 00:05:05,500 --> 00:05:07,000 And a new set of parts. 55 00:05:20,630 --> 00:05:22,310 Also, by the way, this. 56 00:05:24,510 --> 00:05:26,190 Is the same thing, is this. 57 00:05:27,780 --> 00:05:31,020 And you know what, we can also pass this array directly. 58 00:05:52,320 --> 00:05:55,050 And now we can pass in the car object at index zero. 59 00:06:00,040 --> 00:06:01,090 Index one. 60 00:06:01,950 --> 00:06:02,880 An index to. 61 00:06:03,880 --> 00:06:08,560 And now you might be wondering, isn't there a better way to do this and you would be right to assume 62 00:06:08,560 --> 00:06:14,260 so instead of writing the same code three times a week can create a far loop that runs through the length 63 00:06:14,260 --> 00:06:15,100 of the car's array. 64 00:06:15,880 --> 00:06:20,650 By the way, since you're familiar with loops at this point, I don't mind showing you a trick to autocomplete 65 00:06:20,650 --> 00:06:21,150 A for loop. 66 00:06:21,550 --> 00:06:22,960 Just click on Fawry. 67 00:06:26,810 --> 00:06:31,940 And what this does is it creates a loop that runs through the length of the nearest Thouret, pretty 68 00:06:31,940 --> 00:06:32,600 convenient. 69 00:06:33,170 --> 00:06:36,620 I didn't want to show it to you before just because I wanted you to practice creating loops. 70 00:06:36,620 --> 00:06:38,480 But right now, I know you're an expert. 71 00:06:39,610 --> 00:06:42,730 Anyways, during each run, we're going to call a set car. 72 00:06:44,190 --> 00:06:50,010 And we're going to index a car object and pass it in as the first argument and in the second argument 73 00:06:50,010 --> 00:06:52,500 pass in the index, that's it. 74 00:06:59,550 --> 00:07:04,170 Ultimately, the Sloup is going to update the cars filled with car objects, that index is zero one 75 00:07:04,170 --> 00:07:04,590 and two. 76 00:07:07,250 --> 00:07:08,880 In this lesson, you added a Seder. 77 00:07:08,930 --> 00:07:15,170 The Seder set car receives two values, an object and an index, and then it updates the array at an 78 00:07:15,170 --> 00:07:16,460 index of your choice. 79 00:07:18,730 --> 00:07:22,390 You updated index zero of the cars field with the Nissan object. 80 00:07:26,150 --> 00:07:27,530 One would dodge. 81 00:07:33,570 --> 00:07:35,700 And indexed to what, the second Nissan?