Templates, the STL, and Other Skills
The course is part of this learning path
In this course, we will build on your existing foundational and object-oriented oriented skills and enhance them by looking at templates, the Standard Template Library, and other skills to help you in your builds.
- Learn about function templates and class templates
- Learn how to write efficient and excellent code with the data structures and algorithms in the standard template library
- Learn about smart pointers to manage dynamic memory automatically
- Understand friend functions, friend classes, and operator overloading
- 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 the last lecture, we had a project focusing on the STL. Specifically, a program creating an interactive dictionary based on the map type. In this lecture, you will create a project called AlienProject in which you will create an Alien class with operator overloading for breeding aliens and comparing them. Specifically, you will overload the equal to or quality operator, the not equal to operator, greater than, greater than or equal to, less than, less than or equal to, which each have to be done independently, addition operator and assignment operator as well. Clearly you have to implement the methods in the UML diagram, as well as the operators that we discussed. Note that all of the relational operators will be quite simple, in that they all return boolean values and simply take an Alien reference as their parameter, and compare yourself alien to the other alien, kind of like what we do with rectangle. The gender should be a single uppercase character of M or F for male or female. This alien species considers height, weight, and gender to impact social status. In this alien society, tall heavy female aliens have the most prestige, and short skinny males have little prestige. Specifically, prestige is calculated using two gender points for males and three gender points for females. So, you have the following formula, prestige equals height times weight times gender points. Two aliens are considered equal if they have the same prestige value. When our aliens breed to produce a new alien, you need to use the addition operator. Two aliens of any gender combination, male and female, female and female or male and male in this species can actually create offspring. The calculation to determine what happens when you add them is as follows, for the new alien that's created, the weight of that alien is the sum of both parents weights divided by two, the height is the sum of both parents weights divided by two. For the gender, use a random number generator with a 30% chance for female and 70% chance for male. There are many ways to do this. One way would be to generate a number between 1 and 10, and if the number is a one, two or three, it's a female, and male otherwise. But you can use whatever technique you would like to use. The assignment operator simply sets all the fields of one alien object equal to another. To test your aliens in main, create a vector of unique pointers to alien objects. You can manually breed select aliens or create an entire user interaction for creating and breeding aliens. It's really up to you. This will be a pretty cool project for sure. So, when you're ready, pause the video and create the AlienProject. Come back when you're done, or if you need some help. How did that go for you? Were you able to create and test out your alien objects? Let's work on this together. So, we'll create the AlienProject right here as an empty project, AlienProject right here, and we of course need Alien.cpp, Alien.cpp right here and Alien.h. We need both of those, so Alien.h, Alien.ccp. We probably need cpp, right? There you go, Alien.cpp. And I can drag that down here, and I need main.cpp, of course. For main, we'll include iostream for right now, and we'll also include Alien, we'll add some other stuff in just a moment, but we'll get some of the bare bones stuff that we need. We'll need to include the C standard library and also Ctime, in order to use the randomizer, but we'll get to that in just a moment. All right, so Alien.h is where we will start. ifndef ALIEN_H define ALIEN_H and endif. And for the... We have a public section. Oops, sorry. I guess we need to have the class first, right? Okay, class Alien. We have a public section and we also have a private section as well. For the private data, we just need an int weight and int height. Now you can consider that centimeters, inches, whatever makes sense to you. You could even go with feet, it doesn't really matter. Maybe they are kilometers, maybe it's a really, really big alien species or meters tall. So, now we have Alien constructor which takes a weight, a height and a gender. We also have the getter, so getWeight, pretty easy, getHeight, also easy. Oops, sorry. This should be a char for getGender. We also need to get the Prestige which is not just a stagnant piece of data, it's calculated. And then, we have the operators which I'll put a little bit separated. And there's a lot of these too, so there's a lot to do with this but most of them are fairly simple. bool operator, Alien reference other, that doesn't modify the current aliens, so we put const just like any other function that doesn't modify. And then, we have operator not equal to. So, it is not automatically granted that once you have equal to that it knows what not equal to means. Similarly, when you have operator greater than, you also have to do greater than or equal to, if you want to have that functionality, it doesn't automatically figure it out. So, any operator you want to be available, you have to overload. Less than, so this is quite a bit of typing it seems. But we'll do a little bit of copying and pasting and most of the definitions are not too bad. Less than or equal to, Alien reference other const. Now, the plus actually returns an Alien. So, it's a little bit different, right? Because it will add the two aliens and return a new one, and that one is const because it doesn't modify either alien being added. It just creates a new one, which is totally okay. And then, the equal operator cannot be const because it sets my alien information to some other aliens information. Awesome. Now, I guess we'll do the randomization inside the aliens. So, let's keep the C Standard libon here. So, we'll do this Alien.h. And we also need to include, sorry, the C standard library and ctime. Those will be used for the randomizer, as we're using the plane rand function. And now I'm going to, just to avoid the typing. I'm going to grab all of these, and I'm going to copy them over here. Pretty decent, right? So this is going to take a little bit of time so you can pause and grab a sandwich if you need to, but we're not going to get here automatically. And if coding was totally easy, everyone would do it, right? So, this is just really, just take some time. It's not really that difficult. We'll figure it out and it won't be too crazy. All right. So, we've got greater than, and then we've also got the greater than or equal to which again, has to be defined separately. We have less than and we also have less than or equal to which is again defined separately. And then, we have the addition operator and the assignment operator as well. So, we've got all of these bodies ready. The next thing we need to do is set up the name of the class and the scope resolution operator so we know that they all belong. So this type of copy pasting is actually not frowned upon in coding, this will save you time. And this is totally okay. There are some automated features that we could be using to actually help us with this but I find that sometimes typing them out and going through the motions and getting good at it. And also, even though I've been using Visual Studio throughout this entire course, I don't want to use too many Visual Studio specific features. I know that seems kind of silly, but it's not a course on Visual Studio. It's a course on C++. So, this height is equal to height. This gender is equal to the gender that's passed in, right? So, we're overcoming the name shadowing by doing this in front of it, using this. Some of these are very simple, and I think you can probably figure out we need a getWeight, hopefully you did figure that out. We have height, return height and return gender. So, literally under 30 seconds, we just implemented three methods. Prestige is a little bit more difficult, but it's not too crazy. I'm going to create an integer to help me. So, it's not quite as messy, and you could use the conditional operator here. the ternary conditional operator here. But to make it a little more readable, I'm going to do this. So, if the gender is male, the gender value is two, remember. And if the gender is female, which is the only other option in this particular species of alien, the gender value is three. Now, once we do that, we want to have the Prestige. So, I'm just going to directly do it in line and return. You might have chosen to use a separate variable, and that's totally fine. But its weight * height * genderValue. All right. The equal to is fairly simple. Remember we used the Prestige that we just implemented for any of the relational operators. So, we know that the I my Prestige is equal to the others Prestige. If that's the case, then we, as aliens, are equal. Now, that could be done with a male and a female and they just happen to have the same Prestige value. But it could be two females with exactly all the same characteristics or any combination that leads to the same Prestige value. So, Prestige not equal to others Prestige. So, I think you're going to see a pattern here. All right. So, greater than return getPrestige greater than others Prestige, right? There we go. Right? That's pretty simple. So, I guess, with this, I might be able to take advantage of a little bit of copying and pasting, if you'd like. So, this will be greater than or equal to. Then we have this next one. Just be very careful when you copy and paste because it can lead to some very strange behaviors if you do it incorrectly. Less than or equal to. So, we've got all the relational operators. Those are pretty good. Now, the last two are a little bit different. So, remember, for the adding them, we create a brand new alien. And since it has a constructor that requires us to pass in the weight and the height and the gender, what I'm going to do is we're going to figure out, well, what's the gender first, and also get the new weight and height. So, we do need to seed the random number generator using the time function and null pointer, that will get the system time and be pretty random. And then we have randNum. We're going to say modulus 10. I'm going to use the technique I referred to earlier. So, that will generate a random number. And then it does clock arithmetic or modulus, right? So, since it does modulus, it will go from 0-9. So, those are the numbers we're going to get back from this. If we want 1-10, we could add one to it. You could have just used 0-9, it's not really a big deal. But we'll just say it will generate 1-10 inclusive. So, newGender. And then we have int newHeight is equal to, remember how we calculate the weight and the height, or new weight, sorry. I guess it doesn't really matter which one I do as long as I'm consistent. But (weight + other.weight) / 2. And then newHeight. Right. That's part of the variable name. Don't use the keyword, clearly. If it's part of the variable name, we're okay. Others height divided by two. And then we're going to determine what's the new gender, which we declared but did not set. So, say, if the random number that we generated is less than four, so if it's one, two or three, then we set the gender to female. That's the 30% chance for female. And otherwise, we set the gender to male. And now, we did all of that work so we could return an alien who has new weight, new height, and new gender. There we go. So, that wasn't too bad. Now, the next thing that we're going to do is assign the values from the other, right here. Since, this is the equals operator and it's not comparing but it's the assignment operator; not equal to but it's assignment. We're going to set our own values to the other aliens values. So, we're going to say, my weight is equal to others weight, my height is equal to others height, and also my gender is equal to others gender. So, this would be like a cloning process, right? So, that's making a deep clone of the other alien. Deep copy. Awesome. Now, if we go over to main.cpp, our alien is ready to test, and I think, actually, that should be good. I think we'll just need those. Let's do everything in main. We could break it out into functions but we'll do it this way. So, there's one alien. And there's the second alien, alien2, 200, 55, and male. And then Alien alien3 is 150, 60, and female. And then let's make alien4 = alien1 + alien2. And maybe you will throw in an alien5 = alien2 + alien3 just to work it out a little bit. And just do some kind of semi-random testing. You'd probably be a lot more deliberate if you were creating something for industry but... Notice my extra space there. I'm just going to put a bunch of true false as I could create actual functions to do the testing. Some people actually create the testing functions first, and that's actually called test-driven development. So, if you've ever heard of that, that's what that means. Let's actually... Since a lot of these are very similar, I'm going to do some copying and pasting and switching around. So, we'll compare alien1, maybe the not equal to alien3. Just make sure we're really careful. I don't really care which one you decide to do, just kind of remain consistent. Or if you did an entire user interface, that's fantastic. That's great. That's even more practice. So, let's try alien4 > alien5. And we're going to do this four. And then greater than alien5, for the boolean for that. And maybe we want to also... We'll use the same aliens and do greater than or equal to. That's not really going to probably do much difference for us. And then let's see here, let's try alien4 less than. All right. So, less than. And what else do we have? We've got less than or equal to, and I think we're pretty much done at that point. We'll just exercise all of them just to be extra cautious. All right. Let's run it and make sure that all of these are defined. Okay. There we go. So, alien1 and alien2 are clearly not the same, so they are... That's false that they're equal. Then we also have alien1 compared to alien2, three, which they are also not equal. So, not equal to alien3, that's true that they are not equal. alien4 > alien5, false. alien4 >= to alien5, also false. So, it looks like alien5 is more substantive than alien4 in terms of its Prestige. You can do the little calculations if you want to verify that, but it looks pretty good to me. Looks like everything's working. So, good job everyone. We did a lot of work on this project, didn't we? But it really was, in many ways, pretty straightforward for many of the operator functions that we define. Many of them directly involved Prestige. So, if you were able to get even a couple of these on your own, that's amazing. Regardless, practice makes perfect, as they say. So, if you need to repeat any of the lectures or sections even, that's a perfectly great idea. It's good to keep in practice. In the next lecture, we'll close out this section in our section wrap up. 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.