Advertisement
Guest User

Untitled

a guest
Mar 20th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4.  
  5. typedef char const* (*PTRFUN)();
  6.  
  7. typedef struct {
  8.     PTRFUN *vtable;
  9.     char* imeLjubimca;
  10. } Animal;
  11.  
  12. typedef void (*funptrVF)(Animal*);
  13.  
  14. void AnimalPrintGreeting(Animal *obj) {
  15.     printf("%s pozdravlja: %s!", obj->imeLjubimca, obj->vtable[0]());
  16. }
  17.  
  18. void AnimalPrintMenu(Animal *obj) {
  19.     printf("%s voli %s!", obj->imeLjubimca, obj->vtable[1]());
  20. }
  21.  
  22. PTRFUN AnimalVTable[2] = {
  23.         (PTRFUN)NULL,
  24.         (PTRFUN)NULL
  25. };
  26.  
  27. AnimalInit(Animal* obj) {
  28.     obj->vtable = AnimalVTable;
  29. }
  30.  
  31. //|||||||||||||||||||||||||||||||||||||
  32. //razred Dog
  33. //|||||||||||||||||||||||||||||||||||||
  34.  
  35. typedef struct{
  36.     PTRFUN *vtable;
  37. } Dog;
  38.  
  39. char const* dogGreet(void){
  40.     return "vau!";
  41. }
  42.  
  43. char const* dogMenu(void){
  44.     return "kuhanu govedinu";
  45. }
  46.  
  47. PTRFUN DogVTable[2] = {
  48.         (PTRFUN)dogGreet,
  49.         (PTRFUN)dogMenu
  50. };
  51.  
  52. Animal* createDog(char* imeLjubimca) {
  53.     Animal* obj = (Animal*)malloc(sizeof(Dog));
  54.     constructDog(obj, &imeLjubimca);
  55.     return obj;
  56. }
  57.  
  58. void constructDog(Animal* obj, char* imeLjubimca) {
  59.     AnimalInit((Animal*) obj);
  60.     obj->vtable = DogVTable;
  61.     obj->imeLjubimca = imeLjubimca;
  62. }
  63.  
  64.  
  65. //|||||||||||||||||||||||||||||||||||||
  66. //razred Cat
  67. //|||||||||||||||||||||||||||||||||||||
  68.  
  69.  
  70. typedef struct{
  71.     PTRFUN *vtable;
  72. } Cat;
  73.  
  74. char const* catGreet(void){
  75.     return "mijau!";
  76. }
  77.  
  78. char const* catMenu(void){
  79.     return "konzerviranu tunjevinu";
  80. }
  81.  
  82. PTRFUN CatVTable[2] = {
  83.         (PTRFUN)catGreet,
  84.         (PTRFUN)catMenu
  85. };
  86.  
  87. Animal* createCat(char* imeLjubimca) {
  88.     Animal* obj = (Animal*)malloc(sizeof(Cat));
  89.     constructCat(obj, &imeLjubimca);
  90.     return obj;
  91. }
  92.  
  93. void constructCat(Animal* obj, char* imeLjubimca) {
  94.     AnimalInit((Animal*) obj);
  95.     obj->vtable = CatVTable;
  96.     obj->imeLjubimca = imeLjubimca;
  97. }
  98.  
  99. void testAnimals(void){
  100.     struct Animal* p1 = createDog("Hamlet");
  101.     struct Animal* p2 = createCat("Ofelija");
  102.     struct Animal* p3 = createDog("Polonije");
  103.  
  104.     animalPrintGreeting(p1);
  105.     animalPrintGreeting(p2);
  106.     animalPrintGreeting(p3);
  107.  
  108.     animalPrintMenu(p1);
  109.     animalPrintMenu(p2);
  110.     animalPrintMenu(p3);
  111.  
  112.     free(p1); free(p2); free(p3);
  113. }
  114.  
  115. int main(){
  116.     testAnimals();
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement