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 4.11 illustrates these points about a structure. Also it shows how to initialize one.
// structur.cpp -- a simple structure
#include <iostream>
struct inflatable // structure declaration
{
char name[20];
float volume;
double price;
};
int main()
{
using namespace std;
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
// NOTE: some implementations require using
// static inflatable guest =
cout << "Expand your guest list with " << guest.name;
cout << " and " << pal.name << "!
";
// pal.name is the name member of the pal variable
cout << "You can have both for $";
cout << guest.price + pal.price << "!
";
return 0;
}
Here is the output from the program in Listing 4.11:
Expand your guest list with Glorious Gloria and Audacious Arthur!
You can have both for $62.98!
One important matter related to the program in Listing 4.11 is where to place the structure declaration. There are two choices for structur.cpp
. 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. For this program, there is no practical difference between the two choices. But for programs consisting of two or more functions, the difference can be crucial. 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 Figure 4.7).
Variables, too, can be defined internally or externally, with external variables shared among functions. (Chapter 9, “Memory Models and Namespaces,” looks further into that topic.) C++ practices discourage the use of external variables but encourage the use of external structure declarations. Also it often makes sense to declare symbolic constants externally.
Next, notice the initialization procedure:
inflatable guest =
{
"Glorious Gloria", // name value
1.88, // volume value
29.99 // price value
};
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};
You can initialize each member of the structure to the appropriate kind of data. For example, the name
member is a character array, so you can initialize it to a string.
Each structure member is treated as a variable of that type. Thus, pal.price
is a double
variable, and pal.name
is an array of char
. And when the program uses cout
to display pal.name
, it displays the member as a string. By the way, because pal.name
is a character array, we can use subscripts to access individual characters in the array. For example, pal.name[0]
is the character A
. But pal[0]
is meaningless because pal
is a structure, not an array.
18.221.163.13