MESSAGE
DATE | 2010-03-09 |
FROM | Ruben Safir
|
SUBJECT | Subject: [NYLXS - HANGOUT] C++ Workshop _ Syntax Basics Structs Unions and
|
-------------------------------------------------------
Now - I've reached a crossroads about what direction I want to continue in. I have a binary choice in front of me, left or right. So I decided to split this down the middle. There are two other aggregate data types, similar to structs, which create new data types when used, one is the "union" and the other is the "class". The problem is, and this is also true of struct in C++, that they are actually very complicated and the class data construction is particularly important and lays out the basis for most advanced C++ object oriented coding. But before I get into a full throttle discussion of classes (and unions), I want to discuss more elementry syntax uses, such as functions, a detailed look at operators, typing syntax and flow control. But I want to be complete.
I've been trying to underscore the point that everything in programming is data. The code you write is data, variables are data, functions are data, and literal data types are data. Data can be stored in C++ in typed variable, anonymous memory segments pointed at by pointers, and referred to through references. structs allow us to keep a lot of data together in a single package (or object) that we can carry around as needed. And although I haven't demonstrated this for structs, one of the types of data that can be packaged in the data type includes functions. This is true for unions, an is essential for classes. Classes, Structs and Unions are really all related. But I don't want to detail the correct constructions of Classes and Unions yet. So I'm presenting a very simplified view of these data constructions, and later we will greatly expand on them.
Unions:
Unions are inherited from C and they are often described as being similar to structs. I'd like to turn this upside down and say that Unions are really nothing like structs. They do share similar syntax, but under the hood they are completely different.
A struct has many members that are packaged together sequentially. They sit in memory likes duck on the pond. With union's we have one member in memory, that can morph to any type that is defined within the construction.
Union's are created similarly as struct's Their basic format is
union { member; member; member; } union_object, union_object;
Again, notice the semicolon at the end of the definition.
Here is a simple example:
union one_at_a_time{ int dataone; int matrix[10]; double dollars1; long double parts_per_billion; };
one_at_a_time myunion; //declare a union of type one_at_a_time
union access is similar to a struct:
myunion1.dataone = 500;
cout << myunion1.dataone <
There is only one, however, data member for myunion. But it can be accessed by any of the members names, which will most likely give you a wrong result. We gave it an integer and if we try to retrive the data as the double (using memeber dollar), we're going to get the binary data for the int 500 intepreted as a double. The size of myunion is the size of the largest data member of the union, in our case the int array matrix. Here is an example of a union and the pitfalls:
#include using namespace std;
int main(int argc, char * argv[]){
union one_at_a_time{ int dataone; int matrix[10]; double dollars1; long double parts_per_billion; };
one_at_a_time myunion1; myunion1.dataone = 500;
cout << myunion1.dataone < //one_at_a_time myunion2; //myunion2.matrix= { ,{6,5,4,3,2,1}}; doesn't work //one_at_a_time myunion2= {6,5,4,3,2,1}; doest work ... you can only //initialize with ONE data at a time
one_at_a_time myunion2; myunion2.matrix[0] = 6; myunion2.matrix[1] = 5; myunion2.matrix[2] = 4; myunion2.matrix[3] = 3; myunion2.matrix[4] = 2; myunion2.matrix[5] = 1; myunion2.matrix[6] = 10;
for(unsigned int i = 0; i < sizeof(myunion2.matrix)/sizeof(int); i++){ cout << myunion2.matrix[i] << endl; }
myunion2.dollars1 = 2.75;
for(unsigned int i = 0; i < sizeof(myunion2)/sizeof(int); i++){ cout << "Dollar1==>" << myunion2.dollars1 << endl; cout << "martix ==>" << myunion2.matrix[i] << endl; } }
-- http://www.mrbrklyn.com - Interesting Stuff http://www.nylxs.com - Leadership Development in Free Software
So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998
http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002
"Yeah - I write Free Software...so SUE ME"
"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."
"> I'm an engineer. I choose the best tool for the job, politics be damned.< You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."
? Copyright for the Digital Millennium
|
|