Download presentation
Presentation is loading. Please wait.
1
Heterogeneous aggregate datatypes
Structs Heterogeneous aggregate datatypes
2
Struct is a Definition Struct defines a new data type
Composed of other types
3
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
4
Initialization Can use initialization list
Order MUST match struct order
5
Copy Assignment does memberwise copy of struct:
6
No other Aggregate Operations
Assignment is the ONLY aggregate operation No >, <, +, cout, cin, etc…
7
No other Aggregate Operations
Do operations based on members:
8
Broken Function This does not work…
9
Broken Function Memory going into function In function
10
Correct Function To modify struct take reference parameter:
Main: Function:
11
Const Ref Struct parameter options Pass by value : make copy
Pass by reference : work with original Pass const reference : work with original, prevent changes
12
Struct Return Function can return struct Declare variable Set values
Return var
13
Return More realistic function:
14
Structs in Structs Can define struct using existing struct:
15
Structs in Structs Values in memory:
16
Fields of Fields Use . multiple times to drill down:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.