Chapter 6 Structures

 DEFINING STRUCTURES
Is a way of grouping values of different types together in one variable
Book example: Bank Certificate of Deposit: account balance, interest rate, and length or term.

struct CDAccount
{
    double balance;
    double interest_rate;
    int term;
}; // don’t forget semicolon

keyword struct says defining structure
structure tag: name of structure type CDAccount
member names: identifiers defined inside of braces
Tag and member names can be any legal identifier, tag usually mix of upper and lower case letters (may define part of program with uc)
Definition usually put at beginning of program or in header file so all functions in program can use the type to declare variables:

CDAccount account;

 

ASSIGNING VALUES TO MEMBER VARIABLES
Store member values as the program runs with assignment statements or input stream.
Use structure variable name followed by dot and member name (like calling member function in chapter 5, members are variables this time). Examples:

cin >> account.balance;

account.balance = account.balance + interest;

Can use same member names in different structure types, are NOT the same:

struct NOWAccount
{
    double balance;
    double interest_rate;
    double minimum_bal;
};

CDAccount account1;
NOWAccount account2;

cin >> account1.balance;
cin >> account2.balance;

Can store value in one member variable to another
account1.balance = account2.balance;

even
account1 = oldaccount;        // only if they are the same type

 

STRUCTURES AS FUNCTION ARGUMENTS
Can have call-by-value parameters and/or call-by-reference parameters of a structure type.
Can return a structure type. Example from book:

CDAccount shrink_wrap(double the_balance, double the_rate, int the_term)
{
    CDAccount temp;
    temp.balance = the_balance;
    temp.interest_rate = the_rate;
    temp.term = the_term;
    return temp;
}

if have definition in main:
CDAccount new_account;

call this function:
new_account = shrink_wrap(10000, 5.5, 12);

// Program page 294 in textbook
#include <iostream.h>
struct CDAccount
{
    double bal;
    double int_rate;
    int term;
};

void get_data(CDAccount& the_account);

int main()
{
    CDAccount account;
    double rate_fraction, interest;
 
    get_data(account);
    rate_fraction = account.int_rate/100.0;
    interest = account.bal * rate_fraction * (account.term/12.0);
    account.bal = account.bal + interest;
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    cout << "When your CD matures in " << account.term << "months, \n it will have a balance of $" << account.bal << endl;
    return 0;
}

void get_data(CDAccount& the_account)
{
    cout << "Enter the account balance: $";
    cin >> the_account.bal;
    cout << "Enter the account interest rate: ";
    cin >> the_account.int_rate;
    cout << "Enter the number of months until maturity\n";
    cout << "(must be 12 or fewer months): ";
    cin >> the_account.term;
}

 

HIERARCHICAL STRUCTURES
A structure contains another structure as a member.

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

struct CDAccount
{
    double balance;
    double int_rate;
    Date open_date;
    Date maturity_date;
};

Store something in:

CDAccount NewAcct;
NewAcct.open_date.month = 1;
cin >> NewAcct.maturity_date.month;

 

All material protected by For  questions regarding this web content contact [ProjectEmail].Click HOME to go to main lemetti page.
Graphics Resources-Images and graphics in the Village School Web
Last updated: November 05, 2003.   http://professorlemetti.issmart.com