how to use pure c to write OO programs

Discussion in 'C Programming' started by zhuyin.nju@gmail.com, Oct 19, 2005.

  1. Guest

    can some one tell that how to use pure c to write OO programs?

    thanks in advanced!
     
    , Oct 19, 2005
    #1
    1. Advertising

  2. Zara Guest

    On 19 Oct 2005 03:49:10 -0700, wrote:

    >can some one tell that how to use pure c to write OO programs?
    >
    >thanks in advanced!



    I don´t think "pure" is the right adjective.

    Use structs, pointers to structs, pointer to functions, enums.

    Do not use unions.

    Practise a lot.

    Good Luck!
     
    Zara, Oct 19, 2005
    #2
    1. Advertising

  3. EventHelix.com, Oct 19, 2005
    #3
  4. Skarmander Guest

    wrote:
    > can some one tell that how to use pure c to write OO programs?
    >

    1. Face a wall.
    2. Take quite a few steps back.
    3. Holding your head down, run towards the wall.
    4. Repeat until you feel you have inflicted enough harm on yourself.

    I realize this is not really helpful. Neither is pure C when it comes to
    O-O programming, however.

    In my opinion, you'd be much better off using a C++ frontend,
    restricting yourself to basic O-O concepts while ignoring the C++ bells
    and whistles that are not essential to your design. I don't know about
    any frontends still out there, though ("if I want C++, I know where to
    get it"). Anyone know if cfront or a clone still lives, and is of any use?

    You could even go crazy and use, you know, an actual C++ compiler. Many
    produce quite acceptable code (in terms of size/speed/overhead) for
    simple C++ that doesn't pull in all the STL goodies. In fact, the
    initial design criteria for C++, for better or worse, included the
    notion that compilers shouldn't be too burdened processing it
    effectively (not much was left of this notion by the time the language
    was finalized, but that's another issue).

    Of course this won't help you if you don't have a C++ compiler for the
    platform or if object file compatibility with C code is necessary, but
    it's still worth considering.

    S.
     
    Skarmander, Oct 19, 2005
    #4
  5. Marc Boyer Guest

    Le 19-10-2005, Skarmander <> a écrit :
    > wrote:
    >> can some one tell that how to use pure c to write OO programs?
    >>

    > 1. Face a wall.
    > 2. Take quite a few steps back.
    > 3. Holding your head down, run towards the wall.
    > 4. Repeat until you feel you have inflicted enough harm on yourself.
    >
    > I realize this is not really helpful. Neither is pure C when it comes to
    > O-O programming, however.
    >
    > In my opinion, you'd be much better off using a C++ frontend,
    > restricting yourself to basic O-O concepts while ignoring the C++ bells
    > and whistles that are not essential to your design. I don't know about
    > any frontends still out there, though ("if I want C++, I know where to
    > get it"). Anyone know if cfront or a clone still lives, and is of any use?


    The comeau compiler generates C from C++:
    http://www.comeaucomputing.com/
    What Is Comeau C/C++ 4.3.3?

    Comeau C/C++ is a command line driven C and C++ compiler that generates
    platform specific and C compiler specific C as its object code (the
    generated C code won't work on another platform, as it is CPU, OS and C
    compiler specific, and furthermore, it is not standalone)

    Marc Boyer
     
    Marc Boyer, Oct 19, 2005
    #5
  6. Singamsetty Guest

    EventHelix.com wrote:
    > The following article gives an example of object oriented programming
    > in C:
    >
    > http://www.eventhelix.com/RealtimeMantra/basics/object_oriented_programming_in_c.htm
    >
    > --
    > EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
    > Sequence Diagram Based Real-time and Embedded System Design Tool



    The main characteristics of an object oriented programming are
    encapsulation, inheritance and polymorphism. We can achieve
    encapsulation - information hiding, data abstraction - in C (by
    implementing ADTs or modules) but not the other two. C is more of a
    object-based programming language (not OO language). I guess, there is
    a good discussion in Stroustrup C++ book on object-based (C-like) and
    OO (C++ like) paradigms.

    - Singamsetty
     
    Singamsetty, Oct 19, 2005
    #6
  7. Guillaume Guest

    I was wondering how long it would take before someone says to use C++.
    As it turned out, almost 4 hours.
    Of course, you could have gone one step further and suggest ADA.
    Oh well... lol ;-)
     
    Guillaume, Oct 19, 2005
    #7
  8. Skarmander Guest

    Guillaume wrote:
    > I was wondering how long it would take before someone says to use C++.
    > As it turned out, almost 4 hours.


    Remember, folks: propagation of Usenet posts can take anywhere from
    minutes to days. You never know who reads what and when. So I guess I'm
    just lucky! What did I win?

    > Of course, you could have gone one step further and suggest ADA.


    No. I did make an effort not to stray too far from the OP's intent,
    having noticed that others had already answered the question directly.
    Would the question really have been answered in full without at least
    the suggestion of C++, taking the possible reasons why it could have
    rejected it into account?

    Ada qualifies as "too far". I could have recommended Java, then; at
    least that's more syntax-friendly to C programmers. If someone wants to
    do O-O in C, though, there's probably a good reason why Java would be
    out of the question. A C++ frontend may just have been missed as an option.

    S.
     
    Skarmander, Oct 19, 2005
    #8
  9. Skarmander <> writes:
    > wrote:
    >> can some one tell that how to use pure c to write OO programs?
    >>

    > 1. Face a wall.
    > 2. Take quite a few steps back.
    > 3. Holding your head down, run towards the wall.
    > 4. Repeat until you feel you have inflicted enough harm on yourself.
    >
    > I realize this is not really helpful. Neither is pure C when it comes to
    > O-O programming, however.
    >
    > In my opinion, you'd be much better off using a C++ frontend,
    > restricting yourself to basic O-O concepts while ignoring the C++ bells
    > and whistles that are not essential to your design. I don't know about
    > any frontends still out there, though ("if I want C++, I know where to
    > get it"). Anyone know if cfront or a clone still lives, and is of any use?


    I don't see the point of using a C++ frontend (presumably something
    that translates C++ to C) as opposed to a C compiler.

    The C generated by a translator is unlikely to be maintainable or even
    legible. In ordinary usage, you're never even going to look at it;
    you'll just feed it to a C compiler. Assuming a C++ compiler is
    available for your platform, there's no real benefit in generating
    unmaintanable C as an intermediate step.

    There are ways to do OO in C, but they tend to be clumsy compared to
    using a language with OO facilities built-in.

    To the OP: Why do you specifically want to use pure C?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Oct 19, 2005
    #9
  10. Guillaume <"grsNOSPAM at NOTTHATmail dot com"> writes:
    > I was wondering how long it would take before someone says to use C++.
    > As it turned out, almost 4 hours.
    > Of course, you could have gone one step further and suggest ADA.
    > Oh well... lol ;-)


    And why is that amusing? C++ may well turn out to be the best
    solution for the OP's problem. (Ada might not be such a bad idea
    either.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Oct 19, 2005
    #10
  11. Skarmander Guest

    Keith Thompson wrote:
    > Skarmander <> writes:
    >
    >> wrote:
    >>
    >>>can some one tell that how to use pure c to write OO programs?
    >>>

    >>
    >>1. Face a wall.
    >>2. Take quite a few steps back.
    >>3. Holding your head down, run towards the wall.
    >>4. Repeat until you feel you have inflicted enough harm on yourself.
    >>
    >>I realize this is not really helpful. Neither is pure C when it comes to
    >>O-O programming, however.
    >>
    >>In my opinion, you'd be much better off using a C++ frontend,
    >>restricting yourself to basic O-O concepts while ignoring the C++ bells
    >>and whistles that are not essential to your design. I don't know about
    >>any frontends still out there, though ("if I want C++, I know where to
    >>get it"). Anyone know if cfront or a clone still lives, and is of any use?

    >
    >
    > I don't see the point of using a C++ frontend (presumably something
    > that translates C++ to C) as opposed to a C compiler.
    >


    Because a C compiler won't let you compile C++. You meant "as opposed to
    a C++ compiler", perhaps? Or just "write in C"?

    > The C generated by a translator is unlikely to be maintainable or even
    > legible. In ordinary usage, you're never even going to look at it;
    > you'll just feed it to a C compiler.


    Yes.

    > Assuming a C++ compiler is available for your platform, there's no
    > real benefit in generating unmaintanable C as an intermediate step.
    >


    That is:
    - assuming there *is* a C++ compiler of sufficient quality available
    (and "quality" may also imply "low overhead", which is certainly an
    issue for many C++ compilers);

    - assuming you do not need low-level compatibility with C (name
    mangling, linkage etc.; some of this will play a role with a frontend
    too, but is likely far easier to cope with).

    > There are ways to do OO in C, but they tend to be clumsy compared to
    > using a language with OO facilities built-in.
    >


    Precisely. Which is why my first advice is: don't do it. The second is:
    if you *must* get C for some reason (you need to interface directly with
    existing C code, your compiler supports only C) and you *must* use an
    O-O design (for reasons I can't make up, but there undoubtedly will be
    some) why *not* a frontend?

    S.
     
    Skarmander, Oct 19, 2005
    #11
  12. Singamsetty wrote:

    > The main characteristics of an object oriented programming are
    > encapsulation,
    > inheritance and
    > [run-time] polymorphism.
    > We can achieve encapsulation - information hiding, data abstraction - in C
    > (by implementing ADTs or modules) but not the other two.


    Not quite.
    The C computer programming language supports encapsulation
    but not data hiding.
    Opaque data types are only an inferior attempt at data hiding.
    C supports data abstraction in a number of ways
    including typedefs and structs.
    C structs are used to introduce User Defined Types (UDTs)
    which can implement Abstract Data Types (ADTs).
    C does *not* support inheritance
    so programmers are obliged to cast [pointers] to the base type.
    C programmers have always implemented run-time polymorphism
    using jump tables (virtual function tables).
    The standard FILE type is an example of run-time polymorphism.

    Search Google Groups

    http://groups.google.com/

    for

    comp.lang.c Object-Oriented Programming (run-time polymorphism) with C99

    Please find attached a copy of the "Shape class" example from
    Bjarne Stroustrup, "The C++ Programming Language: Third Edition",
    Chapter 2: A Tour of C++, Section 6: Object-Oriented Programming,
    Subsection 2: Class Hierarchies, pages 38-40 implemented in C99.

    > C is more of a object-based programming language (not OO language).
    > I guess, there is a good discussion in Stroustrup's C++ book
    > on object-based (C-like) and OO (C++ like) paradigms.


    A citation please.
     
    E. Robert Tisdale, Oct 19, 2005
    #12
  13. Skarmander <> writes:
    > Keith Thompson wrote:

    [...]
    >> I don't see the point of using a C++ frontend (presumably something
    >> that translates C++ to C) as opposed to a C compiler.

    >
    > Because a C compiler won't let you compile C++. You meant "as opposed
    > to a C++ compiler", perhaps? Or just "write in C"?


    Whoops, I meant "as opposed to a C++ compiler".

    BTW, a C++ frontend (translating C++ to C) in combination with a C
    compiler *is* a C++ compiler. The fact that it uses C as an
    intermediate language isn't likely to be particularly useful in any
    likely scenario I can think of. You can examine the generated C code
    to see what symbols it uses, but you can just as easily examine an
    object file, which is likely to be about as legible.

    This may be another of those "How do I drive screws without using a
    screwdriver?" questions. Advising the questioner on how to hit the
    screws really hard with a hammer probably isn't helpful until we find
    out *why* he doesn't want to use a screwdriver.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Oct 19, 2005
    #13
  14. Skarmander Guest

    Keith Thompson wrote:
    > Skarmander <> writes:
    >
    >>Keith Thompson wrote:

    >
    > [...]
    >
    >>>I don't see the point of using a C++ frontend (presumably something
    >>>that translates C++ to C) as opposed to a C compiler.

    >>
    >>Because a C compiler won't let you compile C++. You meant "as opposed
    >>to a C++ compiler", perhaps? Or just "write in C"?

    >
    >
    > Whoops, I meant "as opposed to a C++ compiler".
    >
    > BTW, a C++ frontend (translating C++ to C) in combination with a C
    > compiler *is* a C++ compiler.


    Yes.

    > The fact that it uses C as an intermediate language isn't likely to
    > be particularly useful in any likely scenario I can think of. You
    > can examine the generated C code to see what symbols it uses, but you
    > can just as easily examine an object file, which is likely to be
    > about as legible.
    >


    Not quite. The frontend will certainly generate code you can link with
    other object files. The same is not true of your C++ compiler and C
    object files, even if you use the "same" compiler for both. Especially
    if we can disregard issues of overloading, multiple inheritance and all
    that malarkey.

    Of course, this means pointing out that expecting linkage from and to C
    to "magically" work out means you're counting on your implementation's
    amenities to save the day, which may or may not be acceptable. If the
    intent is portable C++ code even *without* the frontend (that is, you
    want the compiler to be *fully* transparent) it's not good enough.

    > This may be another of those "How do I drive screws without using a
    > screwdriver?" questions. Advising the questioner on how to hit the
    > screws really hard with a hammer probably isn't helpful until we find
    > out *why* he doesn't want to use a screwdriver.
    >


    And possibly it is, making an educated guess about why he wouldn't want
    to use it. Of course, I could guess wrong. Such is life. Caveat lector,
    you get what you pay for, etc. :)

    S.
     
    Skarmander, Oct 20, 2005
    #14
  15. bwegge Guest

    You should read this paper on OO in ansi C -
    www.planetpdf.com/codecuts/pdfs/ooc.pdf. I don't like the use of the
    preprocessor in this paper, but thats just me. Also Scott Meyers book
    "Effective C++: 50 ways .." gives good insight into how C++ works, and
    can be recommended if you want to make OO in C. I found it very
    helpful, when trying to do OO in C.

    Basically, you will find it very difficult (if not impossible) to do
    stuff like (operator) overloading and automatic calls to
    constructors/destructors. Inheritance and virtual methods (essence of
    OO) is fairly straight forward to do, but the biggest problem is not
    doing OO in C. It is getting those who read your code a year from now
    to understand what you are doing. OO in C looks very complicated and is
    in my experience a good way to make your code more error prone. In
    other words, dont go there unless you absolutely have to.

    Regards
    Bwegge
     
    bwegge, Oct 20, 2005
    #15
  16. In article <>, "Singamsetty" <> writes:
    >
    > The main characteristics of an object oriented programming are
    > encapsulation, inheritance and polymorphism. We can achieve
    > encapsulation - information hiding, data abstraction - in C (by
    > implementing ADTs or modules) but not the other two.


    Utter rot. C doesn't provide syntactic sugar for polymorphism, but
    there's nothing preventing you from implementing it manually. The
    same can be said of inheritance, unless you use an overly-restrictive
    definition of the term.

    And, of course, it's always possible to implement one Turing-complete
    language in another.

    This has been discussed ad nauseum here.

    --
    Michael Wojcik

    Viewers are bugs for famous brands.
    -- unknown subtitler, Jackie Chan's _Thunderbolt_
     
    Michael Wojcik, Oct 20, 2005
    #16
  17. bwegge <> wrote:

    > You should read this paper on OO in ansi C -
    > www.planetpdf.com/codecuts/pdfs/ooc.pdf. I don't like the use of the
    > (snip context-free text)


    It is proper Usenet etiquette to include the relevant portions of the text
    you are replying to. To do this using Google groups, please follow the
    instructions below, penned by Keith Thompson:

    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Oct 20, 2005
    #17
  18. E. Robert Tisdale, Oct 20, 2005
    #18
  19. Greg Comeau Guest

    In article <43565921$0$11068$4all.nl>,
    Skarmander <> wrote:
    > wrote:
    >> can some one tell that how to use pure c to write OO programs?
    >>

    >1. Face a wall.
    >2. Take quite a few steps back.
    >3. Holding your head down, run towards the wall.
    >4. Repeat until you feel you have inflicted enough harm on yourself.
    >
    >I realize this is not really helpful. Neither is pure C when it comes to
    >O-O programming, however.
    >
    >In my opinion, you'd be much better off using a C++ frontend,
    >restricting yourself to basic O-O concepts while ignoring the C++ bells
    >and whistles that are not essential to your design. I don't know about
    >any frontends still out there, though ("if I want C++, I know where to
    >get it"). Anyone know if cfront or a clone still lives, and is of any use?


    Comeau C++, currently not based on cfront, generates C as an
    internal phase of translation, is going strong. However, not
    knowing the underlying reason for the OPs query, it's unclear
    if that's what the OP is seeking.

    >You could even go crazy and use, you know, an actual C++ compiler.


    Comeau C++ is an actual C++ compiler.

    >Many
    >produce quite acceptable code (in terms of size/speed/overhead) for
    >simple C++ that doesn't pull in all the STL goodies. In fact, the
    >initial design criteria for C++, for better or worse, included the
    >notion that compilers shouldn't be too burdened processing it
    >effectively (not much was left of this notion by the time the language
    >was finalized, but that's another issue).


    That two main hits on C++ are iostreams and exception handling.

    >Of course this won't help you if you don't have a C++ compiler for the
    >platform or if object file compatibility with C code is necessary, but
    >it's still worth considering.


    We get can compilers going rapidly, but as above, not sure these
    things touch on the OPs query.
    --
    Greg Comeau / Celebrating 20 years of Comeauity!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Oct 21, 2005
    #19
  20. Greg Comeau Guest

    In article <43566b9c$0$459$-internet.fr>,
    Guillaume <"grsNOSPAM at NOTTHATmail dot com"> wrote:
    >I was wondering how long it would take before someone says to use C++.
    >As it turned out, almost 4 hours.
    >Of course, you could have gone one step further and suggest ADA.
    >Oh well... lol ;-)


    Are you suggesting that it shouldn't be suggested?
    Since C does not directly support many of the conceptual
    mappings as directly, mentioning C++ is a reasonable
    thing to do.
    --
    Greg Comeau / Celebrating 20 years of Comeauity!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Oct 21, 2005
    #20
    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. Todd Aspeotis
    Replies:
    3
    Views:
    467
    Kanenas
    May 30, 2005
  2. Brian Raab

    Script-kiddies use C to write evil programs ! ! !

    Brian Raab, Oct 6, 2004, in forum: C Programming
    Replies:
    6
    Views:
    498
    Minti
    Oct 6, 2004
  3. Weasel
    Replies:
    16
    Views:
    506
    Ed Jensen
    Nov 8, 2005
  4. Replies:
    4
    Views:
    797
    Ben C
    Mar 29, 2008
  5. Hans-Marc Olsen
    Replies:
    0
    Views:
    91
    Hans-Marc Olsen
    Nov 16, 2004
Loading...

Share This Page