C语言中的面向对像

//使用C语言实现C++面向对象的功能
  1. /** 
  2.  * 用 c 语言实现类的继承,并且可用父类指针操作子类对像的一个测试例子 
  3.  *  
  4.  *  @Author: tomken.zhang 
  5.  *    
  6.  */  
  7.   
  8. #include <stdio.h>     
  9. #include <stdlib.h>  
  10. #include <string.h>  
  11.   
  12. // 定义一个动物类,有二个方法  
  13. struct v_animal_ptr {  
  14.     const char* (*get_name)(); // 获取名字  
  15.     void (*shout)(int volume); // 喊叫  
  16. };  
  17.   
  18. typedef struct v_animal_ptr animal_vptr;  
  19.   
  20. typedef const char* (*GET_NAME)();  
  21. typedef void        (*SHOUT)(int volume);  
  22.   
  23. // 定义一个动物类   
  24. struct animal {  
  25.     animal_vptr *vptr;  
  26. };  
  27.   
  28. static const char* animal_get_name(const void *class)  
  29. {  
  30.     return (*((struct animal*)class)->vptr->get_name)();  
  31. }  
  32.   
  33. static void animal_shout(const void *classint volume)  
  34. {  
  35.     return (*((struct animal*)class)->vptr->shout)(volume);  
  36. }  
  37.   
  38.   
  39. // 定义一个猪类,有一个高度值。  
  40. struct pig {  
  41.     animal_vptr *vptr;  
  42.     int    height;  
  43. };  
  44.   
  45. static const char* pig_get_name() {  
  46.     return "i am pig";  
  47. };  
  48.   
  49. static void pig_shout(int volume) {  
  50.     printf("heng heng %d\n", volume);  
  51. };  
  52.   
  53. // 定义猪的实现方法  
  54. static animal_vptr pig_vptr =   
  55. {  
  56.     (GET_NAME)  &pig_get_name,  
  57.     (SHOUT)     &pig_shout,  
  58. };  
  59.   
  60. // 定义一个狗类,有一个颜色值。  
  61. struct dog {  
  62.     animal_vptr *vptr;  
  63.     int    color;  
  64. };  
  65.   
  66. static const char* dog_get_name() {  
  67.     return "i am dog";  
  68. };  
  69.   
  70. static void dog_shout(int volume) {  
  71.     printf("wang wang %d\n", volume);  
  72. };  
  73.   
  74. static animal_vptr dog_vptr =   
  75. {  
  76.     (GET_NAME)  &dog_get_name,  
  77.     (SHOUT)     &dog_shout,  
  78. };  
  79.   
  80. static struct pig* init_pig() {  
  81.     struct pig* anim = (struct pig*) malloc(sizeof(struct pig));  
  82.     anim->vptr = &pig_vptr;  
  83.     anim->height = 10;  
  84.       
  85.     return anim;  
  86. }  
  87.   
  88. static struct dog* init_dog() {  
  89.     struct dog* anim = (struct dog*) malloc(sizeof(struct dog));  
  90.     anim->vptr = &dog_vptr;  
  91.     anim->color = 255;  
  92.       
  93.     return anim;  
  94. }  
  95.   
  96. int main()    
  97. {  
  98.     struct animal *anim = NULL;  
  99.     struct pig    *pig  = NULL;   
  100.     struct dog    *dog  = NULL;  
  101.   
  102.     // pig 子类  
  103.     pig = init_pig();  
  104.     printf("name=%s\n", animal_get_name(pig));  
  105.       
  106.     printf("------------------------------------\n");  
  107.       
  108.     // dog 子类  
  109.     dog = init_dog();  
  110.     printf("name=%s\n", animal_get_name(dog));  
  111.       
  112.     printf("------------------------------------\n");  
  113.   
  114.     // 转向 动物父类进行操作  
  115.     anim = (struct animal *)pig;  
  116.     printf("name=%s\n", animal_get_name(anim));  
  117.     animal_shout(anim, 20);  
  118.       
  119.     printf("------------------------------------\n");  
  120.       
  121.     // 转向 动物父类进行操作  
  122.     anim = (struct animal *)dog;  
  123.     printf("name=%s\n", animal_get_name(anim));  
  124.     animal_shout(anim, 100);  
  125.       
  126.     return 0;    
  127. }    


运行结果:

  

[plain]  view plain copy print ?
  1. $ ./test  
  2. name=i am pig  
  3. ------------------------------------  
  4. name=i am dog  
  5. ------------------------------------  
  6. name=i am pig  
  7. heng heng 20  
  8. ------------------------------------  
  9. name=i am dog  
  10. wang wang 100  

 

上面的例子有点长,可以仔细看看,如果不关心实现细节,可以看一下 main 函数

确实可以模拟 C++ 类继承的方式进行操作了!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值