Wonderful job, we’ve successfully created our own data type using structures and modeled dogs, books, and birds in our code!
In this lesson, we covered:
- Structures are a means of modeling real-life objects programmatically.
- How to create a structure using the
structkeyword along with properties and methods.
- Structure properties are used to describe the characteristics and values of the structure.
- To model individual objects, we can create instances of
structs, which may have unique property values.
- We can access and edit properties using dot notation.
- If we know that most of our instances will have a specific property value, we can assign default property values inside the struct.
- Using the
init()method allows us to provide an instance with specific values for the structure’s given properties.
- Even without an
init()method, structs come with a default memberwise initialization method that can assign values to declared properties inside a struct.
- Structures can have methods that are functions accessible to their instances.
- Structures are value types, any copied struct that has its properties altered will not affect the original structure from which it was copied.
If you want to challenge yourself further:
- Use a struct to model something in your everyday life.
- Try nesting a struct within another struct.
- Create a method that manipulates a struct’s own properties.
Take this course for free