Inheritance/Single/C: Difference between revisions

(nes C Inheritance)
 
 
(4 intermediate revisions by 3 users not shown)
Line 1:
==Manual Object System==
<lang c>/* Animal.h */
Although [[C]] is not considered an OO language, you can do the following, which provides a semblance of an OO infrastructure allowing Inheritance and Polymorphism. The original [[C++]] was a front end which translated C++ code to C. (C++ was much simpler in those days.)"
<syntaxhighlight lang="c">
/* Animal.h */
#ifndef _ANIMAL_H
#define _ANIMAL_H
Line 12 ⟶ 15:
extern Animal ObjClone(Animal a);
extern void ObjDestroy(Animal a);
#endif</lang>
</syntaxhighlight>
 
<langsyntaxhighlight lang="c">/* AnimalProt.h */
#include "Animal.h"
typedef struct sclass *Class;
Line 20 ⟶ 24:
typedef char * (*SpeakFctn)(Animal s);
typedef void (*DestroyFctn)(Animal s);
typedef void (*ClsRecInit)(Class c);
 
typedef struct object {
Line 26 ⟶ 31:
 
typedef struct sclass {
size_t csize; /* size of the class instance */
char *cname; /* name of the class */
Class parent; /* parent class */
ClsRecInit crInit; /* handle method inheritance for class record */
 
CloneFctn clone; /* clone function */
Line 35 ⟶ 41:
} sClass;
 
#define O_INHERIT (void *)-1
extern sClass boc;</lang>
extern void ClassRecInit(Class c);
<lang c>/* Animal.c */
extern void DfltClsInit(Class c);
extern sClass boc;
</syntaxhighlight>
 
<syntaxhighlight lang="c">/* Animal.c */
#include "AnimalProt.h"
 
Line 78 ⟶ 89:
{ if (s) obj_del( s, s->class ); }
 
void ClassRecInit( Class c)
{
if (c->crInit) {
(*c->crInit)(c);
c->crInit = NULL;
}
}
static
void baseClsRecInit(Class c )
{
if ((O_INHERIT == c->speak) && c->parent)
c->speak = c->parent->speak;
}
void DfltClsInit(Class c)
{
if (c->parent && c->parent->crInit) {
ClassRecInit(c->parent);
}
printf("Initializing class %s\n", c->cname);
baseClsRecInit(c);
}
/* * * * * * */
static
Line 91 ⟶ 123:
}
 
sClass boc = { sizeof(SObject), "Animal", NULL, &baseClsRecInit,
&baseClone, &baseSpeak, NULL };
Class AnimalClass = &boc;</lang>
</syntaxhighlight>
 
<syntaxhighlight lang="c">
<lang c>/* DogProt.h */
/* DogProt.h */
#include "AnimalProt.h"
 
Line 110 ⟶ 144:
DogPart dog;
};
extern sClass dogc;</lang>
 
extern void InitDog(DogPart *dogp, char *name, char *color, double weight);
<lang c>/* Dog.c */
 
extern sClass dogc;
</syntaxhighlight>
 
<syntaxhighlight lang="c">/* Dog.c */
/** * * * * * * * * * * * * * * * * * *
* Dog - a class derived from Animal
Line 136 ⟶ 174:
}
 
sClass dogc = { sizeof(struct sDog), "Dog", &boc, &DfltClsInit,
&dogClone, &dogSpeak, NULL };
Class DogClass = &dogc;
 
 
static
void InitDog(DogPart *dogp, char *name, char *color, double weight)
{
Line 152 ⟶ 190:
Dog dog = (Dog)malloc(DogClass->csize);
if (dog) {
ClassRecInit(DogClass);
dog->class = DogClass;
InitDog(&dog->dog, name, color, weight);
}
return (Animal)dog;
}
}</lang>
</syntaxhighlight>
<lang c>/* Dog.h */
 
<syntaxhighlight lang="c">
/* Dog.h */
#ifndef _DOG_H
#define _DOG_H
#include "Animal.h"
extern Animal NewDog(char *name, char *color, double weight);
#endif</lang>
</syntaxhighlight>
 
