OOP in C

This is a discussion on OOP in C within the A Brief History of Cprogramming.com forums, part of the Community Boards category; I have been programming in C and assembly on and off for about 4 1/2 years now and read some ...

  1. #1
    Registered User whistlenm1's Avatar
    Join Date
    Jan 2002
    Posts
    124

    OOP in C

    I have been programming in C and assembly on and off for about 4 1/2 years now
    and read some very interesting articles, one of which I still use to this day
    from Object Magazine by John J. Matsche, here are the rules for Object Oriented
    programming in C.

    BTW - demolinux rocks!!!
    ================================================== ===================================

    Encapsulation:
    1. Declare an object as a typedef struct. In-
    clude declarations for the function pointers.

    2. Always provide an initialization fuction. Manually
    assign the object's methods to the proper function
    pointers (first) within this function.

    3. Include a self pointer to the object as a parameter
    to each of the object's methods.


    Inheritance:
    1. Declare the "guts" of the object struct in a separate
    #define directive. Always include, as the first item
    in the list, the ancestor's definition.

    2. When initializing an object, always call (statically) the
    ancestor's initialization function first.


    Polymorphism:
    1. This is achieved, because all calls to the functions you've
    created for your objects must be done via the function pointers
    in your object.

    ================================================== ====================================
    ================================================== ====================================

    Examples:

    Listing 1.
    /* Specifying a field list */

    #define dwindow \
    int x, y;\
    int (*move)( );\
    int (*draw)( );\
    void (*done)( );

    /* declare the object */

    typedef struct {
    dwindow
    }twindow;

    /* declare a descendant of dwindow */

    #define dmsgbox dwindow \
    char message[8];\
    void (*getmsg)( );

    /* declare descendant object */

    typedef struct {
    dmsgbox
    }tmsgbox;

    Listing 2.
    /* Initializaion function */

    twindow *twindow_init(twindow *self)
    {
    self->move = twindow_move;
    self->draw = twindow_draw;
    self->done = twindow_done;
    /* initialize data or whatever */
    return(self);
    }

    Listing 3.
    /* Initializaton of an ancestor */

    tmsgbox *tmsgbox_init(tmsgbox *self)
    {
    /* call to ancestor init is static */
    twindow_init(self);
    self->getmsg = tmsgbox_getmsg;
    return(self);
    }

    ================================================== ======================================

    Note:
    1. Establish some sort of naming convention when utilizing this, or
    any other, programming technique. Such as, you could precede
    your function (method) name with the name of its respective object
    (such as the above) and underscore if you so choose.

    2. Dynamically allocate and release your objects, and call your functions with
    thy following syntax.

    #include <stdlib.h>
    #include <stdio.h>

    void main( void )
    {

    twindow *twobj;

    twobj = twindow_init(malloc(sizeof(twindow)));
    (*twobj->move)(twobj);
    (*twobj->draw)(twobj);
    (*twobj->done)(twobj);
    free(twobj);
    }
    /* You guys can figure out the rest */

    ================================================== ========================================

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You cannot do encapsulation in c. You cannot stop me accessing the data without going through member funcs because it is public access.You can only approximate encapsulation and hope programmers behave themselves ( they never do!!)
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    *shrug* What's the last time you accessed the elements of a FILE struct?

    And if you were really adamant about it, you could just have the public definition of the struct be an array of chars of the appropriate size, and let the function itself use a cast that only it has access to to treat the struct as a struct. I mean, it's hardly seems worthwhile, but it's still possible.

    Trying to duplicate the syntax of C++ isn't a good idea in C, personally. Many of the things that C++ can handle with it's preprocessor, are handled at run-time here.
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP flu
    By Hussain Hani in forum General Discussions
    Replies: 15
    Last Post: 06-27-2009, 02:02 AM
  2. Should OOP be any new language priority??
    By Hussain Hani in forum General Discussions
    Replies: 80
    Last Post: 06-13-2009, 10:56 AM
  3. Data Mapping and Moving Relationships
    By Mario F. in forum Tech Board
    Replies: 7
    Last Post: 12-14-2006, 09:32 AM
  4. recommendation for a good OOP book
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2006, 04:28 PM
  5. OOP Theory Question
    By Zeusbwr in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2005, 07:37 AM

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