PIC10A Lecture 17

Reading

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<iostream>
#include<cmath>
using 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
#include<iostream>
using 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: 5
The area of the rectangle is 20
After reading the examples, you should have some basic idea about

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;
};

declaration and initialization

Syntax
structure_tag variable_name
      
Example
BankAccount myAccount;
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
#include<iostream>
using 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/1972
11/17/0

Members

The dot operator
syntax
structure_variable_name.member_variable_name
      
Example
account.balance
account.interestRate
rect.height
rect.width

another example
#include<iostream>
using namespace std;

struct Date {
int month;
int day;
int year;
};

// display information
void 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/1972
Next day is 12/6/1972

Copy


Example
#include<iostream>
using 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 = date; display(date2); cout << endl; return 0; } void display(Date date) { cout << date.month << "/" << date.day << "/" << date.year; }

Output
12/5/1972
12/5/1972

Structures as function arguments


Comparison:call by value
#include<iostream>
using namespace std;

struct Date {
int month;
int day;
int year;
};

// display information
void 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/1972
After the function call:
12/5/1972

Call-by-reference
#include<iostream>
#include<iostream>
using namespace std;

struct Date {
int month;
int day;
int year;
};

// display information
void 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/1972
After the function call:
12/6/1972