How to write object oriented program in c

Discussion in 'C Programming' started by SSG, Aug 10, 2005.

  1. SSG

    SSG Guest

    How to write a object oriented program in c? give me one example....
     
    SSG, Aug 10, 2005
    #1
    1. Advertising

  2. "SSG" <> writes:
    > How to write a object oriented program in c? give me one example....


    #include <stdio.h>
    int main(void)
    {
    puts("a object oriented program");
    return 0;
    }

    Assuming that's not what you had in mind, try searching this
    newsgroup; others have asked the same question here recently.

    --
    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, Aug 10, 2005
    #2
    1. Advertising

  3. SSG

    lampard Guest

    #include<stdio.h>
    struct A
    {
    public:
    A(){ printf(" A:: constructor \n"); }
    ~A(){ printf(" A:: destructor \n"); }
    void print(){ printf(" A::print \n"); }
    };

    int main(int argc, char* argv[])
    {
    A a;
    a.print();

    printf("Hello World!\n");
    return 0;
    }


    use struct, only difference from class to struct is all the methods and
    variables in struct are public by default ....
     
    lampard, Aug 10, 2005
    #3
  4. SSG

    Chris Dollin Guest

    lampard wrote:

    > #include<stdio.h>
    > struct A
    > {
    > public:


    Syntax error. BOOM.

    --
    Chris "electric hedgehog" Dollin
    Stross won one! Farah won one! Langford won TWO!
     
    Chris Dollin, Aug 10, 2005
    #4
  5. SSG

    Markus Moll Guest

    Hi

    Hi

    SSG wrote:

    > How to write a object oriented program in c? give me one example....


    The easiest approach is to have structs encapsulating your data and have
    functions operating on these structs. Typically looks like this:

    @@@ oo.h @@@
    struct Integer;

    Integer* integer_construct();
    /* I avoid using "new" as you might want to compile with a C++ compiler */
    Integer* integer_construct_from_int(int i);
    Integer* integer_copy_construct(const Integer *i);
    void integer_destroy(Integer *i);

    void integer_add(Integer* me, const Integer *other);
    /* again, avoid using "this" */

    and so on...

    See other sources for more sophisticated concepts...

    Cheers
    Markus
     
    Markus Moll, Aug 10, 2005
    #5
  6. SSG

    lampard Guest

    I had compiled on vc++ and looks ok, can you please explain what is
    wrong... hope I can pick one or two points from you...
     
    lampard, Aug 10, 2005
    #6
  7. SSG

    CBFalconer Guest

    SSG wrote:
    >
    > How to write a object oriented program in c? give me one example....


    Take a look at hashlib.

    <http://cbfalconer.home.att.net/download/hashlib.zip>

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Aug 10, 2005
    #7
  8. lampard wrote:
    > I had compiled on vc++ and looks ok, can you please explain what is
    > wrong...


    C is not C++ and you failed to quote any context so someone looking at
    your message by itself may have no idea what you are talking about.

    Robert Gamble
     
    Robert Gamble, Aug 10, 2005
    #8
  9. SSG

    Flash Gordon Guest

    lampard wrote:
    > I had compiled on vc++ and looks ok, can you please explain what is
    > wrong... hope I can pick one or two points from you...


    Provide context. Even with google this is possible and instructions get
    posted MULTIPLE TIMES MOST DAYS. No go and complain at google for them
    changing there interface to something that encouraged you to do the
    wrong thing and get complained at.

    What you were commenting about was:
    >>#include<stdio.h>
    >> struct A
    >> {
    >> public:

    >
    >Syntax error. BOOM.


    No, look at the name of the compiler you are using. Note that it ends
    C++. No look at the name of this group. See that it ends c WITHOUT a ++.
    No look at your list of available groups and you will probably see that
    there is another group ending in c++.

    C++ and C are different languages. You *can* can VC++ to compile C code
    and when you do the code you posted will not compile because it is C++
    and not C.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Aug 10, 2005
    #9
  10. On Wed, 10 Aug 2005 02:55:14 -0700, lampard wrote:

    > I had compiled on vc++ and looks ok, can you please explain what is
    > wrong... hope I can pick one or two points from you...


    What is wrong with what? You forgot to quote the relevant part of qhat you
    are replying to.

    Note however that VC++ provides both C and C++ compilers. Make sure you
    are invoking it as a C compiler.

    Lawrence
     
    Lawrence Kirby, Aug 10, 2005
    #10
  11. In article <>,
    lampard <> wrote:
    >#include<stdio.h>
    >struct A
    >{
    >public:
    > A(){ printf(" A:: constructor \n"); }
    > ~A(){ printf(" A:: destructor \n"); }
    > void print(){ printf(" A::print \n"); }
    >};
    >
    >int main(int argc, char* argv[])
    >{
    > A a;
    > a.print();
    >
    > printf("Hello World!\n");
    > return 0;
    >}
    >
    >
    >use struct, only difference from class to struct is all the methods and
    >variables in struct are public by default ....


    --------
    dave@hct-cvs:~/clc (0) $ cat lampard.c
    #include<stdio.h>
    struct A
    {
    public:
    A(){ printf(" A:: constructor \n"); }
    ~A(){ printf(" A:: destructor \n"); }
    void print(){ printf(" A::print \n"); }
    };

    int main(int argc, char* argv[])
    {
    A a;
    a.print();

    printf("Hello World!\n");
    return 0;
    }

    dave@hct-cvs:~/clc (0) $ gcc -W -Wall -ansi -pedantic -O lampard.c
    lampard.c:4: parse error before `public'
    lampard.c:4: warning: no semicolon at end of struct or union
    lampard.c:8: parse error before `}'
    lampard.c:8: warning: ANSI C does not allow extra `;' outside of a function
    lampard.c: In function `main':
    lampard.c:12: `A' undeclared (first use in this function)
    lampard.c:12: (Each undeclared identifier is reported only once
    lampard.c:12: for each function it appears in.)
    lampard.c:12: parse error before `a'
    lampard.c:13: `a' undeclared (first use in this function)
    lampard.c:10: warning: unused parameter `argc'
    lampard.c:10: warning: unused parameter `argv'
    dave@hct-cvs:~/clc (1) $
    --------

    I would suggest trying again, but I think I'm afraid to see the results
    of such an attempt.


    dave

    --
    Dave Vandervies
    This outdoor-light gizmo had fewer transistors than you've got fingers,
    unless you've been playing with explosives.
    --Anthony de Boer in the scary devil monastery
     
    Dave Vandervies, Aug 10, 2005
    #11
  12. SSG

    Eric Laberge Guest

    SSG wrote:

    > How to write a object oriented program in c? give me one example....


    I found the following helpful when I wanted to add some object-like behavior
    to a project in C:
    http://www.planetpdf.com/codecuts/pdfs/ooc.pdf

    --
    Eric Laberge
     
    Eric Laberge, Aug 10, 2005
    #12
  13. SSG

    John Bode Guest

    lampard wrote:
    > I had compiled on vc++ and looks ok, can you please explain what is
    > wrong... hope I can pick one or two points from you...


    What's wrong is that the OP was looking for an example of OOP in *C*,
    not C++. Your example is not valid C.

    Most of the C OOP implementations I've seen have relied heavily on
    preprocessors to convert files containing high-level class definitions
    into compilable C code (CORBA's one example), essentially doing the
    same kind of name mangling that the C++ compiler does.
     
    John Bode, Aug 10, 2005
    #13
  14. SSG

    lampard Guest

    Flash Gordon wrote:
    > lampard wrote:
    > > I had compiled on vc++ and looks ok, can you please explain what is
    > > wrong... hope I can pick one or two points from you...

    >
    > Provide context. Even with google this is possible and instructions get
    > posted MULTIPLE TIMES MOST DAYS. No go and complain at google for them
    > changing there interface to something that encouraged you to do the
    > wrong thing and get complained at.

    I was not complaining, I was requesting information.. You better go for
    a crash course in english because your english sucks.. look at your
    phrase "No go and complain ..".

    >
    > What you were commenting about was:
    > >>#include<stdio.h>
    > >> struct A
    > >> {
    > >> public:

    > >
    > >Syntax error. BOOM.

    >
    > No, look at the name of the compiler you are using. Note that it ends
    > C++. No look at the name of this group. See that it ends c WITHOUT a ++.
    > No look at your list of available groups and you will probably see that
    > there is another group ending in c++.
    >
    > C++ and C are different languages. You *can* can VC++ to compile C code
    > and when you do the code you posted will not compile because it is C++
    > and not C.
    > --
    > Flash Gordon
    > Living in interesting times.
    > Although my email address says spam, it is real and I read it.
     
    lampard, Aug 11, 2005
    #14
  15. "lampard" <> writes:
    > Flash Gordon wrote:
    >> lampard wrote:
    >> > I had compiled on vc++ and looks ok, can you please explain what is
    >> > wrong... hope I can pick one or two points from you...

    >>
    >> Provide context. Even with google this is possible and instructions get
    >> posted MULTIPLE TIMES MOST DAYS. No go and complain at google for them
    >> changing there interface to something that encouraged you to do the
    >> wrong thing and get complained at.

    > I was not complaining, I was requesting information.. You better go for
    > a crash course in english because your english sucks.. look at your
    > phrase "No go and complain ..".


    Yes, he wrote "No" where he meant "Now" several times. I don't know
    why, but we don't usually jump on typos (unless they're in C code and
    therefore significant).

    His point, I think, is that you *should* be complaining to Google
    about their broken Usenet interface. It apparently led you to make
    the same mistake that far too many others have made here, posting
    followups with no context. I'm truly delighted to see that you've now
    corrected that.

    As for your original followup, you're probably aware by now that what
    you posted was C++ code, not C, and therefore both off-topic here and
    not a response to the original question. (If, as I said, you're
    already aware of that, feel free to ignore this paragraph.)

    --
    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, Aug 11, 2005
    #15
  16. SSG

    lampard Guest

    Keith Thompson wrote:
    > "lampard" <> writes:
    > > Flash Gordon wrote:
    > >> lampard wrote:
    > >> > I had compiled on vc++ and looks ok, can you please explain what is
    > >> > wrong... hope I can pick one or two points from you...
    > >>
    > >> Provide context. Even with google this is possible and instructions get
    > >> posted MULTIPLE TIMES MOST DAYS. No go and complain at google for them
    > >> changing there interface to something that encouraged you to do the
    > >> wrong thing and get complained at.

    > > I was not complaining, I was requesting information.. You better go for
    > > a crash course in english because your english sucks.. look at your
    > > phrase "No go and complain ..".

    >
    > Yes, he wrote "No" where he meant "Now" several times. I don't know
    > why, but we don't usually jump on typos (unless they're in C code and
    > therefore significant).
    >
    > His point, I think, is that you *should* be complaining to Google
    > about their broken Usenet interface. It apparently led you to make
    > the same mistake that far too many others have made here, posting
    > followups with no context. I'm truly delighted to see that you've now
    > corrected that.
    >
    > As for your original followup, you're probably aware by now that what
    > you posted was C++ code, not C, and therefore both off-topic here and
    > not a response to the original question. (If, as I said, you're
    > already aware of that, feel free to ignore this paragraph.)
    >

    Thank you friend, Thank you for your advice. Sorry Jordon for jumping
    into early conclusion. I am here to learn from experienced persons, but
    not to discourage people over here.
    > --
    > 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.
     
    lampard, Aug 11, 2005
    #16
  17. SSG

    Flash Gordon Guest

    lampard wrote:
    > Keith Thompson wrote:
    >
    >>"lampard" <> writes:
    >>
    >>>Flash Gordon wrote:
    >>>
    >>>>lampard wrote:


    <snip>

    >>Yes, he wrote "No" where he meant "Now" several times. I don't know
    >>why,


    I don't know why either. However, here are some "w"s to make up for it.
    wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww

    <snip>

    > Thank you friend, Thank you for your advice. Sorry Jordon


    heh

    > for jumping
    > into early conclusion. I am here to learn from experienced persons, but
    > not to discourage people over here.


    Apology accepted. I was probably a bit annoyed at the time and you just
    happened to be in the firing line. My Tryping, as opposed to my English
    skills, does deteriorate when I am in a bad mood.

    Google should include a couple of sentences when you sign up telling you
    to check a group and its FAQ before posting (more and it definitely
    won't be read). I also think that new Google users should complain at
    Google when they get jumped on because Google led them in to doing the
    wrong thing. Maybe if enough people told them that they are leaving
    Google because it is broken they would do something about it (many here
    including me have complained to them). Maybe I should set up my own
    subscription web interface to Google...
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Aug 11, 2005
    #17
  18. SSG

    Alex Guest

    Eric Laberge wrote:
    > SSG wrote:
    >
    >
    >>How to write a object oriented program in c? give me one example....

    >
    >
    > I found the following helpful when I wanted to add some object-like behavior
    > to a project in C:
    > http://www.planetpdf.com/codecuts/pdfs/ooc.pdf
    >


    I also found Eric's link to be useful. It's a PDF edition of the book,
    which I believe is out of print, "Object-oriented Programming with
    ANSI-C"; Axel-Tobias Schreiner 1993. The source code for the book's
    examples can be downloaded here -
    ftp://ftp.informatik.uni-osnabrueck.de/pub/hanser/books/ooc-94.2.11.tar.gz

    Also see the post "Switch from C++ to C" at the comp.lang.c.moderated forum.
     
    Alex, Aug 11, 2005
    #18
  19. SSG

    Alex Guest

    Alex, Aug 11, 2005
    #19
    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. Abs
    Replies:
    1
    Views:
    1,076
    Thomas Weidenfeller
    May 11, 2004
  2. jon3484
    Replies:
    3
    Views:
    363
  3. Replies:
    2
    Views:
    434
    Bruno Desthuilliers
    May 26, 2008
  4. rolo
    Replies:
    3
    Views:
    182
    Robert Klemme
    Apr 9, 2004
  5. J W
    Replies:
    2
    Views:
    401
Loading...

Share This Page