Monday, November 29, 2010

Structure in c++

Structures
A structure is a collection of simple variables. The variables in a structure can be of different
types: Some can be int, some can be float, and so on. (This is unlike the array, which we’ll
meet later, in which all the variables must be the same type.) The data items in a structure are
called the members of the structure.
In books on C programming, structures are often considered an advanced feature and are introduced
toward the end of the book. However, for C++ programmers, structures are one of the
two important building blocks in the understanding of objects and classes. In fact, the syntax of
a structure is almost identical to that of a class. A structure (as typically used) is a collection of
data, while a class is a collection of both data and functions. So by learning about structures
we’ll be paving the way for an understanding of classes and objects. Structures in C++ (and C)
serve a similar purpose to records in some other languages such as Pascal.
A Simple Structure
Let’s start off with a structure that contains three variables: two integers and a floating-point
number. This structure represents an item in a widget company’s parts inventory. The structure
is a kind of blueprint specifying what information is necessary for a single part. The company
makes several kinds of widgets, so the widget model number is the first member of the structure.
The number of the part itself is the next member, and the final member is the part’s cost.
(Those of you who consider part numbers unexciting need to open your eyes to the romance of
commerce.)
The program PARTS defines the structure part, defines a structure variable of that type called
part1, assigns values to its members, and then displays these values.
// parts.cpp
// uses parts inventory to demonstrate structures
#include <iostream>
using namespace std;

////////////////////////////////////////////////////////////////
struct part //declare a structure
{
int modelnumber; //ID number of widget
int partnumber; //ID number of widget part
float cost; //cost of part
};
////////////////////////////////////////////////////////////////
int main()
{
part part1; //define a structure variable
part1.modelnumber = 6244; //give values to structure members
part1.partnumber = 373;
part1.cost = 217.55F;
//display structure members
cout << “Model “ << part1.modelnumber;
cout << “, part “ << part1.partnumber;
cout << “, costs $” << part1.cost << endl;
return 0;
}
The program’s output looks like this:
Model 6244, part 373, costs $217.55
The PARTS program has three main aspects: defining the structure, defining a structure variable,
and accessing the members of the structure. Let’s look at each of these.
Defining the Structure
The structure definition tells how the structure is organized: It specifies what members the
structure will have. Here it is:
struct part
{
int modelnumber;
int partnumber;
float cost;
};
Syntax of the Structure Definition
The keyword struct introduces the structure definition. Next comes the structure name or tag,
which is part. The declarations of the structure members—modelnumber, partnumber, and
cost—are enclosed in braces. A semicolon follows the closing brace, terminating the entire
Structures


Use of the Structure Definition
The structure definitiondefinition serves only as a blueprint for the creation of variables of type
part. It does not itself create any structure variables; that is, it does not set aside any space in
memory or even name any variables. This is unlike the definition of a simple variable, which
does set aside memory. A structure definition is merely a specification for how structure variables
will look when they are defined. This is shown in Figure 4.2.
It’s not accidental that this description sounds like the distinction we noted between classes
and objects in Chapter 1, “The Big Picture.” As we’ll see, an object has the same relationship
to its class that a variable of a structure type has to the structure definition.
Defining a Structure Variable
The first statement in main()
part part1;
defines a variable, called part1, of type structure part. This definition reserves space in
memory for part1. How much space? Enough to hold all the members of part1—namely
modelnumber, partnumber, and cost. In this case there will be 4 bytes for each of the two ints
(assuming a 32-bit system), and 4 bytes for the float. Figure 4.3 shows how part1 looks in
memory. (The figure shows 2-byte integers.)

Initializing Structure Members
The next example shows how structure members can be initialized when the structure variable
is defined. It also demonstrates that you can have more than one variable of a given structure
type (we hope you suspected this all along).
Here’s the listing for PARTINIT:
// partinit.cpp
// shows initialization of structure variables
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
struct part //specify a structure
{
int modelnumber; //ID number of widget
int partnumber; //ID number of widget part
float cost; //cost of part
};
////////////////////////////////////////////////////////////////
int main()
{ //initialize variable
part part1 = { 6244, 373, 217.55F };
part part2; //define variable
//display first variable
cout << “Model “ << part1.modelnumber;
cout << “, part “ << part1.partnumber;
cout << “, costs $” << part1.cost << endl;
part2 = part1; //assign first variable to second
//display second variable
cout << “Model “ << part2.modelnumber;
cout << “, part “ << part2.partnumber;
cout << “, costs $” << part2.cost << endl;
return 0;
}
This program defines two variables of type part: part1 and part2. It initializes part1, prints
out the values of its members, assigns part1 to part2, and prints out its members.
Here’s the output:
Model 6244, part 373, costs $217.55
Model 6244, part 373, costs $217.55
Not surprisingly, the same output is repeated since one variable is made equal to the other.
The part1 structure variable’s members are initialized when the variable is defined:
part part1 = { 6244, 373, 217.55 };

No comments:

Post a Comment