Dynamically Create Rectangles Project


Course Overview

The course is part of this learning path

Start course
1h 25m

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.


Learning Objectives

  • 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

Intended Audience

  • 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++.




For this project, we'll use the exact same rectangle class files that we used when we were learning about classes. You can copy your own class files into Visual Studio folder or even recreate the files and copy and paste the code. You can also download them as a resource with this lecture. Either way, this will be the same class from before. We're going to create a project in Visual Studio named DynamicRectangles. And what you're going to do with this is you're going to do something a little different than what we did before. I'd like you to create a regular built-in array of size three in the main file. In other words, you don't have to dynamically create it. However, it should be an array of Rectangle pointers. For each element, you need to use the new keyword and create rectangles of different sizes and shapes. Then, loop through the array and print out the areas and perimeters of each rectangle pointed to by the indices in your array. And don't forget to delete them when you're done. An important little hint: remember, in this case, you are not dynamically allocating the array. So, you don't need to use the delete [] notation. You have to delete each of the rectangle objects pointed to by each index in the array, though.

So, this one has some interesting challenges associated with it. But I think you can figure it out. Don't get too frustrated if you have troubles because I'm asking you to extrapolate a little having learned some things in the course already based on your current knowledge. But if you think it through carefully, I think you'll do just fine. Okay, let's demo the DynamicRectangles project, so that you know what the output might look like. So, debug. Start without debugging. And then, this would just be the three different rectangles, perimeters, and areas printed out. So hopefully, that gives you at least an idea. It's not a huge hint, but it might make it a little bit more feasible for you. So, pause the video and create the DynamicRectangles Visual Studio project. Come back when you're done or if you need some help.

All right, how'd that go for you? Were you able to solve the problem? Well, let's create a new project and we will call this DynamicRectangles. So, empty project. We would call it DynamicRectangles. And notice, it seems like most of the projects in the course I've saved under my repos directory, source repos in my user profile, DynamicRectangles. So, I'm going to keep that in mind for just a second and hit 'create'. And here's my project here. But before I get to this, I want to go to that repose directory. I had it open conveniently. And you'll notice that I have DynamicRectangles right here. DynamicRectangles. And you'll also notice that down here, I have something called  RectangleFun. All right. So, what I'm going to do, watch me carefully, is go into RectangleFun. And then, the RectangleFun project folder. So, that was the solution folders, the project folder. This will be different, if you're using a different IDE, of course. But we have these three files right here. The cpp, the other cpp and then the .h file. We only need Rectangle.cpp and Rectangle.h.

So, I am going to go in Windows, 'control click' these two, so it highlights both of them. And then, I'm going to either 'ctrl C', or 'right click', copy. I'm going to very carefully back out into the repos again. And then, go to DynamicRectangles, go into the project directory and 'paste' them. Good. Now, what we need to do now though, is tell Visual Studio about it. We need to tell them about these two files, so they're in our project directory, and DynamicRectangles. So, let's go to source, or well, we'll do header first. We'll say 'Header Files', 'Add', Existing Item'. So, this is different than what we've been doing before. We're going to right click 'Add', 'Existing Item'. And actually, just to save us some time, I'm going to actually 'control click' both of these, and then, I'll just move them to the appropriate directory of Visual Studio. So, I'll take both of these, I will click 'Add' and I notice they both show up under Header Files. Well Rectangle.cpp, you belong in Source Files. So, I'd click, drag it down there, and you can open these and double check what they look like, but they're exactly the same as before. They're the same code, Rectangle.cpp, our implementation from the class section.

And I'm going to create a main file 'Add', 'New Item' under the sources. So, let's go to main, do main.cpp, #include our 'iostream' and then namespace std, return 0. And of course, we need Rectangle.h. "Rectangle.h", excellent. Now, remember we're supposed to create an array of size three. So, I'm actually going to create a constant to help me. If you just use the literal, that's okay. But I'm just doing this to make it a little easier, and it's going to be rectPtrs[ARR_SIZE]. Now, notice this creates the array of this particular size and what is in the array, rectangle pointers. Okay? So, hopefully, we're able to extrapolate that. If not, don't be too frustrated. It is new. So, we have to create three rectangles. Let's do a (5, 3). And let's do, a (20, 40) and a (2,10); int i = 0; i < ARR_SIZE; i++. And now, we can print out rectPtrs[i] and then, use the '=>' because number at every index in this rectPtrs array, we have a pointer to a rectangle. So, we can use the arrow notation. Okay, perimeter. And we also want area for each of those. Good. When you're done with them, you want to make sure to delete,  right? int i = 0. Here's an [i].  And then, we could be extra careful here.

Each index should point to a null pointer. So, if you forgot it, not a big deal. I mean it, in industry level code it would be a bigger deal. But since we're at the end of main, it's not that big of a deal. But if you did think of it, congratulations. That was a bonus points for that. So, let's run it just to make sure everything's good. We made an error or something and it doesn't look like we did. All right. So, it prints out all the different numbers in here. So, you notice that it's. Oh, that's interesting. Okay, so it's printing out the perimeter on the one line and the area for the first rectangle. Then, we have the perimeter and area for the next rectangle and perimeter and area for the next rectangle. We could make it a little more obvious. We'll put a little endl at the end there, and run it again just to make it, the output a little cleaner and there we go. So, we've got a perimeter and area for the five by three. That's funny. The area and the perimeter so close together. That's what I was thinking about. So, it's interesting. Then, you've got the perimeter and then the area here, and then the perimeter and area there.

So, I always find it interesting that the area and the perimeter can be so close, or even the perimeter can be bigger than the area as far as the number is concerned. Very interesting. So, awesome job everyone. Even if you didn't finish the challenge, or forgot some part of it, that's fine. You're growing in your knowledge, and adding to your skill set with every minute you study and practice. For that, I applaud you. You're now in much deeper water than when we started. So, if you ever feel like you're drowning, just relax and keep on swimming. Sometimes, you might need to float. And just check my solutions if you get totally lost. And that's okay too. Other times, you might feel like you need to repeat lectures, or even entire sections again, that's okay. This isn't a sprint. It's a marathon. Stay with it and you will continue to improve. In the next lecture, there's another similar project waiting for you. I'll see you there.


About the Author
Learning Paths

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.

Covered Topics