Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лабораторні, на англійській.doc
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
604.67 Кб
Скачать

1.2 Using a Structure in a Program

Now that we’ve covered some of the main features of structures, it’s time to put the ideas together in a structure-using program. Listing 1 illustrates these points about a structure. Also, it shows how to initialize one.

Listing 1

struct inflatable { // structure declaration

char name[20];

float volume;

double price;

};

void main() {

inflatable guest = {

“Glorious Gloria”, // name value

1.88, // volume value

29.99 // price value

}; // guest is a structure variable of type inflatable

// It’s initialized to the indicated values

inflatable pal = {

“Audacious Arthur”,

3.12,

32.99

}; // pal is a second variable of type inflatable

cout << “Expand your guest list with “ << guest.name;

cout << “ and “ << pal.name << “!\n”; // pal.name is the name member of the

// pal variable

cout << “You can have both for $”;

cout << guest.price + pal.price << “!\n”;

}

Here is the output from the program in Listing 1:

Expand your guest list with Glorious Gloria and Audacious Arthur!

You can have both for $62.98!

1.3 Program Notes

One important matter related to the program in Listing 1 is where to place the structure declaration. You could place the declaration inside the main() function, just after the opening brace. The second choice, and the one made here, is to place it outside and preceding main(). When a declaration occurs outside any function, it’s called an external declaration. The external declaration can be used by all the functions following it, whereas the internal declaration can be used only by the function in which the declaration is found. Most often, you want an external structure declaration so that all the functions can use structures of that type (see Fig. 2).

Fig. 2. Local and external structure declarations

Variables, too, can be defined internally or externally, with external variables shared among functions.

As with arrays, you use a comma-separated list of values enclosed in a pair of braces. The program places one value per line, but you can place them all on the same line. Just remember to separate items with commas:

inflatable duck = {“Daphne”, 0.12, 9.98};

1.4 Other Structure Properties

C++ makes user-defined types as similar as possible to built-in types. For example, you can pass structures as arguments to a function, and you can have a function use a structure as a return value. Also, you can use the assignment operator (=) to assign one structure to another of the same type. Doing so causes each member of one structure to be set to the value of the corresponding member in the other structure, even if the member is an array. This kind of assignment is called memberwise assignment, Listing 2 provides an example.

Listing 2

struct inflatable {

char name[20];

float volume;

double price;

};

void main() {

using namespace std;

inflatable bouquet = {

“sunflowers”,

0.20,

12.49

};

inflatable choice;

cout << “bouquet: “ << bouquet.name << “ for $”;

cout << bouquet.price << endl;

choice = bouquet; // assign one structure to another

cout << “choice: “ << choice.name << “ for $”;

cout << choice.price << endl;

}

Here’s the output from the program in Listing 2:

bouquet: sunflowers for $12.49

choice: sunflowers for $12.49

As you can see, member wise assignment is at work, for the members of the choice structure are assigned the same values stored in the bouquet structure.

You can combine the definition of a structure form with the creation of structure variables. To do so, you follow the closing brace with the variable name or names:

struct perks {

int key_number;

char car[12];

} mr_smith, ms_jones; // two perks variables

You even can initialize a variable you create in this fashion:

struct perks {

int key_number;

char car[12];

} mr_glitz = {

7, // value for mr_glitz.key_number member

“Packard” // value for mr_glitz.car member

};

However, keeping the structure definition separate from the variable declarations usually makes a program easier to read and follow.

Unlike C structures, for example, C++ structures can have member functions in addition to member variables. But these more advanced features most typically are used with classes rather than structures.