当前位置:   article > 正文

C++之从一个实例看数据抽象与封装

C++之从一个实例看数据抽象与封装

1、用C的方式实现栈

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<assert.h>
  4. struct Link
  5. {
  6. int data;
  7. struct Link* next;
  8. }
  9. struct Stack
  10. {
  11. struct Link* head;
  12. int size;
  13. }
  14. void StackInit(struct Stack* stack)
  15. {
  16. stack->head = NULL;
  17. stack->size =0;
  18. }
  19. void StackPush(struct Stack* stack, const int data)
  20. {
  21. struct Link* node;
  22. node =(struct Link*)malloc(sizeof(struct Link));
  23. assert(node != ULL);
  24. node->data = data;
  25. node->next = stack->head;
  26. stack->head = node;
  27. ++stack->size;
  28. }
  29. int StackEmpty(struct Stack* stack)
  30. {
  31. return(stack->size ==0)
  32. }
  33. int StackPop(struct Stack* stack, int* data)
  34. {
  35. if(StackEmpty(stack))
  36. {
  37. return 0;
  38. }
  39. struct Link* tmp= stack->head;
  40. *data= stack->head->data;
  41. stack->head = stack->head->next;
  42. free(tmp);
  43. --stack->size;
  44. return 1;
  45. }
  46. void StackCleanup(struct Stack* stack)
  47. {
  48. struct Link* tmp;
  49. while(stack->head)
  50. {
  51. tmp = stack->head;
  52. stack->head = stack->head->next;
  53. free(tmp);
  54. }
  55. stack->size = 0;
  56. }
  57. int main(void)
  58. {
  59. struct Stack stack;
  60. StackInit(&stack);
  61. int i;
  62. for(i = 0; i < 5; i++)
  63. {
  64. StackPush(&stack, i);
  65. }
  66. while(!StackEmpty(&stack))
  67. {
  68. StackPop(&stack, &i);
  69. printf("%d", i);
  70. }
  71. printf("\n");
  72. return 0;
  73. }

2、用C++的方式实现栈

  1. class Stack
  2. {
  3. struct Link
  4. {
  5. int data_;
  6. Link* next_;
  7. Link(int data, Link* next) : data_(data), next_(next)
  8. {
  9. }
  10. };
  11. public:
  12. Stack():head_(0), size_(0)
  13. {
  14. }
  15. ~Stack()
  16. {
  17. Link* tmp;
  18. while(head_)
  19. {
  20. tmp = head_;
  21. head_ = head_->next_;
  22. delete tmp;
  23. }
  24. }
  25. void Push(const int data)
  26. {
  27. Link* node = new Link(data, head_);
  28. head_ = node;
  29. ++size_;
  30. }
  31. bool Empty()
  32. {
  33. return (size_ == 0)
  34. }
  35. bool Pop(int& data)
  36. {
  37. if(Empty())
  38. {
  39. return false;
  40. }
  41. Link* tmp = head_;
  42. data = head_->data_;
  43. head_ = head_->next_;
  44. delete tmp;
  45. --size_;
  46. return true;
  47. }
  48. private:
  49. Link* head_;
  50. int size_;
  51. }
  52. int main(void)
  53. {
  54. Stack stack; // 抽象数据类型,实际上就是类类型
  55. int i;
  56. for(i = 0; i < 5; i ++)
  57. {
  58. stack.Push(i); // 这边编译的时候,该函数的第一个参数为this指针,即this = &back;
  59. }
  60. while(!stack.Empty())
  61. {
  62. stack.Pop(i);
  63. cout << i << " ";
  64. }
  65. return 0;
  66. }

优点:

避免函数名称冲突:因为都定义在类里面,别人定义的接口名称跟我们没关系

数据类型的扩充

数据封装:能够保护内部的数据结构不遭受外部的破坏,只能通过该类提供的接口来访问

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

闽ICP备14008679号