Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-09-2005, 05:55 AM   #1
LQ Newbie
Registered: Jul 2005
Posts: 12

Rep: Reputation: 0
How to write a object oriented program using c

How to write a program using object oreinented concepts in c?
any one know write one simple program...
Old 08-09-2005, 06:37 AM   #2
Registered: Dec 2004
Location: Denmark
Distribution: Yoper 2.2, Source Mage, Ubuntu 5.04, Slackware 10.1
Posts: 70

Rep: Reputation: 16
As far as I know you can't write object oreinented programs in c, but I have never used c much.
Old 08-09-2005, 07:16 AM   #3
Registered: Sep 2003
Posts: 103

Rep: Reputation: 15
Re: How to write a object oriented program using c

Originally posted by ssg14j
How to write a program using object oreinented concepts in c?
any one know write one simple program...
I know you can do it in FORTRAN 90, so probably you can manage it in c too. But in FORTRAN it is painful. Given this, why not just learn c++ which is designed to be used in an OO way?

Old 08-09-2005, 09:03 AM   #4
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
How easy it is depends on how many OO concepts you need. If you're simply looking for encapsulation, you can do this style:

/* dog.h */
struct dog;

    /* public methods */
void dog_bark(struct dog *d);
void dog_walk(struct dog *d);
/* end dog.h */

/* dog.c */

struct dog {
   int legs;

    /* here are my private methods: */
static void scratch(struct dog *d)
    /* end private methods */

void dog_bark(struct dog *d)

void dog_walk(struct dog *d)
    scratch(d); /* call to private method */
If you need polymorphism, it's easy to include some function pointers in the struct dog, or to include a pointer to a static table of function pointers in the struct dog. (That's more space efficient if dog has more than a couple of public methods.)

If you need single inheritance, you can do that by extending a struct like so:

struct poodle {
    struct dog d; /* this must be the first thing in the poodle struct! */
    int poofiness;
Then you can call the dog_* methods on a poodle by casting a poodle pointer to (struct dog*). If you're doing polymorphism too, you'd change the function pointers in the d field of the poodle before doing anything with it.

I'll try to explain more of whatever parts of this long ass post you find interesting, if any : ) Clearly it would be beneficial to do some OO programming in a language which actually has syntax for it before trying to fake it in C.

Last edited by aluser; 08-09-2005 at 09:05 AM.
Old 08-10-2005, 09:25 AM   #5
Registered: Nov 2004
Posts: 72

Rep: Reputation: 15
even u can make some private public variables in one structure using c.


// start test1.h

struct test1
int x;

struct test2;

struct test
struct test1 *t1;
struct test2 *t2;

// end test1.h

//start test1.c

#include <stdio.h>
#include "test1.h"

int main()
struct test *t = (struct test *)malloc(sizeof(struct test));
t->t1->x = 90;

//end test1.c

//start test2.h
struct test2
int y;

//end test2.h

//start test2.c
#include "test1.h"
#include "test2.h"

int alloc(struct test *t)
t->t1 = (struct test1 * )malloc(sizeof(struct test1));
t->t2 = (struct test2 *)malloc(sizeof(struct test2));

int fun(struct test *t)
t->t1->x = 10;
t->t2->y = 20;

int print(struct test *t)
printf("%d %d\n",t->t1->x,t->t2->y);
//end test2.c

Compile prg with gcc test1.c test2.c, from test1.c u can access t->t1->x bt u cant access t->t2->y .... this is somthing like private and public variable bt it is per file..... if u hv any other file which includes both test1.h and test2.h thn it can use both of above vairables.

Old 08-10-2005, 09:41 AM   #6
Registered: Jan 2005
Location: Chandler, AZ USA
Distribution: Mandrake/Mandriva 10.2
Posts: 186

Rep: Reputation: 30
As mentioned before, a struct with function pointers inside used for methods, would be possible.

But why bother? For the all that pain, you should just use C++... Unless this is not an option for some reason, or you are just doing this for academics.
Old 08-10-2005, 09:56 AM   #7
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
why bother
My stab at it:

If you're writing a library, it may be more useful as C because both C and C++ programs can easily link to it, not to mention that there are more programmers who understand C well than who understand C++ well.

Gtk+ is fully object oriented and uses a gobject library to describe its OO idioms. Also, the linux kernel makes use of objects with function pointers, so maybe it's a good thing to understand the idiom since it does exist in the wild.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to write a object oriented c program? ssg14j Programming 2 08-17-2005 10:47 AM
How are object-oriented languages transformed to GCC's RTL ? desmond5 Programming 0 04-17-2005 01:38 AM
perl object Oriented Programming Question eastsuse Programming 1 08-20-2004 12:29 PM
Problem with "Object Oriented Concepts" Mohsen Programming 6 03-24-2004 12:21 AM
Object-Oriented Design Citizen Bleys Programming 2 02-11-2002 01:25 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:50 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration