Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

can u enforce OOPS in C

0 views
Skip to first unread message

vas...@rocketmail.com

unread,
Dec 30, 1998, 3:00:00 AM12/30/98
to
Hi
Consider this in OOPS

class abc{
private a1;
protected a2;
public method1(){
//method1 implemttion
}
public method2(){
//method2 implemttion
}

}

can we achieve the same in C. Kindly let me know how at vas...@rocketmail.com

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

Ben Pfaff

unread,
Dec 30, 1998, 3:00:00 AM12/30/98
to
vas...@rocketmail.com writes:

Consider this in OOPS

Is that a language? I don't think so. It's more of an exclamation,
really.

class abc{
private a1;
protected a2;
public method1(){
//method1 implemttion
}
public method2(){
//method2 implemttion
}

}

can we achieve the same in C.

You can't do exactly the same thing. If you ignore the
public/private/protected qualifiers you can come pretty close:

typedef struct abc {
int a1;
int a2;
int (*method1) (struct abc *this);
int (*method2) (struct abc *this);
} abc;

if you supply a constructor to fill in values for method1 and
method2. Or if you want to be more accurate:

struct abc_class {
int (*method1) (struct abc *this);
int (*method2) (struct abc *this);
};

typedef struct abc {
struct abc_class *class;
int a1;
int a2;
} abc;

Kindly let me know how at vas...@rocketmail.com

No.
--
"In My Egotistical Opinion, most people's C programs should be indented six
feet downward and covered with dirt." -- Blair P. Houghton
Please: do not email me copies of your posts to comp.lang.c
do not ask me C questions via email; post them instead

David M. Cook

unread,
Dec 31, 1998, 3:00:00 AM12/31/98
to
On Wed, 30 Dec 1998 05:57:04 GMT, vas...@rocketmail.com
<vas...@rocketmail.com> wrote:

> protected a2;


>can we achieve the same in C.

Well, there's no such thing as protected in C, although I suppose you could
use a naming convention to indicate names that should be "protected."

You can use C's static keyword to make names private to a file.

static void mysqare(int n)
{
return n*n;
}

would make foo "private" to the C file it is in.

You can do object based programming in C using abstract data types. Here's
an example for a circular linked list:

/* clist.h */

/* clist_cdt is the concrete type; its definition is deferred to clist.c.
* C allows the definition of a pointer to a struct to be deferred.
* The client will not have access to the details of the clist_cdt struct. */

typedef struct clist_adt *clist_cdt;

void clist_new(void);
void clist_free(clist_adt);
void clist_insert(clist_adt, void * item);
/* other prototypes */


/* clist.c */

struct node
{
void * info;
struct node *next;
}

typedef struct node *nodeptr;

/* Actual implementation of the clist_cdt struct. */

struct clist_cdt
{
nodeptr first, last;
}

/* Body of clist functions. */


A fragment showing the use of the ADT:

clist_adt cl;

cl = clist_new();
clist_insert(cl, item);

Of course, you don't get inheritance with this method. For a more detailed
explanation see

http://www.accu.org/acornsig/public/articles/oop_c.html
http://www.ccs.neu.edu/home/cloder/cscene/CS1/CS1-02.html

And also the book _Programming Abstractions in C_ by Eric S. Roberts.

I've used this method to structure my own programs. I find the discipline
it enforces to be helpful.

Dave Cook

0 new messages