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 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 6-6
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-6 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
Structure Example: Display 6.1 A Structure Definition (1 of 3) Display 6.1 A Structure Definition 1 //Program to demonstrate the CDAccountV1 structure type. 2 #include <iostream> 3 using namespace std; 4 //Structure for a bank certificate of deposit: An improved version of this struct CDAccountV1 6 structure will be given later in this > double balance; chapter. e double interestRate; 9 int term;//months until maturity 10 11 void getData(CDAccountV1&theAccount); 1 //Postcondition:theAccount.balance,theAccount.interestRate,and 3 //theAccount.term have been given values that the user entered at the keyboar Copyright 2006 Pearson Addison-Wesley.All rights reserved. 6-7
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-7 Structure Example: Display 6.1 A Structure Definition (1 of 3)
Structure Example: Display 6.1 A Structure Definition(2 of 3) 14 int main() 15 16 CDAccountV1 account; 17 getData(account); 18 double rateFraction,interest; 19 rateFraction account.interestRate/100.0; 20 interest account.balance*(rateFraction*(account.term/12.0)); 21 account.balance account.balance interest; 22 cout.setf(ios:fixed); 23 cout.setf(ios:showpoint); 24 cout.precision(2); 25 cout <<"When your CD matures in 26 <account.term <<months,\n" 7 <<"it will have a balance of $ 28 <account.balance <endl; 29 return 0; 30} (continued) Copyright006 Pearson Addison-Wesley.All rights reserved. 6-8
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-8 Structure Example: Display 6.1 A Structure Definition (2 of 3)
Structure Example: Display 6.1 A Structure Definition(3 of 3) Display 6.1 A Structure Definition 31 //Uses iostream: 32 void getData(CDAccountV1&theAccount) 33 34 cout <"Enter account balance:$" 35 cin >theAccount.balance; 36 cout <"Enter account interest rate:" 37 cin >theAccount.interestRate; 38 cout <"Enter the number of months until maturity:" 39 cin >theAccount.term; 40 3 SAMPLE DIALOGUE Enter account balance:$100.00 Enter account interest rate:10.0 Enter the number of months until maturity:6 When your CD matures in 6 months, it will have a balance of $105.00 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 6-9
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-9 Structure Example: Display 6.1 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 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 6-10
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-10 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