封装

当单一变量无法完成描述需求的时候,结构体类型解决了这一问题。可以将多个类型打包成一体,形成新的类型。这是 c 语言中封装的概念。但是,新类型并不包含,对数据类的操作。所的有操作都是通过函数的方式,去其进行封装。

对一组数据变量组进行结合形成结构体--初步的封装。

C语言的封装风格,数据放到一起找包Struct,然后把数据以引用或者指针的方式传给行为。

  1. #include <iostream>
  2. using namespace std;
  3. struct Date
  4. {
  5. int year;
  6. int month;
  7. int day;
  8. };
  9. void init(Date &d)
  10. {
  11. cout<<"year,month,day:"<<endl;
  12. cin>>d.year>>d.month>>d.day;
  13. }
  14. void print(Date & d)
  15. {
  16. cout<<"year month day"<<endl;
  17. cout<<d.year<<":"<<d.month<<":"<<d.day<<endl;
  18. }
  19. bool isLeapYear(Date & d)
  20. {
  21. if((d.year%4==0&& d.year%100 != 0) || d.year%400 == 0)
  22. return true;
  23. else
  24. return false;
  25. }
  26. int main()
  27. {
  28. Date d;
  29. init(d);
  30. print(d);
  31. if(isLeapYear(d))
  32. cout<<"leap year"<<endl;
  33. else
  34. cout<<"not leap year"<<endl;
  35. return 0;
  36. }

C++ 认为c封装不彻底。

1.数据和行为没有分离。

2.没有权限控制。

封装的特点:对内数据开放,逻辑抽象,对外提供接口。

C++增加权限控制,private protected public 数据和行为在一起,对内开放,对外提供接口。

过程:类 -> 类对象 ->对象。对象调用行为完成需求。

  1. class Date
  2. {
  3. protect://属性,成员变量
  4. int year;
  5. int month;
  6. int day;
  7. public://行为,成员函数
  8. void init()
  9. {
  10. cin>>year;
  11. cin>>month;
  12. cin>>day;
  13. }
  14. void print()
  15. {
  16. cout<<"year:"<<"month:"<<"day"<<endl;
  17. cout<<year<<":"<<month<<":"<<day<<endl;
  18. }
  19. }
  20. int main()
  21. {
  22. Date d;
  23. d.init();
  24. d.print();
  25. return 0;
  26. }
  1. class Date
  2. {
  3. protect://属性,成员变量
  4. int year;
  5. int month;
  6. int day;
  7. public://行为,成员函数
  8. void init();
  9. void print();
  10. }
  11. void Date:: init()//防止冲突
  12. {
  13. cin>>year;
  14. cin>>month;
  15. cin>>day;
  16. }
  17. void Date:: print()
  18. {
  19. cout<<"year:"<<"month:"<<"day"<<endl;
  20. cout<<year<<":"<<month<<":"<<day<<endl;
  21. }
  22. int main()
  23. {
  24. Date d;
  25. d.init();
  26. d.print();
  27. return 0;
  28. }

c++多文件中的管理:

date.h:

  1. #ifndef DATE_H
  2. #define DATE_H
  3. using namespace Spac
  4. {
  5. class Date
  6. {
  7. private:
  8. int year;
  9. int month;
  10. int day;
  11. public:
  12. void init();
  13. void print();
  14. int getYear();
  15. bool isLeapYear();
  16. }
  17. }
  18. #endif

class文件:date.cpp

  1. #include<iostream>
  2. #include "date.h"
  3. using namespace std;
  4. using namespace Space
  5. {
  6. void Date:: init()
  7. {
  8. cin>>year;
  9. cin>>month;
  10. cin>>day;
  11. }
  12. void Date:: print()
  13. {
  14. cout<<"year:"<<"month:"<<"day"<<endl;
  15. cout<<year<<":"<<month<<":"<<day<<endl;
  16. }
  17. }

main.cpp

  1. #include<iostrem>
  2. #include "date.h"
  3. using namespace std;
  4. using namespace Space;
  5. int main()
  6. {
  7. Date d;
  8. d.init();
  9. d.print();
  10. return 0;
  11. }

应用栈的实现:

