By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,038 Members | 1,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,038 IT Pros & Developers. It's quick & easy.

How to write object oriented program in c

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

Nov 15 '05 #1
Share this Question
Share on Google+
18 Replies


P: n/a
"SSG" <ss****@gmail.com> 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) ks***@mib.org <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.
Nov 15 '05 #2

P: n/a
#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 ....

Nov 15 '05 #3

P: n/a
lampard wrote:
#include<stdio.h>
struct A
{
public:


Syntax error. BOOM.

--
Chris "electric hedgehog" Dollin
Stross won one! Farah won one! Langford won TWO!
Nov 15 '05 #4

P: n/a
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

Nov 15 '05 #5

P: n/a
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...

Nov 15 '05 #6

P: n/a
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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 15 '05 #7

P: n/a
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

Nov 15 '05 #8

P: n/a
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
Nov 15 '05 #9

P: n/a
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.
Nov 15 '05 #10

P: n/a
In article <11**********************@z14g2000cwz.googlegroups .com>,
lampard <de***********@hotmail.com> 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 dj******@csclub.uwaterloo.ca
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
Nov 15 '05 #11

P: n/a
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
Nov 15 '05 #12

P: n/a

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.

Nov 15 '05 #13

P: n/a

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.


Nov 15 '05 #14

P: n/a
"lampard" <de***********@hotmail.com> 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) ks***@mib.org <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.
Nov 15 '05 #15

P: n/a

Keith Thompson wrote:
"lampard" <de***********@hotmail.com> 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) ks***@mib.org <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.


Nov 15 '05 #16

P: n/a
lampard wrote:
Keith Thompson wrote:
"lampard" <de***********@hotmail.com> 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.
Nov 15 '05 #17

P: n/a
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....94.2.11.tar.gz

Also see the post "Switch from C++ to C" at the comp.lang.c.moderated forum.
Nov 15 '05 #18

P: n/a
SSG wrote:
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....94.2.11.tar.gz
Nov 15 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.