当前位置:   article > 正文

面向对象-类-C语言实现_c实现类

c实现类

最近在看《C嵌入式编程设计模式》这本书时,发现书中介绍面向对象编程时用结构体去实现类和类的继承,正好前段时间也在学习C++的基础语法,反向思维去理解面向对象也挺有意思的,所以做一下笔记。

下面文章虽然实现了类,但和C++里面的还是有很大不同的,但是在日常写代码使用这种方式的话可以让代码更加的紧凑。

类的实现

使用结构体方式声明一个Queue类,类里面有对应的成员,成员方法使用函数指针方式进行封装,将其单独放到queue.h里面。

  1. #define QUEUE_SIZE 10
  2. typedef struct Queue Queue;
  3. //Queue类
  4. struct Queue
  5. {
  6. int buffer[QUEUE_SIZE]; //队列大小
  7. int head;//队头
  8. int size;//元素数量
  9. int tail;//队尾
  10. int (*isFull)(Queue * const me);//判断是否队满
  11. int (*isEmpty)(Queue * const me);//判断是否队空
  12. int (*getSize)(Queue * const me);//获取当前元素个数
  13. void (*insert)(Queue * const me,int k);//插入数据
  14. int (*remove)(Queue * const me);//删除数据
  15. };
  16. //构造函数和析构函数
  17. void Queue_Init(Queue *const me,int (*isFullfunction)(Queue * const me),
  18. int (*isEmptyfunction)(Queue * const me),
  19. int (*getSizefunction)(Queue * const me),
  20. void (*insertfunction)(Queue * const me,int k),
  21. int (*removefunction)(Queue * const me));
  22. void Queue_Cleanup(Queue *const me);
  23. //Queue类的成员函数
  24. int Queue_isFull(Queue * const me);
  25. int Queue_isEmpty(Queue * const me);
  26. int Queue_getSize(Queue * const me);
  27. void Queue_insert(Queue * const me,int k);
  28. int Queue_remove(Queue * const me);
  29. //创建类对象和删除
  30. Queue * Queue_Create(void);
  31. void Queue_Destory(Queue * const me);

 Queue类的成员函数实现过程放在queue.c里面

  1. #include "queue.h"
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. void Queue_Init(Queue *const me,int (*isFullfunction)(Queue * const me),
  6. int (*isEmptyfunction)(Queue * const me),
  7. int (*getSizefunction)(Queue * const me),
  8. void (*insertfunction)(Queue * const me,int k),
  9. int (*removefunction)(Queue * const me))
  10. {
  11. printf("Queue构造函数\r\n");
  12. me->head = 0;
  13. me->tail = 0;
  14. me->size = 0;
  15. me->isFull = isFullfunction;
  16. me->isEmpty = isEmptyfunction;
  17. me->getSize = getSizefunction;
  18. me->insert = insertfunction;
  19. me->remove = removefunction;
  20. }
  21. void Queue_Cleanup(Queue *const me)
  22. {
  23. printf("Queue析构函数\r\n");
  24. }
  25. int Queue_isFull(Queue * const me)
  26. {
  27. return ((me->head+1) % QUEUE_SIZE == me->tail);
  28. }
  29. int Queue_isEmpty(Queue * const me)
  30. {
  31. return me->tail == me->head;
  32. }
  33. int Queue_getSize(Queue * const me)
  34. {
  35. return me->size;
  36. }
  37. void Queue_insert(Queue * const me,int k)
  38. {
  39. if(!me->isFull(me))
  40. {
  41. me->buffer[me->head] = k;
  42. me->head = (me->head+1)%QUEUE_SIZE;
  43. ++me->size;
  44. }
  45. }
  46. int Queue_remove(Queue * const me)
  47. {
  48. int value = 9999;
  49. if(!me->isEmpty(me))
  50. {
  51. value = me->buffer[me->tail];
  52. me->tail = (me->tail+1) % QUEUE_SIZE;
  53. --me->size;
  54. }
  55. return value;
  56. }
  57. Queue * Queue_Create(void)
  58. {
  59. Queue* me= malloc(sizeof(Queue));
  60. if(me != NULL)
  61. {
  62. Queue_Init(me,Queue_isFull,Queue_isEmpty,Queue_getSize,Queue_insert,Queue_remove);
  63. }
  64. return me;
  65. }
  66. void Queue_Destory(Queue * const me)
  67. {
  68. if(me != NULL)
  69. {
  70. Queue_Cleanup(me);
  71. }
  72. free(me);
  73. }

在main.c实例化一个Queue对象进行队列的使用,书中例子往队列里面插入元素,再逐一进行删除

  1. #include "queue.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main()
  5. {
  6. int j,k,h,t;
  7. Queue * myQ = Queue_Create();
  8. k = 1000;
  9. for(j=0;j<QUEUE_SIZE;j++)
  10. {
  11. h = myQ->head;
  12. myQ->insert(myQ,k);
  13. printf("intserting %d ,at position %d, size %d\r\n",k--,h,myQ->getSize(myQ));
  14. }
  15. printf("\r\nintserting len:%d\r\n\r\n",myQ->getSize(myQ));
  16. for(j=0;j<QUEUE_SIZE;j++)
  17. {
  18. h = myQ->tail;
  19. k = myQ->remove(myQ);
  20. printf("removeting %d ,at position %d, size %d\r\n",k,h,myQ->getSize(myQ));
  21. }
  22. printf("\r\removeting len:%d\r\n",myQ->getSize(myQ));
  23. Queue_Destory(myQ);
  24. }

最后,在gcc编译结果如下: 

分析结果符合先进先出的队列数据结构特性,但是在插入第10个元素是没有成功的,导致后面删除第10个元素也是失败的,不知道是书的作者写错还是本人理解的错误。

总结

虽然说教科书把C语言定义为面向过程语言,C++定义为面对象语言,但是更多时候阅读开源的项目代码时,还是可以看到使用C语言去实现面向对象的开发。

备注:上述的例子来自于书籍《C嵌入式编程设计模式》,如有侵权联系删除,下附书本目录:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/43734
推荐阅读
相关标签
  

闽ICP备14008679号