The course is part of this learning path
In this course, we will explore the basic concepts and fundamentals of pointers in C++ and you'll learn how to use them to point to dynamically allocated memory.
- Learn the fundamentals of pointers
- Learn how to allocate memory dynamically and also how to return it
- Explore rectangle and circle classes and create instances of them dynamically
- Beginner coders, new to C++
- Developers looking to upskill by adding C++ to their CV
- College students and anyone studying C++
To get the most out of this course, you should have a basic understanding of the fundamentals of C++.
In this lecture, I want to expand your knowledge even more by working on another project. This will be somewhat similar to the previous project in which you created rectangles dynamically and placed the pointers to them into a built-in array of rectangle pointers. In this project, you will use the Circle class we worked on before. And you will place pointers to the circles inside a dynamically created array. I want you to create a project called DynamicCircles. I want you to copy the circle.h and circle.cpp files from your project we did when talking about classes or from the course resources into your project directory. Before creating the dynamically allocated array, ask the user how many Circle objects they'd like to make, then create an array of that size using dynamic techniques we've learned in this section. This takes a really interesting technique to do this. Remember when we created an array of integers but created the array dynamically?
We had to use the asterisk for that, right? Like int*, right. Well, here's your big hint. If it had been an array of integer pointers, we would have needed to use two asterisks in the declaration, like int-star-star-myArray. So, int**myArray. So, that should give you an idea of what you're doing with the circles. You need one of the asterisks to say, hey, it's a dynamic array and then another to say it's a dynamic array of circle pointers. Then what you're going to do is you'll iterate and continue asking the user to enter a radius for each circle, which should also be allocated dynamically. The circle should be allocated dynamically. When the user has entered the radii for all the circles that were to be created, you will then print out the circumference and areas of each of the circles. Finally, don't forget to return the allocated memory for each circle. And also when that's done, separately return the memory for the array of circle pointers that you allocated. This might seem tricky, but you need to make sure to return the allocated memory for each individual circle object and then and only then return the memory allocated for the array. So, here's the DynamicCircles project that's been completed, but I won't be showing you the code, of course. The circle.h and circle.cpp is just the code from before.
But let's run it, 'Debug'. 'Start Without Debugging', and it asks us how many circles we'd like to make. So, let's say four circles. And also what's the radius for circle 0, and I can say 10 and then 13, 2, and 6, and there we go. So, it gives us exactly however many circles we need. And I could have put a different number, I could put 7, I could put 177. It doesn't matter. It's going to dynamically create the array to hold pointers to each of these circles. Don't forget to delete them when you're done, of course. All right. So, pause the video and work on this project. Come back when you're done, or if you need any help. How did that work out for you? Were you able to complete the project? It was a little tricky and used some combinations of syntax you haven't used before. So, I hope you were able to complete it. Regardless, be proud of yourself for trying. Let's work on it together. So, we will create a new project , and this new project will be called DynamicCircles. So, 'Empty project', 'Next'. We will call it DynamicCircles. Give it a moment. Now as we did with the rectangles, we will go into wherever you have your source code which you can download as a resource or if you have it from working on it previously. We have the CircleProject. The regular old CircleProject, go in there and then the project folder. I have the circle.cpp and the circle.h which I am doing 'Ctrl-Click' in order to get both. Then I'm going to 'right' click, 'Copy', 'Ctrl-C' on Windows. And then I'm going to go to DynamicCircles in here, DynamicCircles which is our current project, 'right' click, 'Paste'. So, I've put both of these in here. I'll close that but we still have to tell the project they're part of the project. So, just like we did with the rectangles, I'm going to 'right' click, 'Add', 'Existing item' and then I'm going to grab the two here, circle.cpp and circle.h. And then drag cpp into source files. And obviously if you want to, you can look at them. They're identical to how we left them before. We also need to create a source file, main.cpp. Good.
So, let's see here. All right. So, we're going to include iosotream, namespace std, and of course, I don't want to forget the circle.h. That would be silly. We need to have main right there, and I'm going to actually make a helper function for one of the tasks, which is printing circles. If you didn't do this, that's fine. It was not spelled out. So, to point to an array of circle pointers, I actually can use a double pointer here. So, my circleArray, int numCircles and I will actually copy that and paste it down here, and we will fill that out in just a moment. So, inside main I'm going to start with how many, and then I'm also going to have a radius variable to hold the radius as we go along. Print out, "How many circles would you like to make?" All right. And then the user enters how many. Right? Then we have to create our array. So, circles = new Circle* array of the size howMany. Right? There's two here because it's one of them is to say, hey, this is a dynamic array. The other is to say this is a dynamic, well this says it's a dynamic array, this says it's a dynamic array of circle pointers. Okay. All right. Now for (int i = 0; i < howMany, i ++) You're going to again prompt to the user, "What radius for circle" and maybe we're numbering the circles. This is just to make the input look cool, but the output and the input looked cool. So, don't worry if you didn't do something like this. Again, these projects are a lot more open ended than a hello world project like we did at the beginning. So, each time you create new circles. All right. And then we're going to do an endl and then I want to call printCircles() and I have to pass it circles and howMany. Well, before we write anymore in main, we should probably write out print circles. So, in here, this is going to be fairly simple. This is the whatever number of circles was passed in. We're going to loop through, and print out "Circle" << i << endl then tab and then we're going to say circleArray[i]-> circumference(). Right? And then I'm going to do another tab, and then circleArray[i]-> area. And then another nice endl at the end to separate the different circles, provide some space. So, now what do I do after I call printCircles? Well, you have to delete individual circles. So, you open through again, i < howMany, i++ And we have to delete circles at i. And then as a good habit, set that to null pointer, just like we did before. But we also dynamically allocated the array this time.
Last time we did not. So, this time we have to delete the array named circles. All right, pretty cool. Looks pretty good. Let's give it a run just to see if it works, there's any errors, any problems. All right. So, how many circles would you like to make? Let's say three. What's the radius for the first circle? circle 0 will say 8. Let's say, oh I don't know, 2. And then 15, right. Good. Pretty cool. That we could have obviously taken in radius as a double as well if we wanted to. They would probably be more realistic, but no big deal here. You'll notice that we have circle 0 right here, circle 1, and then circle 2. Everything looks pretty good to me. So, I hope it looks good to you. We could run it again to double check and maybe make a different number of circles. Maybe I want I want to make five circles. So, circle 0, maybe it's 10, then 12, then 8, 9, and 6. And that does the same thing. Our print method seems to be for function, rather print function seems to be working really, really well. Everything seems to be great. So, that's great. That was quite a challenging project, wasn't it?
I wouldn't blame you if you didn't get it all done without some help and looking ahead or reviewing. And especially the part with the double pointers, the two asterisks. That was a bit new. So again, I'm trying to improve your extrapolation skills. I did give you big hints. But we want to work with our current knowledge as well as the hints I give you and form ideas about what something might look like and how to solve the problem. So, if you find yourself getting frustrated with some of these more challenging problems or problems where I have you fill in some of the empty places with information we didn't explicitly talk about, don't get worked up. You're doing fine. Just breathe. In the next lecture, we'll wrap up this section and prepare for the next. I'll see you there.
John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.