当前位置:   article > 正文

C语言实现封装_c语言如何将多个开关信号进行封装

c语言如何将多个开关信号进行封装
//封装、数据与方法在一起,数据和方法,不可以随便访问
  1. class object
  2. {
  3. public:
  4. int num;
  5. void print()
  6. {
  7. cout << num << endl;
  8. }
  9. protected:
  10. private:
  11. };
  12. int main1()
  13. {
  14. object ob1;
  15. ob1.num = 12;
  16. ob1.print();
  17. system("pause");
  18. return 0;
  19. }
  1. //C++的封装解决了,数据和函数在一起,整体化
  2. //C++的封装解决了权限问题
  3. //c语言 结构体加函数指针实现C++类
  4. //函数不可以在结构体内,但是可以有函数指针
  5. struct MySturct
  6. {
  7. int flag;//floag == 0 ,代表公有,1代表私有
  8. int num;
  9. void(*p)(struct MySturct *pstruct);//函数访问的限制
  10. };
  11. void print(struct MySturct *pstruct)
  12. {
  13. if (pstruct->flag==0)
  14. {
  15. //公有
  16. printf("%d\n", pstruct->num);
  17. }
  18. else
  19. {
  20. return;
  21. }
  22. }
  23. void main()
  24. {
  25. struct MySturct my1;
  26. my1.flag = 1;
  27. my1.p = print;
  28. my1.num = 100;
  29. my1.p(&my1);
  30. system("pause");
  31. }





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

闽ICP备14008679号