Object orientation.....

Discussion in 'C Programming' started by Vishal Naidu, Sep 1, 2005.

  1. Vishal Naidu

    Vishal Naidu Guest

    i m a college student in my second year.....
    my queston is..
    is it really possible to write object oriented code in C ?
    and if yes how do we achieve abstration, polymorhism , hierarchy etc.
    in C
     
    Vishal Naidu, Sep 1, 2005
    #1
    1. Advertising

  2. Vishal Naidu said:

    > i m a college student in my second year.....
    > my queston is..
    > is it really possible to write object oriented code in C ?


    Yes. Similarly, it is really possible to build a life-sized replica of the
    Statue of Liberty using nothing but matchsticks and chewing gum.

    > and if yes how do we achieve abstration, polymorhism , hierarchy etc.
    > in C


    system("g++ -o foo foo.cpp");


    I don't wish to be 100% negative, so I should point out that you can get
    data hiding and encapsulation fairly painlessly by using opaque types.


    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29 July 1999
    http://www.cpax.org.uk
    Email rjh at the above domain
     
    Richard Heathfield, Sep 1, 2005
    #2
    1. Advertising

  3. Vishal Naidu

    ajm Guest

    Vishal,

    It rather depends on precisely what you mean by "object oriented" but
    in the early days of C there was a well defined notion of what was
    meant by "object based" programming which has some of the notions of OO
    (e.g., data hiding) but could ultimately be subverted by the programmer
    if they so wished.

    The basic idea was to construct a struct (the data members) and a
    function pointer table (the methods) and use void pointers a lot ;) -
    you can probably find a detailed description by searching for "object
    based" and C but this approach is really not used anymore for obvious
    reasons.

    hth,
    ajm.
     
    ajm, Sep 1, 2005
    #3
  4. Vishal Naidu wrote:

    > I'm a college student in my second year.


    Congratulations!

    > Is it really possible to write object oriented code in C?


    Yes.

    > And, if yes, how do we achieve
    > [data] abstraction,
    > [run-time] polymorhism,
    > [inheritance],
    > etc. in C?


    There are two kinds of abstraction:

    1.) algorithmic abstraction and
    2.) data abstraction.

    Algorithmic abstraction refers to language features
    that allow you to create subprograms
    (subroutines, procedures, functions, etc.)
    which are independent, reusable modules.
    The original Dartmouth BASIC
    did not support algorithmic abstraction
    but most other high level computer programming languages do.
    Data abstraction refers to language features
    that allow you to create new, User Defined Types (UDTs).
    Standard Fortran 77 does not support data abstraction
    but most modern high level computer programming languages do.

    The C computer programming language allows you to create UDTs
    by *encapsulating* other data types in a struct
    but C does *not* support data hiding -- private data members.
    Beware -- for some object oriented programmers,
    encapsulation implies data hiding.
    Data hiding does *not* mean encryption.
    There is no way to hide the data representation
    which a determined programmer can't expose
    with just a few simple tests.
    Data hiding is intended only to prevent programmers
    from accidently accessing the actual data representation directly.

    The C computer programming language does *not* support inheritance.
    An explicit caste is required of a pointer to a derived type
    to a pointer of the base type is required.

    C programmers have been using run-time polymorphism
    as long as there have been C programmers.
    The standard FILE type, for example, is a polymorphic type.
     
    E. Robert Tisdale, Sep 1, 2005
    #4
  5. Vishal Naidu

    Malcolm Guest

    "Vishal Naidu" <> wrote
    > is it really possible to write object oriented code in C ?
    > and if yes how do we achieve abstration, polymorhism , hierarchy etc.
    > in C
    >

    The advantage of using C rather than an object-oriented language is that you
    get to decide the protocol.

    For instnace you could define a struct OBJECT
    typedef struct
    {
    void **interfaces;
    char **interfacenames;
    in Ninterfaces;
    } OBJECT;

    Then you can have a function void *get_interface(OBJECT *obj, char *name)

    This queries the object for an interface, and returns it.

    eg
    typedef struct
    {
    void *draw(unsigned char *rgb, int width, int height, int x, int y);
    } DRAWABLE;

    Thus you build your object from interfaces.
     
    Malcolm, Sep 3, 2005
    #5
  6. Vishal Naidu

    sahu Guest

    hello vishal i too is a coll student in svnit surat second yr comps. i
    think it's not possible to use c for oop for using oop u have to use
    cpp.by the way it will be nice to discuss with u the probs in c. tell
    me how will u create a two-d array with dynamic allocation.like int **t
    for a[5][8].ok bye
     
    sahu, Sep 3, 2005
    #6
  7. Vishal Naidu

    Mark Gordon Guest

    On 3 Sep 2005 12:02:16 -0700
    "sahu" <> wrote:

    > hello vishal i too is a coll student in svnit surat second yr comps. i
    > think it's not possible to use c for oop for using oop u have to use


    Use real English not stupid contractions like u, yr etc. We don't
    expect perfection, but at least make the attempt. The overall effect
    of these contractions is that I just can't be bothered to to put in
    the work required to read most of your post.

    > cpp.by the way it will be nice to discuss with u the probs in c. tell
    > me how will u create a two-d array with dynamic allocation.like int
    > **t for a[5][8].ok bye


    This is a FAQ, so search for and read the FAQ.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Mark Gordon, Sep 3, 2005
    #7
  8. Vishal Naidu

    Jack Klein Guest

    On 1 Sep 2005 01:43:41 -0700, "Vishal Naidu" <>
    wrote in comp.lang.c:

    > i m a college student in my second year.....
    > my queston is..
    > is it really possible to write object oriented code in C ?
    > and if yes how do we achieve abstration, polymorhism , hierarchy etc.
    > in C


    What do "polymorphism" (to spell it correctly) and "hierarchy" have to
    do with object orientation? These are features provided by some
    languages to promote and extend the use of object orientation, but
    they are far from the minimum object oriented feature set.

    The FILE type in C, declared in <stdio.h>, is a perfect example of
    object orientation in C.

    It's members are unspecified. There is a function to create an object
    of this type, fopen(). There is a function to uncreate an object of
    this type when it is no longer needed, fclose().

    While you have an object of this type, after a successful fopen() and
    prior to fclose(), you can invoke object specific methods on it, such
    as fread() or fprintf() or fseek(). You can call other methods to
    check its status, such as feof() or ferror().

    You confuse many concepts build on top of object orientation with
    object orientation itself. Or you are using the term to means
    something considerably more than it actually does.

    --
    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++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Sep 4, 2005
    #8
  9. Vishal Naidu

    Guillaume Guest

    Vishal Naidu wrote:
    > is it really possible to write object oriented code in C ?


    Of course. I do it all the time.
    The real question is "why would it not be possible".

    Object-oriented programming is basically a form of programming
    that's centered around objects. If you design your programs
    around objects, that will come naturally. Some "features" of
    some typical OO-languages will be harder to implement; but
    they also might be less than desirable (it can lead to a long
    debate...)

    I'd go as far as claiming that any serious piece of program
    implemented in C *should* be object-oriented. As long as you
    have data structures and functions that use them, you have
    the opportunity to write in an object-oriented maneer.

    No one should be allowed to tell you that you should use C++
    instead. ;-) Now if you want a really "strong" alternative
    to C, you should consider ADA instead. (And here come the
    flame wars...)
     
    Guillaume, Sep 4, 2005
    #9
  10. Vishal Naidu

    Imre Palik Guest

    "E. Robert Tisdale" <> writes:

    > but C does *not* support data hiding -- private data members.


    Except for singletons, where static data can be used for it.

    ImRe
     
    Imre Palik, Sep 28, 2005
    #10
  11. Vishal Naidu

    pete Guest

    Imre Palik wrote:
    >
    > "E. Robert Tisdale" <> writes:
    >
    > > but C does *not* support data hiding -- private data members.

    >
    > Except for singletons, where static data can be used for it.


    What's a singleton?

    --
    pete
     
    pete, Sep 28, 2005
    #11
  12. "pete" <> wrote in message
    news:...
    > > > but C does *not* support data hiding -- private data members.

    > > Except for singletons, where static data can be used for it.

    >
    > What's a singleton?


    There're classes which must have only instance. If I'm not mistaken, this is
    what's called singleton, the only instance.

    Alex
     
    Alexei A. Frounze, Sep 28, 2005
    #12
    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. Tommy
    Replies:
    2
    Views:
    361
    John C. Bollinger
    Nov 22, 2004
  2. Mike Wahler
    Replies:
    1
    Views:
    381
    Az Tech
    Aug 2, 2003
  3. Tom Anderson
    Replies:
    17
    Views:
    490
    Tom Anderson
    Apr 1, 2011
  4. Krekna Mektek

    Object Orientation

    Krekna Mektek, Oct 5, 2005, in forum: Ruby
    Replies:
    15
    Views:
    254
    William James
    Oct 7, 2005
  5. Len Sumnler
    Replies:
    1
    Views:
    94
    Robert Klemme
    Feb 16, 2010
Loading...

Share This Page