You like some sort of data form that could hold all this information in one unit.
An array won't do. Because an array can only hold data from the same data type.
The answer to your desire is the C++ structure.
A structure is a more versatile data form than an array because a single structure can hold items of more then one data type.
If you want to keep track of a whole team, you can use an array of structures.
The structure type is a stepping stone to C++ OOP, the class. Learning a little about structures now takes you that much closer to the OOP heart of C++.
struct inflatable // structure declaration
{
char name[20];
float volume;
double price;
};
The keyword struct indicates that the code defines the layout for a structure.
The identifier inflatable is the name, or tag, for this form; this makes inflatable the name for the new type. Thus now you can create variables of type inflatable just as you create variables of type char or int.
Next, between braces are the list of data types to be held in the structure.
Each list item is a declaration statement. You can use any of the C++ types here, including arrays and other structures. This example uses an array of char, which is suitable for storing a string, a float and a double. Each individual item in the list is called a structure member, so the inflatable structure has three members.
After you have the template, you can create variables of that type:
inflatable hat; // hat is a structure variable of type inflatable
inflatable woopie_cushion; // type inflatable variable
inflatable mainframe; // type inflatable variable
In C++ the structure tag is used just like a fundamental type name.
Given that hat is type inflatable, you use the membership operator (.) to access individual members.
For example hat.volume refers to the volume member of the structure, and hat.price refers to the price member.
Because the price member is declared as type double, hat.price and vincent.price are both equivalent to the type double variables and can be used in any manner an ordinary type double variable can be used.
In short, hat is a structure, but hat.price is a double.
By the way, the method used to access class member functions such as cin.getline() has its origins in the method used to access structure member variables such as vincent.price
NOTE: One important matter related to using a structure, is where to put the structure declaration in your program.
There are two choiches, you can place it in the main() function just after the opening brace, or you place it outside of main.
When you declare something outside any function it is called external declaration. The external declaration can be used by all functions following it.
Whereas the internal declaration can only be used by the function in which the declaration is found!
Most often you declare a struct external, so that all the functions can use structures of that type.
Variables, too, can be defined internally or externally, with external variables shared among functions.
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.
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. You can also place the values on one line:
inflatable duck = {"Daphne", 0.12, 9.98};
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 you use 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!
Can a structure use a string Class member?
With the latest compilers yes!
#include
struct inflatable // structure template
{
std::string name;
float volume;
double price;
};
You can also use the using directive so that it is above the the strucure declaration. using namespace std;
Other Structure Properties
C++ makes user-defined types as similar as possible to build-in types.
You can pass structures as arguments to a function.
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.
You can combine the definition of a structure form with the creation of structure variables.
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,
"Packard"
};
However, keeping the structure definition separate from the variable declarations usually makes a program easier to read and follow.
Another thing you can do with structures is create a structure with no type name!
struct // no tag
{
int x; // 2 members
int y;
}; position; // a structure variable
This creates one structure variable called position.
You can access its members with the membership operator as in position.x but there is no general name for the type. You can NOT subsequently create other variables of the same type.
C++ structs go further then C-type structs, they can even contain member functions, that will be covered later.
Arrays of Structures
It is possible to create arrays whose elements are structures. The technique is exactly the same as for creating arrays of the fundamental types.
inflatable gifts[100]; // array of 100 inflatable structures
This makes gifts an array of inflatables, Hence each element of the array such as gifts[0] or gifts[99], is an inflatable object and can be used with the membership operator.
cin >> gifts[0].volume; // use volume member of first struct
cout << gifts[99].price << endl; // display price member of last struct
KEEP in mind that gifts itself is an array, not a structure, so constructions shuch as gifts.price are not valid.
To initialize an array of structures, you combine the rule for initializing arrays, a brace enclosed , comma-separated list of values for each element. With the rule of structures a brace enclosed, comma-seperated list of values for each member.
inflatable guest[2] = // initializing an array of structs
{
{"Bambi", 0.5, 21.99}, // first structure in array
{"Godzilla", 2000, 565.99} // next structure in array
};
As usual, you can format this the way you like. For example initialize the array on one line.
Bit Fields in Structures
C++, like C enables you to specify members that occcupy a particular number of bits. This can be handy for creating a data structure that corresponds, say to a register on some hardware device.
The field should be an integral or enumerationm type(which is explained somewhere else later.)
amd a colon followed by a number indicates the actual number of bits to be used. You can use unamed fields to provide spacing. Each member is termed a bit field. Here is the example:
struct torgle_register
{
unsigned int SN : 4; // 4 bits for SN value
unsigned int : 4; // 4 bits unused
bool goodIn : 1; // valid input (1 bit)
bool goodTorgle : 1; // successful torgling
};
You can initialize the fields in the usual manner, and you use the standard structure notation to access bit fields:
torgle_register tr = { 14, true, false };
if (tr.goodIn) // if statement covered in a later blog.
Bit fields are typically used in low-level programming. Often, using an integral type and the bitwise operators. bitwise operators not yet covered and I don't know if I will do that too, just buy a book, mostly they are in the appendix.
Geen opmerkingen:
Een reactie posten