473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to write object oriented program in c

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

Nov 15 '05 #1
18 2209
"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
#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
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
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
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
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
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
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
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
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
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

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

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
"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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Simon Wigzell | last post by:
I recently wrote a program with MS Visual Studio C++, sent it off to the client where it didn't run, after some probing I discover they are on a Mac! My program is a MSF interface that is really...
0
by: Benjamin C. Pierce | last post by:
The Twelth International Workshop on Foundations of Object-Oriented Languges (FOOL 12) Saturday 15 January 2005 Long Beach, California, USA Following POPL 05 The search for sound principles...
5
by: John Ladasky | last post by:
Hi, folks, At the beginning of 2003, I was a frustrated computer user, and lapsed programmer, with problems to solve that screamed for programming. Thanks to the Python language and community, I...
5
by: Martin | last post by:
When was inheritance intruduced into object oriented programming? More generally, does anyone know or have any sources on when the different features were introduced into object oriented...
2
by: Krimgelas | last post by:
Hello, I have been programming in PHP for a while, but always the classic function oriented approach. I would like to learn how to program in PHP with an object oriented approach, but so far...
46
by: ajba74 | last post by:
Hi fellows, I am reading some books to learn the C programming language, and sometimes I have the feeling that when somebody becomes a C expert, he must learn a more modern and object-oriented...
139
by: Joe Mayo | last post by:
I think I become more and more alone... Everybody tells me that C++ is better, because once a project becomes very large, I should be happy that it has been written in C++ and not C. I'm the only...
3
by: notnorwegian | last post by:
i have some confusion over this. sure a class is basically a classification, like for example an animal or flower. and an object/instance of that class is then for example a cat. an object is...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.