ITP 165x Lecture 13: Structs, Classes

23 views1 pages

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.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents