LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 08-09-2005, 05:55 AM   #1
ssg14j
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
kimx
Member
 
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
markhod
Member
 
Registered: Sep 2003
Posts: 103

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

Quote:
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?

Mark
 
Old 08-09-2005, 09:03 AM   #4
aluser
Member
 
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:

Code:
/* 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)
{
    stuff...
}
    /* end private methods */


void dog_bark(struct dog *d)
{
    stuff...
}

void dog_walk(struct dog *d)
{
    stuff....
    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:

Code:
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
mehuljv
Member
 
Registered: Nov 2004
Posts: 72

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

e.g.

// 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));
alloc(t);
fun(t);
t->t1->x = 90;
print(t);
}


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

Mehul.
 
Old 08-10-2005, 09:41 AM   #6
elyk1212
Member
 
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
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
Quote:
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.
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:03 PM.

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