Chapter 6 ABSOLUTE C++ Structures and Classes WALTER SAVITCH SECOND EDITION PEARSON Copyright2006 Pearson Addison-Wesley All rights reserved
Chapter 6 Structures and Classes
Learning Objectives ◆Structures ◆Structure types Structures as function arguments Initializing structures ◆Classes Defining,member functions Public and private members Accessor and mutator functions Structures vs.classes Copyright006 Pearson Addison-Wesley.All rights reserved. 6-2
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives ¨ Structures ¨ Structure types ¨ Structures as function arguments ¨ Initializing structures ¨ Classes ¨ Defining, member functions ¨ Public and private members ¨ Accessor and mutator functions ¨ Structures vs. classes
Structures 2nd aggregate data type:struct Recall: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 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 6-3
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-3 Structures ¨ 2nd aggregate data type: struct ¨ Recall: 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
Structure Types ◆Define struct globally(typically) No memory is allocated Just a "placeholder"for what our struct will "look like" ◆Definition: struct CDAccountV1 Name of new struct "type" double balance;member names double interestRate; int term; }; Copyright 2006 Pearson Addison-Wesley.All rights reserved. 6-4
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-4 Structure Types ¨ Define struct globally (typically) ¨ No memory is allocated ¨ Just a "placeholder" for what our struct will "look like" ¨ Definition: struct CDAccountV1 Name of new struct "type" { double balance; member names double interestRate; int term; };
Declare Structure Variable With structure type defined,now declare variables of this new type: CDAccountV1 account; Just like declaring simple types Variable account now of type CDAccount 1 It contains "member values" Each of the struct "parts" Copyright 2006 Pearson Addison-Wesley.All rights reserved. 6-5
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-5 Declare Structure Variable ¨ With structure type defined, now declare variables of this new type: CDAccountV1 account; ¨Just like declaring simple types ¨Variable account now of type CDAccountV1 ¨It contains "member values" ¨Each of the struct "parts