Hi
What I'm writing here is by no means the full story, but it hould be enough to get you started and give you a general idea of the differences...
A struct is generally used hen you need to create simple data
structures, e.g. a point that consists of x and y coordinates can be structured as
1 2 3 4 5
|
struct Point
{
int x;
int y;
};
|
If you have declared a Point-variable pt, you can then access the data using e.g.
1 2 3 4
|
Point pt;
pt.x=4;
pt.y=6
|
As you can see, the members of a struct, x and y, are directly accessible - you may think of the members as being "public members" of the struct.
A
class is a combination of member data and methods on the member data. Typically, the class has a public section (containing the methods) and a private section (containing the member data). It may look like
1 2 3 4 5 6 7 8 9 10 11 12
|
class Point
{
public:
Point(int _x, int _y);
int getX();
int getY();
void setX(int _x);
void setY(int _y);
private:
int x;
int y;
};
|
As you can see, the member data is private - this is the default access in a class if you do not specify any. You access data via the methods, e.g. getX() or setY().
A union is a declaration you use when you wish for several variables to share the same memory area. Unions are not used much, but are a personal favourite of mine. An example:
1 2 3 4 5 6
|
union
{
unsigned long int li;
char ch[4];
};
|
The idea is that you can choose which view to have on the used memory - either as a 32-bit thingy or 4 8-bit thingies...
Now, this is the REALLY short story and tries only to contrast the three - not to explain each in turn.
Poke around the web for more info - I found e.g.
http://irc.essex.ac.uk/www.iota-six.co.uk/c/h2_introduction_to_struct.asp