C:

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct stack
  4. {
  5. char space[1024];
  6. int top;
  7. }Stack;
  8. void init(Stack *s)
  9. {
  10. s->top = 0;
  11. memset(s->space,0,1024);
  12. }
  13. int isEmpty(Stack * s)
  14. {
  15. return s->top == 0;
  16. }
  17. int isFull(Stack *s)
  18. {
  19. return s->top == 1024;
  20. }
  21. char pop(Stack *s)
  22. {
  23. return s.space[--s.top];
  24. }
  25. void push(Stack * s,char c)
  26. {
  27. s.space[s.top++] = c;
  28. }
  29. int main()
  30. {
  31. Stack st;
  32. init(&st);
  33. if(!isFull)
  34. push(&st,'a');
  35. if(!isFull)
  36. push(&st,'b');
  37. if(!isFull)
  38. push(&st,'c');
  39. if(!isFull)
  40. push(&st,'d');
  41. if(!isFull)
  42. push(&st,'e');
  43. while(!isEmpty(&st))
  44. printf("%c\n",pop(&st));
  45. return 0;
  46. }

C++:

  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. using namespace std;
  6. class Stack
  7. {
  8. public:
  9. void init();
  10. bool isEmpty();
  11. bool isFull();
  12. void push(int data);
  13. int pop();
  14. private:
  15. int space[1024];
  16. int top;
  17. };
  18. void Stack::init()
  19. {
  20. memset(space,0,sizeof(space));
  21. top = 0;
  22. }
  23. bool Stack::isEmpty()
  24. {
  25. return top == 0;
  26. }
  27. bool Stack::isFull()
  28. {
  29. return top == 1024;
  30. }
  31. void Stack::push(int data)
  32. {
  33. space[top++] = data;
  34. }
  35. int Stack::pop()
  36. {
  37. return space[--top];
  38. }
  39. int main()
  40. {
  41. Stack s;
  42. s.init();
  43. if(!s.isFull())
  44. s.push(10);
  45. if(!s.isFull())
  46. s.push(20);
  47. if(!s.isFull())
  48. s.push(30);
  49. if(!s.isFull())
  50. s.push(40);
  51. if(!s.isFull())
  52. s.push(50);
  53. while(!s.isEmpty())
  54. cout<<s.pop()<<endl;
  55. return 0;
  56. }

个文件分离管理:

stack.h(class文件)

  1. #ifndef STACK_H
  2. #define
  3. class Stack
  4. {
  5. public:
  6. void init();
  7. bool isEmpty();
  8. bool isFull();
  9. void push(int data);
  10. int pop();
  11. private:
  12. int space[1024];
  13. int top;
  14. };
  15. #endif

stack.cpp:

  1. #include<iostream>
  2. #inlcude "stack.h"
  3. #include<stdlib.h>
  4. #include<string.h>
  5. void Stack::init()
  6. {
  7. memset(space,0,sizeof(space));
  8. top = 0;
  9. }
  10. bool Stack::isEmpty()
  11. {
  12. return top == 0;
  13. }
  14. bool Stack::isFull()
  15. {
  16. return top == 1024;
  17. }
  18. void Stack::push(int data)
  19. {
  20. space[top++] = data;
  21. }
  22. int Stack::pop()
  23. {
  24. return space[--top];
  25. }

main.cpp

  1. #include<iostream>
  2. #include "stack.h"
  3. using namespace std;
  4. int main()
  5. {
  6. Stack s;
  7. s.init();
  8. for(char v = "a";!st.isFull()&& v != 'z'+1;v++)
  9. {
  10. st.push(v);
  11. }
  12. while(!s.isEmpty())
  13. cout<<s.pop()<<endl;
  14. return 0;
  15. }

C++链表的实现:

  1. class List
  2. {
  3. private:
  4. Node * head;
  5. public:
  6. void initList();
  7. void insertList();
  8. void traverseList();
  9. void deleteNode(Node * pfind);
  10. Node * searchList(int find);
  11. void sortList();
  12. void destroy();
  13. };
  14. void List:: initList()
  15. {
  16. head = new Node;
  17. head->next = NULL;
  18. }
  19. void List:: insertList(int d)
  20. {
  21. Node * cur = new Node;
  22. cur->data = d;
  23. cur->next = head->next;
  24. head->next = cur;
  25. }
  26. void List:: traverseList()
  27. {
  28. Node * ph = head->next;
  29. while(ph != NULL)
  30. {
  31. cout<<ph->data<endl;
  32. ph = ph->next;
  33. }
  34. }
  35. void List:: deleteNode(Node * pfind);
  36. Node * List:: searchList(int find);
  37. void List:: sortList();
  38. void List:: destroy();
  39. int main()
  40. {
  41. List list;
  42. list.init();
  43. for(int i = 0;i < 10;i++)
  44. {
  45. list.insertList(i);
  46. }
  47. list.traverseList();
  48. return 0;
  49. }

