Heterogeneous aggregate datatypes Structs Heterogeneous aggregate datatypes
Struct is a Definition Struct defines a new data type Composed of other types
Members Members : component parts of struct Member access operator : . variableName.member Address Identifier Value 16 15 s1.Name "Bob Smith" 14 13 12 11 10 9 8 7 s1.quizAvg 84 6 5 4 3 s1.labAvg 92 2 1
Initialization Can use initialization list Order MUST match struct order
Copy Assignment does memberwise copy of struct:
No other Aggregate Operations Assignment is the ONLY aggregate operation No >, <, +, cout, cin, etc…
No other Aggregate Operations Do operations based on members:
Broken Function This does not work…
Broken Function Memory going into function In function
Correct Function To modify struct take reference parameter: Main: Function:
Const Ref Struct parameter options Pass by value : make copy Pass by reference : work with original Pass const reference : work with original, prevent changes
Struct Return Function can return struct Declare variable Set values Return var
Return More realistic function:
Structs in Structs Can define struct using existing struct:
Structs in Structs Values in memory:
Fields of Fields Use . multiple times to drill down: