Re: RE:How to realize the OOP in C?

From: xarax (xarax_at_email.com)
Date: 01/19/04


Date: Mon, 19 Jan 2004 05:27:33 GMT


"pvii" <pvii007@eastday.com> wrote in message news:bufo8o$hr2$1@mail.cn99.com...
> I am reading "concepts of programming languages".
> I think the oop is just a concept.
> #include <stdio.h>
> #define new /*"new"keyword,but it is no usage */
> typedef
> struct o{
> /* data of object*/
> char *s;
> int *i;
> int n;
>
> /* here two function point and I think they are more like abstract
> methords.
> However, we can see them as polymorphism
> */
> void (*toString)(char *,int *);
> void (*init)(char *,int *,int );
>
> /* "extend" is used to realize inheritance*/
> struct o *extend;
> } object;
>
> void tS(char *s,int *n)
> {
> int i;
> printf("My name is %s\n",s);
> printf("number is %d\n",*n);
> }
> void IT(char *a,int *b,int n)
> {
> a=(char*)malloc(n*sizeof(char));
> b=(int*)malloc(sizeof(int));
> }
> /* abstract methord are initialized */
> void createp(object *p)
> {
> p->toString=tS;
> p->init=IT;
> }
> object *Object(int n)
> {
> object *p;
> p=(object*)malloc(sizeof(object));
> createp(p);
> p->s="object";
> *(p->i)=n;
> return p;
> }
> main()
> {
> object *demo;
> demo=new Object(1);
> demo->init(demo->s,demo->i, 10);
> demo->toString(demo->s,demo->i);
> getch();
> }

Probably way off topic here.

Generally, you want the object instance to
be a pointer to a standard structure like:

================================================
/* forward reference to the object structure */
typedef struct object_of_fubar * ObjectOfFubarP;

typedef struct class_of_fubar
{
    /* instance method: gorko */
    int (*gorko)(ObjectOfFubarP,char*);

    /* instance method: snafu */
    void (*snafu)(ObjectOfFubarP,int,int);
} ClassOfFubar, * ClassOfFubarP;

typedef struct instance_data_of_fubar
{
    /* whatever instance data you want */
    int snarf;
    short grib;
} InstanceDataOfFubar, * InstanceDataOfFubarP;

typedef struct object_of_fubar
{
    /* points to class structure */
    ClassOfFubarP classOfFubarP;

    /* points to the object's instance data */
    InstanceDataOfFubarP instanceDataOfFubarP;
} ObjectOfFubar;
================================================

Instance methods always receive their first
parameter as a pointer to the object that
was used to invoke them (ObjectOfFubarP),
followed by any other parameters they need.

Instance methods are defined as function
pointers in the class structure, so they
have a local namespace. Multiple classes
can use the same function names, because
they are simply fields within the class
structure (defined as function pointer types).

The object reference points to the structure
that has two pointers: Pointer to the class
structure and pointer to the instance data
for the object.

Single inheritance is simply extending these
structures by adding new methods or new instance
fields onto the end of the parent structure. When
an object is instantiated by a constructor function,
space is allocated for the instance data and the
object structure, then the pointers are filled-in.
Note that the class structure is allocated only
once and a pointer to it is stuffed into each object
that is allocated. The constructor uses a singleton
class structure that is specific to its class, and
it must call its parent constructor to let the parent
fill-in its own fields before the constructor assigns
values for its own extensions.

Multiple inheritance is more complicated, but it
is still possible to resolve a method call in
constant time.

There's much more to it, but that's the
gist of it.

-- 
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS!
Are ISV upgrade fees too high? Check our custom product development!


Relevant Pages