Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Structures  Structure types  Pointers and structure types  Structures.

Similar presentations


Presentation on theme: "Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Structures  Structure types  Pointers and structure types  Structures."— Presentation transcript:

1 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Structures  Structure types  Pointers and structure types  Structures as function arguments

2 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  Aggregate data type: struct  Aggregate meaning "grouping"  Recall array: collection of values of same type  Structure: collection of values of different types  Treated as a single item, like arrays  Major difference: Must first "define" struct  Prior to declaring any variables

3 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-3 Structure Types  Define struct globally (typically)  No memory is allocated  Just a "placeholder" for what our struct will "look like"  Definition: struct CDAccount  Name of new struct "type" { double balance;  member names double interestRate; int term; };

4 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-4 Declare Structure Variable  With structure type defined, now declare variables of this new type: CDAccount account_1;  Just like declaring simple types  Variable account now of type CDAccount  It contains "member values"  Each of the struct "parts"

5 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-5 Accessing Structure Members  Dot Operator to access members  account.balance  account.interestRate  account.term  Called "member variables"  The "parts" of the structure variable  Different structs can have same name member variables  No conflicts

6 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-6 Structure Pitfall  Semicolon after structure definition  ; MUST exist: struct WeatherData { double temperature; double windVelocity; };  REQUIRED semicolon!

7 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-7 Structure Assignments  Simple assignments are legal between instances of same structure: apples = oranges;  Simply copies each member variable from apples into member variables from oranges  Sometime confusing, not recommend to use  Assign field by field, more accurate and clear  apple.price = oragnge.price;  apple.producer = orange.produce;

8 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-8 Initializing Structures  Can initialize at declaration  Example: struct Date { int month; int day; int year; }; Date dueDate = {12, 31, 2003};  Declaration provides initial data to all three member variables  Not commonly used

9 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-9 Initializing Structures (continued)  Initialize after declaration (more commenly used) struct Date { int month; int day; int year; }; Date dueDate; dueDate.month =12; dueDate.day = 31; Duedate.year = 2003;

10 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-10 Using Pointers with Struct  The -> operator  Shorthand notation  Combines dereference operator, *, and dot operator  Specifies member of class "pointed to" by given pointer

11 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-11 Using Pointers with Struct (continued) struct Date { int month; int day; int year; }; Date dueDate; Date * P = &dueDate P->month =12; P->day = 31; P->year = 2003;

12 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-12 Using Pointers with Struct (continued) struct Date { int month; int day; int year; }; Date * P = new Date; P->month =12; P->day = 31; P->year = 2003;

13 Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-13 Structures as Function Arguments  Passed like any simple data type  Pass-by-value  Pass-by-reference  Pass-by-pointer  Can also be returned by function  Return-type is structure type  Return statement in function definition sends structure variable back to caller


Download ppt "Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Structures  Structure types  Pointers and structure types  Structures."

Similar presentations


Ads by Google