封装

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

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

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

#include <iostream>
using namespace std;
struct Date
{
int year;
int month;
int day;
};
void init(Date &d)
{
cout<<"year,month,day:"<<endl;
cin>>d.year>>d.month>>d.day;
}
void print(Date & d)
{
cout<<"year month day"<<endl;
cout<<d.year<<":"<<d.month<<":"<<d.day<<endl;
}
bool isLeapYear(Date & d)
{
if((d.year%4==0&& d.year%100 != 0) || d.year%400 == 0)
return true;
else
return false;
}
int main()
{
Date d;
init(d);
print(d);
if(isLeapYear(d))
cout<<"leap year"<<endl;
else
cout<<"not leap year"<<endl;
return 0;
}

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

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

2.没有权限控制。

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

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

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

class Date
{
protect://属性,成员变量
int year;
int month;
int day;
public://行为,成员函数
void init()
{
cin>>year;
cin>>month;
cin>>day;
}
void print()
{
cout<<"year:"<<"month:"<<"day"<<endl;
cout<<year<<":"<<month<<":"<<day<<endl;
}
} int main()
{
Date d;
d.init();
d.print(); return 0;
}
class Date
{
protect://属性,成员变量
int year;
int month;
int day;
public://行为,成员函数
void init();
void print();
} void Date:: init()//防止冲突
{
cin>>year;
cin>>month;
cin>>day;
}
void Date:: print()
{
cout<<"year:"<<"month:"<<"day"<<endl;
cout<<year<<":"<<month<<":"<<day<<endl;
} int main()
{
Date d;
d.init();
d.print(); return 0;
}

c++多文件中的管理:

date.h:

#ifndef DATE_H
#define DATE_H
using namespace Spac
{
class Date
{
private:
int year;
int month;
int day;
public:
void init();
void print();
int getYear();
bool isLeapYear();
}
}
#endif

class文件:date.cpp

#include<iostream>
#include "date.h"
using namespace std;
using namespace Space
{
void Date:: init()
{
cin>>year;
cin>>month;
cin>>day;
}
void Date:: print()
{
cout<<"year:"<<"month:"<<"day"<<endl;
cout<<year<<":"<<month<<":"<<day<<endl;
}
}

main.cpp

#include<iostrem>
#include "date.h"
using namespace std;
using namespace Space;
int main()
{
Date d;
d.init();
d.print(); return 0;
}

应用栈的实现:

C:

#include<stdio.h>
#include<stdlib.h>
typedef struct stack
{
char space[1024];
int top;
}Stack;
void init(Stack *s)
{
s->top = 0;
memset(s->space,0,1024);
}
int isEmpty(Stack * s)
{
return s->top == 0;
}
int isFull(Stack *s)
{
return s->top == 1024;
}
char pop(Stack *s)
{
return s.space[--s.top];
}
void push(Stack * s,char c)
{
s.space[s.top++] = c;
}
int main()
{
Stack st;
init(&st);
if(!isFull)
push(&st,'a');
if(!isFull)
push(&st,'b');
if(!isFull)
push(&st,'c');
if(!isFull)
push(&st,'d');
if(!isFull)
push(&st,'e');
while(!isEmpty(&st))
printf("%c\n",pop(&st)); return 0;
}

C++:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
class Stack
{
public:
void init();
bool isEmpty();
bool isFull();
void push(int data);
int pop();
private:
int space[1024];
int top;
};
void Stack::init()
{
memset(space,0,sizeof(space));
top = 0;
}
bool Stack::isEmpty()
{
return top == 0;
}
bool Stack::isFull()
{
return top == 1024;
}
void Stack::push(int data)
{
space[top++] = data;
}
int Stack::pop()
{
return space[--top];
}
int main()
{
Stack s;
s.init();
if(!s.isFull())
s.push(10);
if(!s.isFull())
s.push(20);
if(!s.isFull())
s.push(30);
if(!s.isFull())
s.push(40);
if(!s.isFull())
s.push(50);
while(!s.isEmpty())
cout<<s.pop()<<endl;
return 0;
}

个文件分离管理:

stack.h(class文件)

#ifndef STACK_H
#define
class Stack
{
public:
void init();
bool isEmpty();
bool isFull();
void push(int data);
int pop();
private:
int space[1024];
int top;
};
#endif

stack.cpp:

#include<iostream>
#inlcude "stack.h"
#include<stdlib.h>
#include<string.h>
void Stack::init()
{
memset(space,0,sizeof(space));
top = 0;
}
bool Stack::isEmpty()
{
return top == 0;
}
bool Stack::isFull()
{
return top == 1024;
}
void Stack::push(int data)
{
space[top++] = data;
}
int Stack::pop()
{
return space[--top];
}

main.cpp

#include<iostream>
#include "stack.h"
using namespace std;
int main()
{
Stack s;
s.init();
for(char v = "a";!st.isFull()&& v != 'z'+1;v++)
{
st.push(v);
}
while(!s.isEmpty())
cout<<s.pop()<<endl;
return 0;
}

C++链表的实现:

class List
{
private:
Node * head;
public:
void initList();
void insertList();
void traverseList();
void deleteNode(Node * pfind);
Node * searchList(int find);
void sortList();
void destroy();
};
void List:: initList()
{
head = new Node;
head->next = NULL;
}
void List:: insertList(int d)
{
Node * cur = new Node;
cur->data = d; cur->next = head->next;
head->next = cur;
}
void List:: traverseList()
{
Node * ph = head->next;
while(ph != NULL)
{
cout<<ph->data<endl;
ph = ph->next;
}
}
void List:: deleteNode(Node * pfind);
Node * List:: searchList(int find);
void List:: sortList();
void List:: destroy(); int main()
{
List list;
list.init();
for(int i = 0;i < 10;i++)
{
list.insertList(i);
}
list.traverseList(); return 0;
}

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. 继续过Hard题目.周五

      # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word ...

  2. [Typescript] Promise based delay function using async / await

    Learn how to write a promise based delay function and then use it in async await to see how much it ...

  3. C++开发人脸性别识别教程(3)——OpenCv配置和ImageWatch插件介绍

    OpenCv是C++图像处理的重要工具.这个人脸性别识别的项目就是借助OpenCv进行开发的. 尽管网上已经有了非常多关于OpenCv的配置教程,但出于教程完整性考虑.这里还是用专门的一篇博客来介绍O ...

  4. h5 离线存储

  5. flatMap作用

    总结:1. map会将每一条输入映射为一个新对象.{苹果,梨子}.map(去皮) = {去皮苹果,去皮梨子} 其中: “去皮”函数的类型为:A => B 2.flatMap包含两个操作:会将每一 ...

  6. 《剑指offer》数值的整数次方

    一.题目描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 二.输入 double base, int exponent 三.输出 b ...

  7. numa 和 mysql

    cpu numa结构反应的内存访问速度问题: 在多核cpu的时代引入了cpu的numa(非一致内存访问结构): NUMA引入了node的概念,每个物理CPU都被视作一个node,而每个node都有一个 ...

  8. 共用体 union

    共用体是一种数据格式,能够存储不同的数据类型,但只能同时存储其中的一种类型. union one4all { int int_val; double double_val; long long_val ...

  9. 洛谷P3332 [ZJOI2013]K大数查询 权值线段树套区间线段树_标记永久化

    Code: #include <cstdio> #include <algorithm> #include <string> #include <cstrin ...

  10. HDU-1541 Stars 树状数组

    题目链接:https://cn.vjudge.net/problem/HDU-1541 题意 天上有许多星星 现给天空一个平面坐标轴,统计每个星星的level, level是指某一颗星星的左下角(x& ...