用C语言模拟面向对象

 

用C语言模拟面向对象

分类: C语言算法   180人阅读  评论(0)  收藏  举报

 这个代码是修改自lwoopc,截取其中一部分,然后添加一部分。

在VC6.0和gcc可编译,大家可以从程序代码中的"//文件开始部分"开始看


CTOR是构造,DTOR是析构


  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3.   
  4.   
  5. #define CLASS(type)                 \  
  6. typedef struct type type;               \  
  7. type* type##_new( );            \  
  8. void type##_ctor(type* t);          \  
  9. void type##_dtor(type* t);          \  
  10. void type##_delete(type* t);            \  
  11. struct type  
  12.   
  13.   
  14.   
  15. #define CTOR(type,sencentes)                        \  
  16.   type* type##_new() {                          \  
  17.   struct type *cthis;                           \  
  18.   cthis = (struct type*)malloc(sizeof(struct type));            \  
  19.   if(!cthis)                                \  
  20.   {                                 \  
  21.    return 0;                                \  
  22.   }                                 \  
  23.   type##_ctor(cthis);                           \  
  24.   return cthis;                             \  
  25.  }                                  \  
  26.                                     \  
  27. void type##_ctor(type* cthis) {sencentes;}          
  28.      
  29.   
  30.   
  31.     
  32.   
  33. #define DTOR(type,sencentes)                    \  
  34. void type##_delete(type* cthis)                 \  
  35. {                               \  
  36.         type##_dtor(cthis);                 \  
  37.     free(cthis);                        \  
  38. }                               \  
  39. void type##_dtor(type* cthis){sencentes;}     
  40.   
  41.   
  42.   
  43. #define SET(name,value) ( cthis -> name ) = (value)  
  44.   
  45. #define FUN(retType,funName) retType(* funName)  
  46. #define CONNECT(funName) ( cthis -> funName ) = (& _##funName)  
  47. #define INCALL(funName)  (* cthis->funName)      //在成员函数内调用其他函数  
  48. #define CALL(this,funName) (* this->funName)  
  49.   
  50.   
  51.   
  52.   
  53. //文件开始部分  
  54. void _coutSome(char * str)          //在原来函数名前加上_,和CONNECT宏相关  
  55. {  
  56.     printf("%s\n",str);  
  57. }  
  58.   
  59.   
  60. //haha 类  
  61. CLASS(haha){                //声明type##_new()和type##_ctor() 和 type##_delete()和type##_dtor() 函数  
  62.     #define MEMhaha int a;  \  
  63.             int b;    
  64.     FUN(void,coutSome)();       //没定义在MEMhaha里面,表示自己专用的  
  65.     MEMhaha  
  66. };                    
  67.   
  68. CTOR(haha,SET(a,10);  
  69.       SET(b,5);  
  70.       CONNECT(coutSome))        //定义type##_new()和type##_ctor()函数    括号内第二个参数中的语句用;号隔开  
  71. DTOR(haha,SET(a,0);  
  72.       INCALL(coutSome) ("dtoring..."))//定义type##_delete()和type##_dtor()函数    括号内第二个参数中的语句用;号隔开  
  73.   
  74.   
  75. //wuwu 类,只有部分成员继承于haha,构造和析构等不继承,因为担心内存地址错误【继承后的排布不同】  
  76. CLASS(wuwu){      
  77.     #define MEMwuwu MEMhaha \  
  78.             char c;  
  79.     MEMwuwu  
  80. };  
  81.   
  82. CTOR(wuwu, printf("I am ctoring...\n");  
  83.        SET(a,10);  
  84.        SET(c,'w') )     //语句用;号隔开  
  85. DTOR(wuwu, printf("I am dtoring...\n");  
  86.            SET(a,0) ;  
  87.        SET(c,'?') )  
  88.   
  89.   
  90. //erer 类,和wuwu一模一样,但是本身只是把wuwu中的成员链接过来  
  91. CLASS(erer) {  
  92.     #define MEMerer   
  93.     MEMwuwu  
  94. };  
  95.   
  96. CTOR(erer, printf("I am ctoring...\n");  
  97.        SET(a,10);  
  98.        SET(c,'w') )     //语句用;号隔开  
  99. DTOR(erer, printf("I am dtoring...\n");  
  100.        SET(a,0) ;   
  101.            SET(c,'?') )  
  102.   
  103.   
  104. void main()  
  105. {  
  106.   
  107.     // 需要自己new和delete哦,C语言只能先定义后使用,所以下面几行必须放在最开始  
  108.     wuwu *w;  
  109.     haha *h;  
  110.     erer *e;  
  111.   
  112.     printf("\n\n\n");  
  113.   
  114.     h = haha_new();  
  115.   
  116.     CALL(h,coutSome)("newed");  
  117.   
  118.     printf("new:%d\n",h->a);  
  119.     printf("new:%d\n",h->b);  
  120.   
  121.     h->a  = 100;  
  122.     printf("set value:%d\n",h->a);  
  123.   
  124.     haha_delete(h);         //test  
  125.     printf("delete:%d\n",h->a);  //test  
  126.   
  127.       
  128.   
  129.     printf("\n\n\n");  
  130.   
  131.     w = wuwu_new();         //继承  
  132.   
  133.     printf("new:%d\n",w->a);  
  134.     printf("new:%c\n",w->c);  
  135.   
  136.     w->a  = 100;  
  137.     w->c  = 'c';  
  138.     printf("set value:%d\n",w->a);  
  139.     printf("set value:%c\n",w->c);  
  140.   
  141.     wuwu_delete(w);         //test,这个正常情况下不被调用  
  142.     printf("dtor:%d\n",w->a);  
  143.     printf("dtor:%c\n",w->c);  
  144.   
  145.   
  146.     printf("\n\n\n");  
  147.   
  148.     e = erer_new();           
  149.   
  150.     printf("new:%d\n",e->a);  
  151.     printf("new:%c\n",e->c);  
  152.   
  153.     e->a  = 100;  
  154.     e->c  = 'c';  
  155.     printf("set value:%d\n",e->a);  
  156.     printf("set value:%c\n",e->c);  
  157.   
  158.     erer_delete(e);  
  159.     printf("dtor:%d\n",e->a);    //test  
  160.     printf("dtor:%c\n",e->c);  
  161.   
  162.     printf("\n\n\n");  
  163.   
  164. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值