Transcript from the "Structs" Lesson
>> Now it's going to get more complicated. So far to go, right, because types are not just creating new types or aliases we can create structures and we can create interfaces and here comes the most interesting part. Structures are kind of the replacement of a class. It's just a data type with properties strongly typed.
[00:00:25] A structure, and in fact, in C we have structures as well. It's like you merge a couple of properties under one name. You have the postal address, you have a street, you have street number, apartment number, city, zip code, that's a structure, okay? So it's just a series of properties.
[00:00:47] They do have a constructor that sounds like classes, okay? We'll see how we can add methods to structures because it's a type, okay? And they will have interfaces that we will see in a minute so I don't wanna throw you both at the same time. But with interfaces we can simulate or emulate polymorphism from our P, okay?
[00:01:11] So let's go first with a very simple example. This is how you create a structure. You define a type, then the name of your type, and you use the structure keyword. After that, curly braces, and then a list of properties with identifier and type. As a class, it looks like a class, right?
[00:01:36] And then how do we use that? Well, first you get the variable of type user. And to construct a user, we don't use a new keyword, there is a new keyword going on here, it's a function actually. But we are not going to use it typically. And we can call user that's the name of the type.
[00:01:55] And here comes the maybe the weird part, the odd part, we use curly braces and there are two ways to call the constructor. We can call it property by property or with named properties, where you set each property like a JSON, actually. It looks like a JSON, it's actually JSON-value.
[00:02:42] We will make an example or we will make a front-end master's back-end small solution. It's not going to be back-end yet, just a structure to understand how that works, okay? What about methods? Because I mentioned, I promised you that I can add methods to this. It's gonna be like a class, can we inherit from another structure?
[00:03:06] No, we don't have inheritance. We have something known as embedding that we will see that might simulate some behaviors of that. So you won't like how to define the methods, I guess. Because we are expecting the methods to be defined here within the structure as in classes. And now we define the method the same as the method we did before for distance.
[00:03:31] It's a function outside of the structure of declaration that has that special argument. That's all, we inject a method to the user. And typically we wanna use this here. So there is this ID, this name.