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";
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!
Program Notes
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
Figure 4.7. Local and external structure declarations.
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.
C++11 Structure Initialization
As with arrays, C++11 extends the features of list-initialization. The = sign is optional:
inflatable duck {"Daphne", 0.12, 9.98}; // can omit the = in C++11
Next, empty braces result in the individual members being set to 0. For example, the following declaration results in mayor.volume and mayor.price being set to 0 and all the bytes in mayor.name being set to 0:
inflatable mayor {};
Finally, narrowing is not allowed.
Can a Structure Use a string Class Member?
Can you use a string class object instead of a character array for the name member? That is, can you declare a structure like this:
#include
struct inflatable // structure definition
{
std::string name;
float volume;
double price;
};
The answer is yes unless you are using an obsolete compiler that does not support initialization of structures with string class members.
Make sure that the structure definition has access to the std namespace. You can do this by moving the using directive so that it is above the structure definition. The better choice, as shown previously, is to declare name as having type std::string.
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
Listing 4.12. assgn_st.cpp
// assgn_st.cpp -- assigning structures
#include
struct inflatable
{
char name[20];
float volume;
double price;
};
int main()
{
using namespace std;
inflatable bouquet =
{
"sunflowers",
0.20,
12.49
};
inflatable choice;