Advertisement
xeritt

OOP in C simple example 2

Feb 23rd, 2021 (edited)
988
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.88 KB | None | 0 0
  1. /////////////////////////////////////////////////file class.h
  2. #ifndef CLASS_H_   /* Include guard */
  3. #define CLASS_H_
  4. #define CLASS typedef struct
  5. #define NEW(type) type##_new()
  6. #define NEW_1(type, param) type##_new_1(param)
  7. #endif
  8.  
  9. /////////////////////////////////////////////////file base.h
  10. #ifndef BASE_H_   /* Include guard */
  11. #define BASE_H_
  12. #include "class.h"
  13. typedef struct _public public_t;
  14.  
  15. #define GET_PUB(self) (public_t*)(self + (((base_t*)self)->size - sizeof(public_t)));
  16.  
  17. typedef struct _public{
  18.     public_t* (*get)(void* self);
  19.     void (*draw)(void* self);
  20.     int (*getId)(void* self);
  21.     void (*setPoint)(void* self, int x, int y);
  22.     void (*inherits)(public_t *pub_src, public_t *pub_dest);
  23. } public_t;
  24.  
  25.  
  26. CLASS base{
  27.     int size;
  28.     int id;
  29.     public_t pub;
  30. } __attribute__((packed)) base_t;
  31. void base_t_new(base_t* bse);
  32. #endif
  33. /////////////////////////////////////////////////file base.c
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include "base.h"
  37.  
  38. static int getId(void* self){
  39.     printf("base:");
  40.     fflush(stdout);
  41.     return ((base_t*)self)->id;
  42. }
  43.  
  44. static public_t* get(void* self){
  45.     int size = ((base_t*)self)->size - sizeof(public_t);
  46.     return self + size;
  47. }
  48.  
  49. static void inherits(public_t* pub_src, public_t* pub_dest){
  50.     printf("inherits base\n");
  51.     fflush(stdout);
  52.     pub_dest->getId = pub_src->getId;
  53.     pub_dest->get = pub_src->get;
  54.     //pub_dest.inherits = pub_src.inherits;
  55. }
  56.  
  57. void base_t_new(base_t* bse){
  58.     printf("new base\n");
  59.     fflush(stdout);
  60.     bse->size = sizeof(base_t);//init size object
  61.     bse->pub.getId = getId; //link with method
  62.     bse->pub.get = get; //link with method
  63.     bse->pub.inherits = inherits; //link with method
  64. }
  65. /////////////////////////////////////////////////file child.h
  66. #ifndef CHILD_H_   /* Include guard */
  67. #define CHILD_H_
  68. #include "base.h"
  69. typedef struct child{
  70.     base_t super;
  71.     char name[10];
  72.     public_t pub;
  73. } __attribute__((packed)) child_t;
  74. void child_t_new(child_t* chld);
  75. #endif
  76. /////////////////////////////////////////////////file child.c
  77. #include <stdlib.h>
  78. #include <stdio.h>
  79. #include "base.h"
  80. #include "child.h"
  81. static int getId(void* self){
  82.     printf("child:");
  83.     fflush(stdout);
  84.     base_t *bse = &((child_t*)self)->super;
  85.     return bse->pub.getId(bse);
  86. }
  87. static void inherits(public_t *pub_src, public_t* pub_dest){
  88.     printf("inherits child\n");
  89.     fflush(stdout);
  90.     pub_dest->getId = pub_src->getId;
  91. }
  92.  
  93. void child_t_new(child_t *chld){
  94.     printf("new child \n");
  95.     fflush(stdout);
  96.     base_t_new(&chld->super); //call base constructor
  97.     base_t* bse = (base_t*)chld;
  98.     bse->size = sizeof(child_t); //init size object
  99.     chld->super.pub.inherits(&chld->super.pub, &chld->pub);//call base inherits
  100.     chld->pub.inherits = inherits; //inherits functions for child
  101.     chld->pub.getId = getId;
  102. }
  103. /////////////////////////////////////////////////file point.h
  104. #ifndef POINT_H_   /* Include guard */
  105. #define POINT_H_
  106. #include "child.h"
  107. ////////////// Point
  108. typedef struct point{
  109.     child_t super;
  110.         //      base_t super; //<-- link owner
  111.         //          //size int;<-- base    
  112.         //          //int id;<-- base
  113.         //          //public_t pub; <-- base
  114.         //      public_t pub;
  115.     int x, y; //new fields
  116.     public_t pub;
  117. } __attribute__((packed)) point_t;
  118.  
  119. void point_t_new(point_t* pt);
  120. #endif
  121. /////////////////////////////////////////////////file point.c
  122. #include <stdio.h>
  123. #include <stdlib.h>
  124. #include "base.h"
  125. #include "child.h"
  126. #include "point.h"
  127.  
  128. static int getId(void* self){
  129.     return 0;//((point_t*)self)->super->getId(((point_t*)self)->super);
  130. }
  131.  
  132. static void setPoint(void* self, int x, int y){
  133.     point_t* pt = (point_t*)self;
  134.     pt->x = x;
  135.     pt->y = y;
  136. }
  137.  
  138. static void inherits(public_t *pub_src, public_t *pub_dest){
  139.     printf("inherits point\n");
  140.     fflush(stdout);
  141.     pub_dest->setPoint = pub_src->setPoint;
  142. }
  143.  
  144. void point_t_new(point_t* pt){
  145.     printf("new point \n");
  146.     fflush(stdout);
  147.     child_t_new(&pt->super); //call base constructor
  148.     base_t* bse = (base_t*)pt;
  149.     bse->size = sizeof(point_t); //init size object
  150.     pt->super.pub.inherits(&pt->super.pub, &pt->pub); //inherits functions
  151.     pt->pub.setPoint = setPoint;    //set new functions
  152.     //pt->pub.getId = pt->super.pub.getId;
  153. }
  154. /////////////////////////////////////////////////file main.c
  155. #include <stdio.h>
  156. #include <stdlib.h>
  157. #include <stddef.h>
  158. #include "base.h"
  159. #include "child.h"
  160. #include "point.h"
  161. #define N 3
  162. int main(int argc, char **argv){
  163.     base_t *bse = malloc(sizeof(base_t));
  164.     child_t chl;
  165.     point_t pt;
  166.     bse->id = 10;
  167.     chl.super.id = 1;
  168.     pt.super.super.id = 12; //may use var before call constructor
  169.  
  170.     base_t_new(bse); //call constructor
  171.     child_t_new(&chl);
  172.     point_t_new(&pt);
  173.    
  174.     void* mass[N] = {&chl, bse, &pt};
  175.     for (int i=0; i<N; i++){
  176.         public_t *pub = ((base_t*)mass[i])->pub.get(mass[i]);
  177.         //public_t *pub = GET_PUB(mass[i]); //may use macros GET_PUB
  178.         int id = pub->getId(mass[i]);
  179.         printf("id=%i\n", id);
  180.     }
  181.     pt.pub.setPoint(&pt, 1, 1);
  182.  
  183.     free(bse);
  184.     return 0;
  185. }
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement