## PIC10A Lecture 17

p224-236

### Structures

Sometimes it is useful to have a collection of values of different types and to treat the collection as a single item.
Example
 bank account balance, interest rate student name, student ID, gender rectangle height, width car manufacturer, model, year, license plate, millege

An example of bank account.
 ```#include#includeusing namespace std; struct BankAccount { double balance; double interestRate; }; // get data from the user void getData(BankAccount& account); // calculate the interest double interest(BankAccount account, int year); // display the information void display(BankAccount account); int main() { // magic formula cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); BankAccount myAccount; // declare a BankAccount getData(myAccount); // get data display(myAccount); // display int year = 2; cout << "The interest of your account is " << interest(myAccount, year) << " after " << year << " years.\n"; return 0;}void getData(BankAccount& account) { cout << "Enter account balance: "; cin >> account.balance; cout << "Enter account interest rate: "; cin >> account.interestRate;}double interest(BankAccount account, int year) { return account.balance*pow(1+account.interestRate,year) - account.balance;}void display(BankAccount account) { cout << "Balance: " << account.balance << endl; cout << "Interest Rate: " << account.interestRate << endl;}```

Output
 ```Enter account balance: 5000 Enter account interest rate: 0.05 Balance: 5000.00 Interest Rate: 0.05 The interest of your account is 512.50 after 2 years. ```

Another example: Rectangle
 ```#includeusing namespace std; struct Rectangle { int height; int width; }; // display information void display(Rectangle rect); // return the area of the rect int area(Rectangle rect); int main() { Rectangle rect = {4,5}; display(rect); cout << "The area of the rectangle is " << area(rect) << endl; return 0;}void display(Rectangle rect) { cout << "height: " << rect.height << " " << "width: " << rect.width << endl;}int area(Rectangle rect) { return rect.height*rect.width; } ```

Output
 `height: 4 width: 5The area of the rectangle is 20`

• how to define a structure.
• how to declare a variable as structure.
• how to access members of a structure.
• how to use structure as arguments (call-by-reference or call-by-value) in functions
Let's go through the details.

#### Structure definition

 Syntax ```struct structure_tag { type1 member_variable_1; type2 member_variable_2; ... ... typen member_varialbe_n; } ``` Example `struct BankAccount { double balance; double interestRate;};struct Rectangle { int height; int width;};`
• The keyword `struct` announces that this is a structure type definition.
• The identifier `BankAccount` or `Rectangle` are the names of structure types, which is known as the structure tag.
• The name of the structure tag can be any legal identifier. However, they usually spelled starting with an uppercase letter.
• The variables declared inside the braces { } are called member variables.
• Don't forget the semi-color ; after the brace.
• a structure definition is placed outside any function definition.
• the structure type is then a global definition that is available to all the code follows the structure definition.

#### declaration and initialization

• Once a structure type definition has been given, the structure can be used just like the predefined types `int, char` and so forth.
 Syntax ```structure_tag variable_name ``` Example `BankAccount myAccount;`
• You can initialize a structure at the time that it is declared.
• The initialing values must be given in the order that corresponds to the order of member variables in the structure type definition.
• It is an error if there are more initializer value than `struct` members.
• If there are fewer initializer values than `struct` members, the provided values are used to initialize data members, in order. Each data member without an initializer is initialized to a zero value of an appropriate type for the variable.
 Syntax ```structure_tag variable_name = {member_value_1, member_value_2,... member_value_N}; ``` Example `Rectangle rect = {4,5};` Then the `height` is 4, `width` is 5.

Example
 ```#includeusing namespace std; struct Date { int month; int day; int year; }; // display information void display(Date date); int main() { Date date = {12,5,1972}; display(date); cout << endl; Date date2 = {11,17}; //miss the last value display(date2); cout << endl; return 0; } void display(Date date) { cout << date.month << "/" << date.day << "/" << date.year; } ```

Output
 `12/5/197211/17/0`

#### Members

• A structure variable can hold values just like any other variables can.
• A structure value is a collection of smaller values called member values.
• each structure type has a list of member variables
• Example,
structure member variables
BankAccount balance, interestRate
Rectangle height, width
Date month,day,year
• use dot operator . to access members.
• you can use the members as ordinary variables, i.e. you can use them in arithmetic, assignments, etc etc
The dot operator
 syntax ```structure_variable_name.member_variable_name ``` Example `account.balanceaccount.interestRaterect.heightrect.width`

another example
 ```#includeusing namespace std;struct Date { int month; int day; int year;};// display informationvoid display(Date date);int main() { Date date; date.month = 12; date.day = 5; date.year = 1972; // same as Date date = {12,5,1972}; cout << "Today is "; display(date); cout << endl; date.day++; cout << "Next day is "; display(date); cout << endl; return 0; } void display(Date date) { cout << date.month << "/" << date.day << "/" << date.year; } ```

Output
 `Today is 12/5/1972Next day is 12/6/1972`

#### Copy

• You can copy a structure to another structure.

Example
 ```#includeusing namespace std;struct Date { int month; int day; int year;};// display informationvoid display(Date date);int main() { Date date = {12,5,1972}; display(date); cout << endl; Date date2 = date; display(date2); cout << endl; return 0; } void display(Date date) { cout << date.month << "/" << date.day << "/" << date.year; } ```

Output
 `12/5/197212/5/1972`

#### Structures as function arguments

• A function can have call-by-value parameters of a structure type or call-by-reference parameters of a structure type, or both. Refer to the BankAccount and Rectangle examples.
• you can't change the member variables of a call-by-value parameter in a function.
• you can change the member variables of a call-by-reference parameter in a function.

Comparison:call by value
 ```#includeusing namespace std;struct Date { int month; int day; int year;};// display informationvoid display(Date date);void nextDay(Date date); int main() { Date date = {12,5,1972}; display(date); cout << endl; nextDay(date); cout << "After the function call: \n"; display(date); cout << endl; return 0; } void display(Date date) { cout << date.month << "/" << date.day << "/" << date.year; } void nextDay(Date date) { date.day++; } ```

Output, the date is unchanged
 `12/5/1972After the function call:12/5/1972`

Call-by-reference
 ```#include#includeusing namespace std;struct Date { int month; int day; int year;};// display informationvoid display(Date date);void nextDay(Date& date); int main() { Date date = {12,5,1972}; display(date); cout << endl; nextDay(date); cout << "After the function call: \n"; display(date); cout << endl; return 0; } void display(Date date) { cout << date.month << "/" << date.day << "/" << date.year; } void nextDay(Date& date) { date.day++; } ```

the date is changed
 `12/5/1972After the function call:12/6/1972`