Inheritance/Single/C: Difference between revisions
→Manual Object System
m (moved Inheritance/C to Inheritance/Single/C) |
|||
Line 1:
==Manual Object System==
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">
#ifndef _ANIMAL_H
#define _ANIMAL_H
Line 14 ⟶ 15:
extern Animal ObjClone(Animal a);
extern void ObjDestroy(Animal a);
#endif
</syntaxhighlight>
<
#include "Animal.h"
typedef struct sclass *Class;
Line 42 ⟶ 44:
extern void ClassRecInit(Class c);
extern void DfltClsInit(Class c);
extern sClass boc;
</syntaxhighlight>
<lang c>/* Animal.c */▼
#include "AnimalProt.h"
Line 121 ⟶ 125:
sClass boc = { sizeof(SObject), "Animal", NULL, &baseClsRecInit,
&baseClone, &baseSpeak, NULL };
Class AnimalClass = &boc;
</syntaxhighlight>
<syntaxhighlight lang="c">
#include "AnimalProt.h"
Line 141 ⟶ 147:
extern void InitDog(DogPart *dogp, char *name, char *color, double weight);
extern sClass dogc;
</syntaxhighlight>
<
/** * * * * * * * * * * * * * * * * * *
* Dog - a class derived from Animal
Line 188 ⟶ 195:
}
return (Animal)dog;
}
</syntaxhighlight>
<lang c>/* Dog.h */▼
<syntaxhighlight lang="c">
#ifndef _DOG_H
#define _DOG_H
#include "Animal.h"
extern Animal NewDog(char *name, char *color, double weight);
#endif
</syntaxhighlight>
<syntaxhighlight lang="c">
#include "DogProt.h"
/** * * * * * * * * * * * * * * * * * *
Line 214 ⟶ 226:
};
extern sClass labc;
</syntaxhighlight>
<lang c>/* Lab.c */▼
<syntaxhighlight lang="c">
#include "LabProt.h"
Line 245 ⟶ 260:
}
return (Animal)dog;
}</
<lang c>/* Lab.h */▼
<syntaxhighlight lang="c">
#include "Dog.h"
extern Animal NewLab(char *name, char *color, double weight);
</syntaxhighlight>
<syntaxhighlight lang="c">
/** * * * * * * * * * * * * * * * * * *
* Collie - a class derived from Dog
Line 268 ⟶ 287:
};
extern sClass colliec;
</syntaxhighlight>
<lang c>/* Collie.c */▼
<syntaxhighlight lang="c">
#include "CollieProt.h"
Line 299 ⟶ 321:
}
return (Animal)dog;
}
</syntaxhighlight>
<lang c>/* Collie.h */▼
<syntaxhighlight lang="c">
#include "Dog.h"
extern Animal NewCollie(char *name, char *color, double weight, double height);
</syntaxhighlight>
<syntaxhighlight lang="c">
#include "AnimalProt.h"
/* * * * * * * * * * * * */
Line 322 ⟶ 349:
};
extern sClass catcls;
</syntaxhighlight>
<lang c>/* Cat.c */▼
<syntaxhighlight lang="c">
#include "CatProt.h"
Line 372 ⟶ 402:
}
/** * Now wasn' that fun. * **/
</syntaxhighlight>
<lang c>/* Cat.h */▼
<syntaxhighlight lang="c">
#include "Animal.h"
extern Animal NewCat(char *name, char *color, int age);
</syntaxhighlight>
<syntaxhighlight lang="c">
#include "Lab.h"
#include "Collie.h"
Line 399 ⟶ 434:
return 0;
}</
|