MyProgrammingLab 9-8 Part 2 Transcript

Print

So Day P has already been created for us. And for you to know how that was created it's a date pointer date P. Like that. So that's how you create it.

Because it needs to be a pointer. Because when your use the new keyword to create something over on the heap, the new keyword gives you back a pointer. So all you have is a pointer to the heap. That's all you have.

And then what's at that pointer, well, we get to tell it. So if you create an integer, you have to say int. If you create a date, you have to say date. Like that, so it's a date pointer.

Now they're saying the date pointer has already been created. So I'm going to go ahead and get rid of this part since it's already been created for us. And then they're saying create a new date object. And they're saying the date has a constructor that takes the month, the day, and the year. So the month is 3 the day is 12, and it says the constructor has integer values for the month, day, and year.

And then the year is 2006. So at the date pointer, we're going to assign a new date object using the constructor that takes three integers, one for the month, one for the day, one for the year. So how that works?

Use a new operator to allocate memory for an array of 100 integers. So they want you to create a dynamic array. So we're going to go ahead and say, let's see, what's the integer? We're going to say integer pointer. And they're not giving us a name for the array, so I'm going to say numbers is going to be a new integer array with 100 slots.

So 100 integers are built into this array. So since I'm using the new keyword, this integer array is dynamically being created down there on the heap. And then it gives me a pointer to that array. So this right here is a pointer to that array that was created over in the heap.

Ah. Did something wrong. That's OK. So we just figure out what's going on. Let's see.

I haven't seen a correct solution that uses an asterisk. So that means that they don't want to actually create it. I created it. They just want you to make the call.

So I'm going to go ahead and get rid of that, get rid of this, and there's the call. The new int array with 100 positions. Like that. So they just wanted you to do the new array like that.

Now I'm going to click down here. Oh, and then also, before we continue on, I want to show something really quick. If you create an array-- so if I say int pointer numbers is a new array, and then a semicolon, this is an array. It's not a standard variable. So that means I have to say delete array number.

And actually I shouldn't say number because this is an array. So an array is a group of variables, so arrays should always be plural. So this should be numbers. So deleted array numbers.

If you create an array over there on the heap, you have to say that this is an array that needs to be deleted. Otherwise it will just delete the first position. If I just say delete numbers like that, it will only delete the first position of the array and that's it. The rest of the array will be sitting out there as a memory leak. So just remember, if you create an array, you have to use delete array to clean up the memory.

DP is to refer to an array of doubles. Assume the integer variable n has been assigned a value, declare DP appropriately. And this is where dynamic arrays are so valuable. Because if I say int n is 50 like this, and then I say int-- or double, because they want doubles here-- double numbers, and then I make it-- if I say 50 like this, that works. No problem at all.

So this is how we create arrays. And you learned from your last class this is how we create arrays in C++. However if I come in here and I put an n in there like that, this actually crashes. And it says this number in here must be a constant value.

Because what if I want to make the array whatever size user tells me? So here I can initialize it to 0. And then I could say c out enter the array size like this. And then I can go cn into n like that. And then I can create the array like this.

Well, if you put this code into C++, this will crash and burn. Because it will say you can't do that. It'll say when you're building an array on the stack, you have to give me a constant value here. Because if you don't give a constant value, then C++ doesn't know how to put it on the stack. So it's unable to do it.

You cannot dynamically create an array on the stack. So this actually crashes. And I'm going to grab this code real quick. I'm going to do a Control-C. I'll flip over to Visual Studio and just show you real quick.

So if I do a Control-V right there-- Do you see that blood right there? The red I call blood. And this is blood. So you can see that blood right there. If you hover your mouse over it, it says expression must have a constant value.

It says you can't do this. So how can you do it? Because sometimes you don't know how big you want the array until you read the data size for your database, or you look at your file to look at how many items you have. And the database is constantly changing. And the file is constantly changing.

So sometimes you don't know. Sometimes you have to create it dynamically. So how do you do it? Well, you create a dynamic array.

What you do is you say is assigned a double array, and you have to use the new keyword to put it over on the heap. And the new keyword gives you back a pointer. So that means this needs to be a pointer.

Because it gives you a pointer to the heap like that. And with this right here, it can be any number that I want. No problem at all.

Then I'm just going to say for int i. And actually, the for, I don't know if you all know this, but if you just type for and then hit your Tab key, it actually builds it out for you. So this, I'm going to go ahead and say int. And then the length is going to be n, because that's the length. So I'm going to say n and then I can say see out enter number-- let's go enter number.

And then I'm going to show the i + 1. And then I'm going to show a colon space bar, like that. So i plus 1, just because i starts at 0, and I don't want it to say enter number 0, I want it to say enter number 1 like that. And then enter number 2 like that. So that's why I'm saying i plus 1.

And then I'm going to see in read into numbers as my array, my dynamic array at position i like that. And then to show you that everything works, I'm going to display it. So I'm going to do a for loop. I type in for. I hit my Tab key.

And then I'm going to use the int data type. And then my size is going to be n, because that's what the user told me right up here. And then here I'm just going to say see out. And I'm going to show you numbers at position I like that. So that's going to show you all the numbers.

[End of Audio]