Structures Unions and classes

Hello,
I am a student in first year of engineering and I have this topic in my semester
I would just want to know the basic difference between the three and when do you use either of them ?
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
Last edited on
From what I understand, struct and classes are custom data types. And the main different between struct and classes is that by default classes have private members. Basically within a class you have specifier, of which you can use to categorize its members. Either as private, protected, or public. They simply either allow or restrict who can use the class members. This is important because it allows C++ to be Object Oriented Language. Which is base on four principles: Encapsulation, Data Hiding, Polymorphism, and Inheritance.

And for myself, I alway uses class. Unless it's something really simple
then I use a struct. And for union, I don't even know what the hell it's for. lolz. I tries to read a lot of people code, and never do I see anyone uses union. O_o
Last edited on
IMHO, a union is not very useful, it *could* be useful in some circumstances, but generally those situations will not come up. I have personally never found any use for them in my code at least.
The only differences I know about are that a class has all members declared as private by default and a struct has them public by default. You can usually (at least I can) do things like operators in a struct.
I've never needed to use a union either...
In C, a struct could only contain data, i.e. you could have no methods or anything. However, in C++ struct basically became a typedef for class except it's members are declared public by default (as said before).
Thank you all very much for replies, especially you corpus for all the pain to explain...
How ever can anyone give me one instance where using a union would actually be considered for programming ?
A good use of a union is in connection with structs and bit fields (at least this is where I have used them). Consider the case where you read a 16-bit register e.g. from a bus controller or the like. Each of the 16 bits have a meaning, e.g. bit 0 = subsystem on, bits 1-7 = subsystem state, bits 8-15 = subsystem error. You may then define a bit field to represent this as
1
2
3
4
5
6
typedef struct SubsystemInfoBits
{
  unsigned char ss_on          : 1;  // bit 0
  unsigned char ss_status    : 7;  // bits 1-7
  unsigned char ss_error;            // is already 8 bits
};


You can then use this struct in a union:

1
2
3
4
5
typedef union SubsystemInfo
{
  SubsystemInfoBits infoAsBits;
  unsigned short infoAsWord;
};


So far so good. The real trick is when you need to read the register information and interpret it: You can write it into the struct as a word, but read it as bits:

1
2
3
4
5
6
7
8
9
SubsystemInfo ssi;
ssi.infoAsWord = readRegisterValue();   // Write the 16-bit register value to the union
if(ssi.infoAsBits.ss_status != GOOD_STATUS)
{
  switch(ssi.infoAsBits.ss_error)
  {
    ...
   }
}


Did that make sense?

Last edited on
Topic archived. No new replies allowed.