1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct _Animal Animal;
typedef struct _Chat Chat;
// Classe abstraite
struct _Animal {
void* pDerivedObj;
void (*dit_bonjour) (Animal* self);
};
// Classe normale
struct _Chat {
Animal* parent;
};
Animal* animal_construct ()
{
Animal* pObj = NULL;
pObj = (Animal*)malloc(sizeof(Animal));
return pObj;
}
void animal_dit_bonjour (Animal* obj) {
obj->dit_bonjour(obj);
}
void chat_dit_bonjour (Animal* self)
{
printf("Miaou Miaou Miaou\n");
}
Chat* new_Chat()
{
Chat* chat = NULL;
Animal* animal = NULL;
animal = animal_construct ();
chat = malloc(sizeof(chat));
chat->parent = animal;
animal->pDerivedObj = chat;
animal->dit_bonjour = chat_dit_bonjour;
return chat;
}
int main()
{
Chat* Obj = new_Chat();
animal_dit_bonjour ((Animal*) Obj->parent);
return 0;
} |
Partager