<syntaxhighlight lang="c">
<lang c>/* LabProt.h */
/* LabProt.h */
#include "DogProt.h"
/** * * * * * * * * * * * * * * * * * *
Line 182 ⟶ 226:
};
 
extern sClass labc;</lang>
</syntaxhighlight>
<lang c>/* Lab.c */
 
<syntaxhighlight lang="c">
/* Lab.c */
#include "LabProt.h"
 
Line 199 ⟶ 246:
}
 
sClass labc = { sizeof(struct sLab), "Lab", &dogc, &DfltClsInit,
&labClone, &dogSpeakO_INHERIT, NULL };
Class LabClass = &labc;
 
Line 207 ⟶ 254:
Lab dog = (Lab)malloc(LabClass->csize);
if (dog) {
ClassRecInit(LabClass);
dog->class = LabClass;
InitDog( &dog->dog, name, color, weight);
Line 212 ⟶ 260:
}
return (Animal)dog;
}</langsyntaxhighlight>
 
<lang c>/* Lab.h */
<syntaxhighlight lang="c">
/* Lab.h */
#include "Dog.h"
extern Animal NewLab(char *name, char *color, double weight);</lang>
</syntaxhighlight>
 
<syntaxhighlight lang="c">
<lang c>/* CollieProt.h */
/* CollieProt.h */
/** * * * * * * * * * * * * * * * * * *
* Collie - a class derived from Dog
Line 235 ⟶ 287:
};
 
extern sClass colliec;</lang>
</syntaxhighlight>
<lang c>/* Collie.c */
 
<syntaxhighlight lang="c">
/* Collie.c */
#include "CollieProt.h"
 
Line 252 ⟶ 307:
}
 
sClass colliec = { sizeof(struct sCollie), "Collie", &dogc, &DfltClsInit,
&collieClone, &dogSpeakO_INHERIT, NULL };
Class CollieClass = &colliec;
 
Line 260 ⟶ 315:
Collie dog = (Collie)malloc(CollieClass->csize);
if (dog) {
ClassRecInit(CollieClass);
dog->class = CollieClass;
InitDog( &dog->dog, name, color, weight);
Line 265 ⟶ 321:
}
return (Animal)dog;
}
}</lang>
</syntaxhighlight>
<lang c>/* Collie.h */
 
<syntaxhighlight lang="c">
/* Collie.h */
#include "Dog.h"
extern Animal NewCollie(char *name, char *color, double weight, double height);</lang>
</syntaxhighlight>
 
<syntaxhighlight lang="c">
<lang c>/* CatProt.h */
/* CatProt.h */
#include "AnimalProt.h"
/* * * * * * * * * * * * */
Line 288 ⟶ 349:
};
 
extern sClass catcls;</lang>
</syntaxhighlight>
<lang c>/* Cat.c */
 
<syntaxhighlight lang="c">
/* Cat.c */
#include "CatProt.h"
 
Line 320 ⟶ 384:
}
 
sClass catcls = { sizeof(struct sCat), "Cat", &boc, &DfltClsInit,
&catClone, &catSpeak, &catDel };
Class CatClass = &catcls;
Line 329 ⟶ 393:
if (cat) {
CatPart *catpart = &(cat->catpart);
ClassRecInit(CatClass);
cat->class = CatClass;
catpart->name = strdup(name);
Line 337 ⟶ 402:
}
 
/** * Now wasn' that fun. * **/</lang>
</syntaxhighlight>
<lang c>/* Cat.h */
 
<syntaxhighlight lang="c">
/* Cat.h */
#include "Animal.h"
extern Animal NewCat(char *name, char *color, int age);</lang>
</syntaxhighlight>
 
<syntaxhighlight lang="c">
<lang c>#include "Cat.h"
#include "Cat.h"
#include "Lab.h"
#include "Collie.h"
Line 364 ⟶ 434:
 
return 0;
}</langsyntaxhighlight>