Structures for Diverse Data

  1. The use of a data type called structures allows a collection of values possibly of different types to be treated as a single item.

  2. The syntax for defining a structure data type is:

    struct StructureName
    {
        Type1 MemberVariableName1;
        Type2 MemberVariableName2;
        ... ......................
        TypeN MemberVariableNameN;
    };

    where struct is a C++ reserve word to specify the structure data type. StructureName is the name of the structure type. MemberVariableName1 is the name of its member variable and Type1 is the variable type, etc. The body of the structure definition consists of declarations of its member variables.

  3. The value of a structure variable is the collection of values of its member variables.

  4. For example in considering a bank certificate of deposit (CD), we want to know the amount of money invested, that is its balance, the annual interest rate, the term of the CD (the number of month until maturity), and whether the CD is FDIC insured. A structure type that describes the CD may be defined as follows:

    struct CDAccount
    {
        double balance;
        double interest_rate;
        int term;
        bool isFDICinsured;    // Federal deposit insurance protects the first
                                              // $100000 of deposits in the US
    };

  5. A structure is usually placed before any function declaration so that the structure type is available to all the functions, including the main.

  6. Once a structure type definition has been given, variables of the structure type can be declared just like the way variables of predefined types, int, char, etc. are declared.

  7. For example the following will declare two variables, named my_acount and your_account, both of type CDAccount:

    CDAccount my_account, your_account;

  8. Member variables of a given structure variable are specified by giving the name of the structure variable followed by a dot and the member variable name. For example my_account.balance refers to the balance of the CD account called my_account, and your_account.term refers to the term of the CD account called your_account.

  9. A struct can also be used as a parameter of a function, as the following example shows. Notice that the parameter is passed as a call-by-reference parameter. You can also choose to use a structure variable as a call-by-value parameter (not recommended because of efficiency reasons).

  10. //Program to demonstrate the CDAccount structure type.
    #include <iostream>
    using namespace std;

    //Structure for a bank certificate of deposit:
    struct CDAccount
    {
        double balance;
        double interest_rate;
        int term;   //months until maturity
    };


    void get_data(CDAccount& the_account);
    //Precondition: the_account must have been declared.
    //Postcondition: the_account.balance, the_account.interest_rate,
    //and the_account.term
    //have been given values that the user entered at the keyboard.


    int main( )
    {
        CDAccount account;
        get_data(account);

        double rate_fraction, interest;
        rate_fraction = account.interest_rate/100.0;
        interest = account.balance*rate_fraction*(account.term/12.0);
        account.balance = account.balance + 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.balance << endl;
        return 0;
    }

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

  11. The assignment operator can be used on structure variables. For example the assignment statement:

    my_account = your_account;

    is equivalent to the following three assignment statements:

    my_account.balance = your_account.balance;
    my_account.interest_rate = your_account.interest_rate;
    my_account.term = your_account.term;

  12. In a structure definition, one has the option of declaring structure variables:

    struct WeatherData
    {
    double temperature;
    double wind_velocity;
    }
    JFK_Airport, Central_park;

    Note that the semicolon at the end of the structure definition is needed even if you do not declare structure variables like this.