Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- typedef char const* (*PTRFUN)();
- typedef struct {
- PTRFUN *vtable;
- char* imeLjubimca;
- } Animal;
- typedef void (*funptrVF)(Animal*);
- void AnimalPrintGreeting(Animal *obj) {
- printf("%s pozdravlja: %s!", obj->imeLjubimca, obj->vtable[0]());
- }
- void AnimalPrintMenu(Animal *obj) {
- printf("%s voli %s!", obj->imeLjubimca, obj->vtable[1]());
- }
- PTRFUN AnimalVTable[2] = {
- (PTRFUN)NULL,
- (PTRFUN)NULL
- };
- AnimalInit(Animal* obj) {
- obj->vtable = AnimalVTable;
- }
- //|||||||||||||||||||||||||||||||||||||
- //razred Dog
- //|||||||||||||||||||||||||||||||||||||
- typedef struct{
- PTRFUN *vtable;
- } Dog;
- char const* dogGreet(void){
- return "vau!";
- }
- char const* dogMenu(void){
- return "kuhanu govedinu";
- }
- PTRFUN DogVTable[2] = {
- (PTRFUN)dogGreet,
- (PTRFUN)dogMenu
- };
- Animal* createDog(char* imeLjubimca) {
- Animal* obj = (Animal*)malloc(sizeof(Dog));
- constructDog(obj, &imeLjubimca);
- return obj;
- }
- void constructDog(Animal* obj, char* imeLjubimca) {
- AnimalInit((Animal*) obj);
- obj->vtable = DogVTable;
- obj->imeLjubimca = imeLjubimca;
- }
- //|||||||||||||||||||||||||||||||||||||
- //razred Cat
- //|||||||||||||||||||||||||||||||||||||
- typedef struct{
- PTRFUN *vtable;
- } Cat;
- char const* catGreet(void){
- return "mijau!";
- }
- char const* catMenu(void){
- return "konzerviranu tunjevinu";
- }
- PTRFUN CatVTable[2] = {
- (PTRFUN)catGreet,
- (PTRFUN)catMenu
- };
- Animal* createCat(char* imeLjubimca) {
- Animal* obj = (Animal*)malloc(sizeof(Cat));
- constructCat(obj, &imeLjubimca);
- return obj;
- }
- void constructCat(Animal* obj, char* imeLjubimca) {
- AnimalInit((Animal*) obj);
- obj->vtable = CatVTable;
- obj->imeLjubimca = imeLjubimca;
- }
- void testAnimals(void){
- struct Animal* p1 = createDog("Hamlet");
- struct Animal* p2 = createCat("Ofelija");
- struct Animal* p3 = createDog("Polonije");
- animalPrintGreeting(p1);
- animalPrintGreeting(p2);
- animalPrintGreeting(p3);
- animalPrintMenu(p1);
- animalPrintMenu(p2);
- animalPrintMenu(p3);
- free(p1); free(p2); free(p3);
- }
- int main(){
- testAnimals();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement