[Music] what is going on guys welcome back to the c plus plus tutorial series in today's video we're going to talk about struct so let us get right into it all right so as the name already suggests structs are used to represent data structures in c plus the basic idea is that you have a struct and you initialize it by saying struct and then a name i don't know my struct and then you say this is my structure ended up with a semicolon and the idea behind the struct is that you combine multiple things into one structure so for example instead of saying okay i have an integer and a string and a boolean and they somehow relate to each other you say i have a struct and this struct consists of an integer i for example an std string s for example and a boolean b for example this just something here now this structure is one thing it's one data structure and in c plus it is a little bit like a class and an object so for those of you who already know object oriented programming from from another language like python or java for example or c sharp uh structs in c plus plus are almost like classes now there are some differences for example the default um the the the members are public by default instruct it's not like that for a class um and also it's just not considered best practice to use structs for things that you would actually want to use classes and objects for so the the basic um rule of thumb that you want to follow here is if you want to represent a data structure a classic traditional data structure or something like that you use a struct and if you want to actually represent something like an object like a human like a book and so on you go for a class and an object but this is not the topic of today's video so structs and c plus are much more like uh classes whereas in c structs are really just data structures you cannot go ahead and see and say i have a void here which is the method or the function test and this function goes ahead and see out test like that so this is not something that you could do in c at least as far as i know you can point to a function and so on but you cannot just have this function belongs to that struct and so on um so what we can try here is we can just go ahead and say struct my struct i'm not even sure if we need the struct to be honest but i think we need it yes m1 let's call it m1 and then we say m1 dot i equals 20 and m one dot s equals hello and then we have b uh m one dot b equals uh true like that and then we can just go ahead and say std c out m1i std end line and then we copy that and we print the individual values and last but not least we can also go ahead and say m1.test so we call the function from this struct so when we now compile this and run this what do we get here no member named b uh what is the problem here oh it's a boolean obviously this is not java so bool in c plus and then there you go so we have 20 hello one and test one represents true um and now the interesting thing is that those are not this is not the same as saying in i and string s and so on those values belong to this particular structure so this structure m1 consists of an integer a string a boolean and a function and we can create another one we can say struct my struct m2 and we can try to print those values for m2 oh sorry in addition to that i can try to do that and you're going to see that we're going to get uh different values or undefined values in fact because we don't have anything assigned yet um so those are individual structures and we can manipulate them so the values of m1 are the values of m1 and the values of m2 are the values of m2 now the function is the same in this case so if i say m2 test we're going to get the same result here but if the function test for example prints uh information of the struct so if i say okay print i s and b here i'm going to get a different result for each struct now let's go ahead and do something useful let's not do my struct let's do a person so we can say struck person again as i said person is usually something that you would want to model in a class so in object oriented programming but just for an example we can use a struct for this as well uh especially because this is useful if you're doing something in c or even in go and go you also don't have classes you only have structures uh so you can get used to that as well here uh we can create a new human or a new person we can say struct person p1 and this person shall have a name so std string name then we have s not std int h and let's say we have character gender like that and now what we do is we go ahead and say p1 dot name equals max p1.h equals 25 and p1 dot gender equals m for male and we can copy this and have a second person here and this is going to be anna she is 35 and f for female of course we need to change this to two here there you go and now we can also add a function here that just prints the info so we can say something like um void print info and what we do here is we just say stdc out name i mean yes i could use formatting but i'm going to do it like that right now um i hope i don't need something like this here but i think not but we'll find out in a second h h and gender gender let's see if that works we now just need to call this p1 dot print info and then the same thing for p2 so as you can see it works it can uh it can access the individual attributes uh so this works as you can see more like a class and see that's not possible you cannot just do that and see as far as i know maybe i'm wrong but as far as i know that you cannot do that in c in c plus plus that's not a problem and a struct can be treated like a class again it's not it's not best practice uh but one more thing that we can do here is we can look at the size so we can also go ahead and say stdc out size off p1 just to see how much or how many bytes this memory uh this structure allocates in the memory so we can run this here and we're going to see 32 bytes and i think for p2 it's going to be the same obviously i mean hopefully because i think maybe the name i think the name shouldn't make much of a difference here maybe if i add a lot of ace but even then i think that the string has already allocated more than it needs yeah uh but we can see that there is a change if we add uh something else here so let's say i don't know we have a float weight or something even if we don't assign it we still have a float and because of that we have four more bites here so that's it for this video if you enjoyed i hope you learned something if so let me know by hitting the like button leave a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you very much for watching see you next video and bye [Music] you