C/C++(C++封装)的更多相关文章

  1. [C#] 简单的 Helper 封装 -- RegularExpressionHelper

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. iOS开发之App间账号共享与SDK封装

    上篇博客<iOS逆向工程之KeyChain与Snoop-it>中已经提到了,App间的数据共享可以使用KeyChian来实现.本篇博客就实战一下呢.开门见山,本篇博客会封装一个登录用的SD ...

  3. Ajax实现原理,代码封装

    都知道实现页面的异步操作需要使用Ajax,那么Ajax到是怎么实现异步操作的呢? 首先需要认识一个对象 --> XMLHttpRequest 对象 --> Ajax的核心.它有许多的属性和 ...

  4. 用C语言封装OC对象(耐心阅读,非常重要)

    用C语言封装OC对象(耐心阅读,非常重要) 本文的主要内容来自这里 前言 做iOS开发的朋友,对OC肯定非常了解,那么大家有没有想过OC中NSInteger,NSObject,NSString这些对象 ...

  5. 【知识必备】RxJava+Retrofit二次封装最佳结合体验,打造懒人封装框架~

    一.写在前面 相信各位看官对retrofit和rxjava已经耳熟能详了,最近一直在学习retrofit+rxjava的各种封装姿势,也结合自己的理解,一步一步的做起来. 骚年,如果你还没有掌握ret ...

  6. 对百度WebUploader开源上传控件的二次封装,精简前端代码(两句代码搞定上传)

    前言 首先声明一下,我这个是对WebUploader开源上传控件的二次封装,底层还是WebUploader实现的,只是为了更简洁的使用他而已. 下面先介绍一下WebUploader 简介: WebUp ...

  7. 封装集合(Encapsulate Collection)

    封装就是将相关的方法或者属性抽象成为一个对象. 封装的意义: 对外隐藏内部实现,接口不变,内部实现自由修改. 只返回需要的数据和方法. 提供一种方式防止数据被修改. 更好的代码复用. 当一个类的属性类 ...

  8. CSharpGL(29)初步封装Texture和Framebuffer

    +BIT祝威+悄悄在此留下版了个权的信息说: CSharpGL(29)初步封装Texture和Framebuffer +BIT祝威+悄悄在此留下版了个权的信息说: Texture和Framebuffe ...

  9. CSharpGL(7)对VAO和VBO的封装

    CSharpGL(7)对VAO和VBO的封装 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharpGL源码中包含10多个独立的Demo,更适合入门参考 ...

  10. Swift -- 对AFN框架的封装

    Swift -- 对AFN框架的封装 一.封装AFN的目的 简单的说: 解耦 日常工作中,我们一般都不会去直接使用AFNetWorking来直接发送网络请求,因为耦合性太强,假设有多个控制器都使用AF ...

随机推荐

  1. 洛谷—— P2896 [USACO08FEB]一起吃饭Eating Together

    https://www.luogu.org/problem/show?pid=2896 题目描述 The cows are so very silly about their dinner partn ...

  2. Android中设置半个屏幕大小且居中的button布局 (layout_weight属性)

            先看例如以下布局 : 

  3. Android 最火的高速开发框架xUtils

    Github下载地址:https://github.com/wyouflf/xUtils xUtils简单介绍 xUtils 包括了非常多有用的Android工具. xUtils 最初源于Afinal ...

  4. hdu 2037 贪心

    今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  5. 用java实现螺旋数组

    接收数组的行数和列数,返回正序和倒序的螺旋数组 package cn.baokx; public class Test { public static void main(String[] args) ...

  6. ufldl学习笔记与编程作业:Linear Regression(线性回归)

    ufldl学习笔记与编程作业:Linear Regression(线性回归) ufldl出了新教程,感觉比之前的好.从基础讲起.系统清晰,又有编程实践. 在deep learning高质量群里面听一些 ...

  7. CSS中关于vertical-align垂直对齐

    一向以来,我对vertical-align的属性都搞的不是太清楚,今天刚好碰到有朋友问我相关的问题,于是自己潜心研究了一番,发现这玩意还真不是个简单的东西,在此我分享的东西都是抛弃脑残的IE的,如果你 ...

  8. ubuntu16.04下snort的安装(官方文档安装)(图文详解)

    不多说,直接上干货! 最近为了科研,需要安装和使用Snort. snort的官网 https://www.snort.org/ Snort作为一款优秀的开源主机入侵检测系统,在windows和Linu ...

  9. 线程1—Thread

    随便选择两个城市作为预选旅游目标.实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市.分别用Runnable接口和Thread类实 ...

  10. 使用ShareSDK分享-图片的链接

    微信中使用ShareSDK分享,需要申请微信开放平台账号,并且以微信中的声明的应用签名打包程序. private void showShare(String url, String title, St ...