Structures, Unions and Classes

This is a discussion on Structures, Unions and Classes within the C++ Programming forums, part of the General Programming Boards category; I'm confused with classes, unions and structures. I already read them in books but still I'm confused. Can anybody enlighten ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    14

    Structures, Unions and Classes

    I'm confused with classes, unions and structures. I already read them in books but still I'm confused. Can anybody enlighten me?
    Flamers are worst than Newbies

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    In C++, struct == class except that stucts are public by default, while classes are private. They are basically a collection of dataypes mixed together.

    The members of a union share the same memory, so if you set one of the members you modify the others too.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Keep reading books and the web. It's difficult to explain this stuff in a few sentances.

    UNION - I assume that unions are supposed to be used to make efficient use of memory. I've personally never seen unions used in a real program, only in books... I'm not saying that unions are never used... probably rarely used.

    STRUCTURE - Magos is right, there is little difference between structures and classes in C++. However, typically structures do not have member functions. This is because in C, a structure cannot have member functions. (There are no classes or member functions in C.)

    You can have a structure called Employee, and specific instances of that structure like Makoy and DougDbug. These structures can have variables like:

    Makoy.Salary
    DougDbug.Salary
    Makoy.Title
    Doug.Title

    CLASS - In object oriented programming, a specific instance of a class is an object. Makoy & DougDbug are objects. You can have the same member variables like:

    Makoy.Salary
    Makoy.Title

    In addition, you can have member functions like:
    Makoy.IncreaseSalary()

    You cannot have functions like that in non-object oriented programming languages such as C. In C, functions would have to be outside the structure. You would have to pass a pointer to the structure into your IncreaseSalary() function. You would not have encapsulation.

    You can avoid structures, and always use classes if you wish.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. classes, structs and unions
    By Luciferek in forum C++ Programming
    Replies: 24
    Last Post: 08-09-2008, 10:26 AM
  2. Memory Order in Classes
    By skewray in forum C++ Programming
    Replies: 13
    Last Post: 12-14-2006, 05:40 AM
  3. unions problems!
    By nextus in forum C++ Programming
    Replies: 3
    Last Post: 01-12-2003, 03:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21