< previous page page_779 next page >

Page 779
The left and right braces surround the member list, and a semicolon terminates the struct declaration. Each member name is preceded by the name of a data type, just like the declaration of any variable. Member names must be unique within a struct type, just as variable names must be unique within a block.
Once a struct variable has been declared, the member selectors of the struct variable are treated and used in the same way as any other declared variable. Member selectors can be used in expressions such as
part.quantity = part.quantity + 24;
if (part.cost <= 5.00)
    cout << part.description <<   <<  part.cost;
If the parts wholesaler supplies inventory data that look like
2B3310Ring, piston        2.95    15
then the following program segment would read and store the data into the appropriate members.
cin.get(part.partNumber, 7);
cin.get(part.description, 21);
cin >> part.cost >> part.quantity;
part.partNumber, part.description, part.cost, and part.quantity are the member selectors for the members of the struct variable part.
To complete our initial look at C++ structs, we give a more complete syntax template for a struct type declaration:
0779-01.gif
As you can see in the syntax template, two items are optional: TypeName (the name of the struct type being declared), and a list of variable names between the right brace and the semicolon. Our examples thus far have declared a type name but have not included a variable list. The variable list

 
< previous page page_779 next page >