How to write object oriented program in c

K

Keith Thompson

SSG said:
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.
 
L

lampard

#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 ....
 
M

Markus Moll

Hi

Hi
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
 
L

lampard

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...
 
R

Robert Gamble

lampard said:
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
 
F

Flash Gordon

lampard said:
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:
>
>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.
 
L

Lawrence Kirby

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
 
D

Dave Vandervies

#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
 
J

John Bode

lampard said:
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.
 
L

lampard

Flash said:
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 ..".
 
K

Keith Thompson

lampard said:
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.)
 
L

lampard

Keith said:
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.
 
F

Flash Gordon

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

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...
 
A

Alex

Eric said:
SSG wrote:




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.
 
A

Alex

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

I've found the book "Object-oriented Programming with ANSI-C"
(Axel-Tobias Schreiner 1993) to be helpful with C based OOP. You can
download the PDF edition of the book, which I believe is out of print,
here - http://www.planetpdf.com/codecuts/pdfs/ooc.pdf

AS FOR EXAMPLES: The source code for the book's samples can be
downloaded here -
ftp://ftp.informatik.uni-osnabrueck.de/pub/hanser/books/ooc-94.2.11.tar.gz
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top