difference between structures and classes

Discussion in 'C Programming' started by nick@no.spam, Aug 14, 2003.

  1. Guest

    I have tried finding an answer to this, but most people just explain
    classes as a more modular way to program. It seems to me that
    (forgetting OO programming which I don't quite understand) the
    structures in C are the same as classes in another language such as C++
    or Java only missing the ability to make the data private.

    I've never had this explained sufficiently and would appreciate a good
    answer. It doesn't need to be Mickey Mouse, but I'm not a programmer either.

    Thanks,
    Nick
    , Aug 14, 2003
    #1
    1. Advertising

  2. Jack Klein Guest

    On Wed, 13 Aug 2003 23:50:43 -0400, wrote in comp.lang.c:

    > I have tried finding an answer to this, but most people just explain
    > classes as a more modular way to program. It seems to me that
    > (forgetting OO programming which I don't quite understand) the
    > structures in C are the same as classes in another language such as C++
    > or Java only missing the ability to make the data private.
    >
    > I've never had this explained sufficiently and would appreciate a good
    > answer. It doesn't need to be Mickey Mouse, but I'm not a programmer either.
    >
    > Thanks,
    > Nick


    C++ and Java are completely off-topic here, as are all other languages
    besides C. The C language doesn't define an interface to any other
    language, or acknowledge their existence.

    In this group, nobody knows what a C++ or Java class is, or what it
    means to make data private.

    If you want comparisons of C and other, later languages derived from C
    to a greater or lesser extent, you need to ask in either a
    non-language specific group like news:comp.programming or in groups
    for those descendent languages such as comp.lang.c++ and
    comp.lang.java. They define their similarities and differences to C,
    and they define linkage to C.

    The information does not go in the other direction.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Aug 14, 2003
    #2
    1. Advertising

  3. wrote:

    > I have tried finding an answer to this, but most people just explain
    > classes as a more modular way to program.


    It is possible, and indeed common, for classes to be a /less/ modular way to
    program.

    > It seems to me that
    > (forgetting OO programming which I don't quite understand) the
    > structures in C are the same as classes in another language such as C++
    > or Java only missing the ability to make the data private.


    Not so. Structures, in C, can easily be used to make data private, if you
    know what you're doing. (No, using the C++ "private" keyword doesn't work
    in C!)

    > I've never had this explained sufficiently and would appreciate a good
    > answer. It doesn't need to be Mickey Mouse, but I'm not a programmer
    > either.


    In C, a struct is a collection of one or more data items, defined like this:

    struct structtypename
    {
    char grill;
    double cross;
    int eresting;
    float downstream;
    long drive;
    };

    The idea is that, when you need to treat this stuff as a group, you can, and
    when you need to get at the insides, you can do that too. And if you want
    to get at the insides, but don't want other people to get at the insides
    (i.e. make the data "private"), it's easy - don't publish the struct
    definition. Just publish the typename:

    struct structtypename;

    and a set of interface functions, one of which delivers a pointer to a new
    object of this type, and the rest of which take a pointer, much like the
    stdio FILE interface functions (fopen, fread, fwrite, fclose, etc). If you
    want more info on these "opaque types", ask for it and I'll cheerfully
    natter on about it for hours.

    <Off-topic>
    By a curious coincidence, C++ structs /can/ be defined in precisely the same
    way, but also have extra goodies which are off-topic here. In C++, there is
    precious little difference between structs and classes, and in fact many
    C++ programmers type "class foo { public:" instead of the exact equivalent
    struct foo {", presumably on the grounds that they enjoy typing.
    </Off-topic>



    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Aug 14, 2003
    #3
  4. On Wed, 13 Aug 2003 wrote:

    > I have tried finding an answer to this, but most people just explain
    > classes as a more modular way to program. It seems to me that
    > (forgetting OO programming which I don't quite understand) the
    > structures in C are the same as classes in another language such as C++
    > or Java only missing the ability to make the data private.
    >
    > I've never had this explained sufficiently and would appreciate a good
    > answer. It doesn't need to be Mickey Mouse, but I'm not a programmer either.


    The comp.lang.c newsgroup is for discussing the C programming language.
    Since there are no classes in C the most I can say is that structures
    exist in C and classes do not.

    I know a little about C++ as well and would suggest you ask the question
    in comp.lang.c++. The C++ language has both structures and classes. They
    should have opinions on what the differences are.

    I am under the general impression that a structure holds a group of
    related data. A class holds a group of related data and methods for
    interacting with that data.

    --
    main(){int j=1234;char t[]=":mad:abcdefghijklmnopqrstuvwxyz.\n",*i=
    "iqgbgxmdbjlgdv.lksrqek.n";char *strchr(const char *,int);while(
    *i){j+=strchr(t,*i++)-t;j%=sizeof t-1;putchar(t[j]);} return 0;}
    Darrell Grainger, Aug 14, 2003
    #4
  5. Derk Gwen Guest

    # I have tried finding an answer to this, but most people just explain
    # classes as a more modular way to program. It seems to me that
    # (forgetting OO programming which I don't quite understand) the
    # structures in C are the same as classes in another language such as C++
    # or Java only missing the ability to make the data private.
    #
    # I've never had this explained sufficiently and would appreciate a good
    # answer. It doesn't need to be Mickey Mouse, but I'm not a programmer either.

    An object class is tightly coupled combination of data and code, operations on
    data are included with and linked to the data. A C structure is typically just
    data; when a C structure includes function pointers, the language does not
    couple the functions to the structure containing them: it is the responsibility
    of the programmer to do so.

    You can simulate single inheritance in C with a disciplined use of functions
    whose first parameters are always, perhaps, a pointer to the structure of data
    and a static structure of function pointers.

    A is a class with data members x, y, z and functions X, Y, Z.
    B is a subclass with data members p, q, r and functions P, Q, X.

    typedef struct A A; typedef struct A_Behaviour A_Behaviour;
    typedef struct B B; typedef struct B_Behaviour B_Behaviour;
    int A_X(A *this,int i);
    int A_Y(A *this,int j);
    int A_Z(A *this,int k);
    int B_P(B *this,int i);
    int B_Q(B *this,int j);
    int B_X(B *this,int k);

    struct A_Behaviour {
    int (*X)(A *this,int i);
    int (*Y)(A *this,int j);
    int (*Z)(A *this,int k);
    };
    struct A {
    Root super;
    A_Behaviour *behaviour;
    int x;
    int y;
    A *z;
    };
    static A_Behavior = {A_X,A_Y,A_Z};
    void new_A(A *a) {
    new_Root(&a->super); a->behaviour = &A_behaviour;
    a->x = 0; a->y = 0; a->z = 0;
    }

    struct B_Behaviour {
    int (*P)(B *this,int i);
    int (*Q)(B *this,int j);
    int (*X)(B *this,int k);
    };
    struct B {
    A super;
    B_Behaviour *behaviour;
    int p;
    A *q;
    B *r;
    };
    static B_Behavior = {B_P,B_Q,B_X};
    void new_B(B *b) {
    new_A(&b->super); b->behaviour = &B_behaviour;
    b->p = 0; b->q = 0; b->r = 0;
    }

    For an object of class B, you call method
    b->P with b->behaviour->P(b,i)
    b->Q with b->behaviour->Q(b,j)
    b->X with b->behaviour->X(b,k)
    b->Y with b->super.behaviour->Y(&b->super,j)
    b->Z with b->super.behaviour->Z(&b->super,k)

    --
    Derk Gwen http://derkgwen.250free.com/html/index.html
    I love the smell of commerce in the morning.
    Derk Gwen, Aug 14, 2003
    #5
  6. Darrell Grainger wrote:

    <snip>

    > I know a little about C++ as well and would suggest you ask the question
    > in comp.lang.c++. The C++ language has both structures and classes. They
    > should have opinions on what the differences are.


    Right.

    >
    > I am under the general impression that a structure holds a group of
    > related data. A class holds a group of related data and methods for
    > interacting with that data.


    So can a struct (in C++). All the more reason to ask C++ questions in a C++
    newsgroup. :)

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Aug 16, 2003
    #6
  7. Malcolm Guest

    <> wrote in message
    > I have tried finding an answer to this, but most people just explain
    > classes as a more modular way to program. It seems to me that
    > (forgetting OO programming which I don't quite understand) the
    > structures in C are the same as classes in another language such as
    > C++ or Java only missing the ability to make the data private.
    >
    > I've never had this explained sufficiently and would appreciate a good
    > answer. It doesn't need to be Mickey Mouse, but I'm not a
    > programmer either.
    >

    Unfortunately you have to understnad Object-orientation to understand what
    C++ is doing with classes.

    At the simplest level, I might define an "image" in C.

    typedef struct
    {
    int width;
    int height;
    unsigned char *pixels;
    } IMAGE;

    Now I would write several functions to manipulate the image, all take an
    IMAGE * as the first parameter

    eg

    void image_setpixel(IMAGE *img, int x, int y, unsigned char r, unsigned char
    g, unsigned char b);

    int image_save(IMAGE *img, FILE *fp);

    etc.

    At the simplest level, C++ classes just change the syntax slightly.

    class Image
    {
    private:
    int width;
    int height;
    unsigned char *pixels;
    public:
    Image(int width, int height); /* constructor */
    ~Image(); /* destructor */
    void SetPixel(int x, int y, unsigned char r, unsigned char g, unsigned
    char b);
    int Save(FILE *fp);
    };

    Arguably the C++ is neater, since you don't have to disambiguate all your
    image_ functions by prefixing them. However there is not much real benefit
    to using C++ if we just have simple objects.

    C++ comes into its own when people say " Hey, a Window on screen could be
    treated as an "image" . Come to think of it, so could a greyscale image -
    when you drew in colour you would translate to luminance. And what about a
    circular image?"

    C++ therefore allows you to derive classes from Image which inherit the
    basic interface, but do different things. A Window image would physically
    put up a pixel on screen when written to, for example, the greyscale would
    translate the colour passed to grey, the circular image would exclude pixels
    drawn outside the circle.

    This is the real advantage of the C++ class - it allows for inheritance
    which is the heart of OO programming.
    Malcolm, Aug 16, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,050
  2. Razvan
    Replies:
    5
    Views:
    11,280
    Dale King
    Jul 27, 2004
  3. Alfonso Morra
    Replies:
    11
    Views:
    703
    Emmanuel Delahaye
    Sep 24, 2005
  4. Ravikiran

    Difference between Unions and Structures...

    Ravikiran, Nov 3, 2008, in forum: C Programming
    Replies:
    7
    Views:
    604
    CBFalconer
    Nov 3, 2008
  5. Ravikiran

    Difference between Unions and Structures...

    Ravikiran, Nov 3, 2008, in forum: C Programming
    Replies:
    2
    Views:
    306
    MJ_India
    Nov 3, 2008
Loading...

Share This Page