Hello, and welcome to the 44th tutorial on c++ programming series. And in this part we're going to be looking at unions. Unions allow a single area memory to be accessed by multiple data types, but not at the same time. This is useful if you know a group of variables will never be in use together. So storing them in a single area memory is okay, as long as one would override the obey if you were to assign a value to one that would override what you have in memory. So any the previous variable that was being stored there will no longer technically exist.
So let's just show you how to implement unions. The syntax of unions are very similar to struct which we have covered recently, but it's functionally is obviously totally different. So we do use the keyword Union for love about programming, especially c++ didn't Name of what you're doing has obviously a keyword and the keyword is that name. And it sounds pretty obvious but you'll get stuff where it's doesn't really follow any sort of decent convention. Okay, so now you just put the name of your union one's got a number, input curly braces, semicolon at the end here. That is okay.
You basically put all your variables on put into coin, double body. Boom, hello. And now what we're going to do is create an instance of the number union so number one I'm creating an instance called number w sciatic sees a lowercase n current building number dot i equals seven and if we do STD c out no matter I know, you are STD. And long running actuate the console runner and we get the value seven. But if we were to just sorry about that. And if we were to just literally copy and paste this, but we can change this to a tee and it might, it can alter the deal D. And it's going to change this to 6.7.
And that's when we get my product global or because the variable exists now so but if you were to put data in and run it, you can get the value 6.7 and they'll be the same with the Boolean variable Hello. So you can only store one value at any given time, but it's useful. Like I said, if you know that there's a group of variables and none of them will ever be in existence when one of one or more of the other variables is in it. is in existence or in use, then it's great we help to save memory to the only using one area of memory. So that's it for this part we looked at Union the very simple in the next part of this series we'll be looking at enumerators if you have any questions feel free to message us our support systems clearly okay.
Email will be in the description you can comment on these videos directly messages via YouTube. All the required links or source code will also be in the description. And as usual, thanks for watching and I hope you have a nice day.