Section 3.6 User defined data types | ||
We have already seen a data type that is defined by the user (programmer): the structures. But in addition to these there are other kinds of user defined data types:
typedef existing_type new_type_name ;where existing_type is a C++ fundamental or any other defined type and new_type_name is the name that the new type we are going to define will receive. For example:
typedef char C;In this case we have defined four new data types: C, WORD, string_t and field as char, unsigned int, char* and char[50] respectively, that we could perfectly use later as valid types:
typedef unsigned int WORD;
typedef char * string_t;
typedef char field [50];
C achar, anotherchar, *ptchar1;typedef can be useful to define a type that is repeatedly used within a program and it is possible that we will need to change it in a later version, or if a type you want to use has too long a name and you want it to be shorter.
WORD myword;
string_t ptchar2;
field name;
All the elements of the union declaration occupy the same space of memory. Its size is the one of the greatest element of the declaration. For example:union model_name { type1 element1; type2 element2; type3 element3; . . } object_name;
defines three elements:union mytypes_t { char c; int i; float f; } mytypes;
mytypes.ceach one of a different data type. Since all of them are referring to a same location in memory, the modification of one of the elements will afect the value of all of them.
mytypes.i
mytypes.f
One of the uses a union may have is to unite an elementary type with an array or structures of smaller elements. For example,
defines three names that allow us to access the same group of 4 bytes: mix.l, mix.s and mix.c and which we can use according to how we want to access it, as long, short or char respectively. I have mixed types, arrays and structures in the union so that you can see the different ways that we can access the data:union mix_t{ long l; struct { short hi; short lo; } s; char c[4]; } mix;
union | anonymous union |
struct { char title[50]; char author[50]; union { float dollars; int yens; } price; } book; |
struct { char title[50]; char author[50]; union { float dollars; int yens; }; } book; |
The only difference between the two pieces of code is that in the first one we gave a name to the union (price) and in the second we did not. The difference is when accessing members dollars and yens of an object. In the first case it would be:
book.price.dollarswhereas in the second it would be:
book.price.yens
book.dollarsOnce again I remind you that because it is a union, the fields dollars and yens occupy the same space in the memory so they cannot be used to store two different values. That means that you can include a price in dollars or yens, but not both.
book.yens
For example, we could create a new type of variable called color to store colors with the following declaration:enum model_name { value1, value2, value3, . . } object_name;
enum colors_t {black, blue, green, cyan, red, purple, yellow, white};Notice that we do not include any fundamental data type in the declaration. To say it another way, we have created a new data type without it being based on any existing one: the type color_t, whose possible values are the colors that we have enclosed within curly brackets {}. For example, once declared the colors_t enumeration in the following expressions will be valid:
colors_t mycolor;In fact our enumerated data type is compiled as an integer and its possible values are any type of integer constant specified. If it is not specified, the integer value equivalent to the first possible value is 0 and the following ones follow a +1 progression. Thus, in our data type colors_t that we defined before, black would be equivalent to 0, blue would be equivalent to 1, green to 2 and so on.
mycolor = blue;
if (mycolor == green) mycolor = red;
If we explicitly specify an integer value for some of the possible values of our enumerated type (for example the first one) the following values will be the increases of this, for example:
in this case, variable y2k of the enumerated type months_t can contain any of the 12 possible values that go from january to december and that are equivalent to values between 1 and 12, not between 0 and 11 since we have made january equal to 1.enum months_t { january=1, february, march, april, may, june, july, august, september, october, november, december} y2k;
© The C++ Resources Network, 2000-2003 - All rights reserved |
Previous: 3-5. Structures |
index |
Next: 4-1. Classes. |