MESSAGE
DATE | 2010-03-05 |
FROM | Ruben Safir
|
SUBJECT | Re: [NYLXS - HANGOUT] C++ Workshop _ Syntax Basics
|
The next agregate data type that C++ provides is structs and unions. Structs and Unions have largely been superceded in C++ by Classes. They are inherited from C and are designed as the key multi-type agregrate data type that C uses to create related gruops of data, similar to what a database can provide. Through coding algorithism, their importance have grown far greater than just mear static records, but in this section we will look only at their basic syntax and usage.
The real limitation to arrays is that data must be all of the same type. structs create a new type which contain many of types with in. The basic format of a struct is as follows:
Declaration:
stuct struct_type_name { data type; data tpye; .... }[optional object name];
Notice the semicolon on the end of the struct declaration, which is unusual and particular to struct's in that it is after the curley brace.
structs create a new user defined data type, and after their declaration can be used as any other data type:
Example:
struct birds{ char species[30]; char gender[1]; char color[10]; int size_in_inches; double weight; char diet[30]; };
birds african_grey; birds canary;
or you can make the instances with the declation:
struct birds{ char species[30]; char gender[1]; char color[10]; int size_in_inches; double weight; char diet[30]; } canary, african_grey;
Now it might be noted here an important diference between C and C++. In C, you need the struct keyword to create instances of your struct's.
C EXAMPLE:
struct birds african_grey; struct birds canary;
Otherwise, you need to use typedef in C
typedef birds parrot; parrot african_grey, scarlet_macaw, conour, monk, bundie;
or even use typedef in the declaration:
typedef struct { char species[30]; char gender[1]; char color[10]; int size_in_inches; double weight; char diet[30]; } birds;
birds african_grey; birds canary;
This is a variation of typedef, which we haven't covered. In general typedef creates an alias for a datatype of any kind:
typedef data_type alias;
typedef int BOOL; typedef char[300] buffer;
thus the above example: typedef struct { char species[30]; char gender[1]; char color[10]; int size_in_inches; double weight; char diet[30]; } birds;
is not the same as struct birds { char species[30]; char gender[1]; char color[10]; int size_in_inches; double weight; char diet[30]; } canary, african_grey;
None creates a new data type, "birds" and the other creates new instances of the struct, "canary", "african_grey".
****in C***
struct's can be initialized in C++ as follows, with the use of the assignment operator and curly braces:
birds african_grey = { "African Grey Congo", 'M', "Light Grey", 13, 8.34, "Fruit and Seed" }; birds canary = { "Red Factor Canary", 'M', "Red to Orange", 2, 1.2, "Seed and Grass" };
in C remember to add the struct keyword.
The internal data types are reached by use of the dot operator.
char * color = canary.color; cout << canary.color << endl;
strcpy(canary.color, "Deep Red"); //can not assign a char[]
You can make a pointer to a struct, and this is often useful
birds * canary;
but remember that you have no memory allocated for members yet.
birds *finches, parrot={"Conure", 'F', "Green", 6, 60, "Oranges and Peanuts"};
finches = &parrot;
When you use a pointer, access to members is gained using the infix operaotr "->"
Example:
struct birds{ char species[30]; char gender[1]; char color[10]; int size_in_inches; double weight; char diet[30]; } *finches, parrots = {"Conure",'F',"Yellow",'6',60,"Peanuts and Oranges"};
finches = &parrots;
cout << "Your " << finches->species << "is " << finches->color << endl;
By creating arrays of struct's, large databases of records can be stored in your program
birds finches[100];
strcpy(finches[0].species, "Zebra Finch");
birds *parrots[100];
strcpy(parrot[0]->species, "Amazon");
|
|