tailieunhanh - Chapter 6 Structures and Classes

Structure is collection of different types Class used to combine data and functions into single unit - object, Member variables and member functions Can be public , accessed outside class Can be private , accessed only in a member function’s definition. | 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 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; }; 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 . | 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 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; }; 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" Accessing Structure Members Dot Operator to access members Called "member variables" The "parts" of the structure variable Different structs can have same name member variables No conflicts Structure Example: Display A Structure Definition (1 of 3) Structure Example: Display A Structure Definition (2 of 3) Structure Example: Display A Structure Definition (3 of 3) Structure Pitfall Semicolon after structure definition ; MUST exist: struct WeatherData { double temperature; double windVelocity; }; REQUIRED semicolon! Required since you "can" declare structure variables in this location Structure Assignments Given structure named CropYield Declare two structure variables: CropYield apples, oranges; Both are variables of "struct type CropYield" Simple assignments are legal: apples = oranges; Simply copies each member variable from apples into member variables .

TỪ KHÓA LIÊN QUAN