ITP 165x Lecture 13: Structs, Classes
Document Summary
Itp 165: intro to c++ - lecture 13: structs, classes. A struct (short for structure) allows us to group together related variables into a single variable. So for example, we could create a struct called contact that has: This way, any functions that need to operate on a contact can just be given a contact struct, rather than all the separate variables. Syntax: struct contact { std::string firstname; std::string lastname; std::string phonenumber; Once you declare a struct, the name of the struct can now be used as if it were a type. Once you create an instance of the struct, you can access the variables inside that instance (member variables) using the dot. We can pass a struct to a function by either value or reference. Let"s say we want to add a new member to the struct. So we need to add this field to the struct declaration. Then just use it where it"s appropriate.