C/C++(C++封装)
封装
当单一变量无法完成描述需求的时候,结构体类型解决了这一问题。可以将多个类型打包成一体,形成新的类型。这是 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++封装)的更多相关文章
- [C#] 简单的 Helper 封装 -- RegularExpressionHelper
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- iOS开发之App间账号共享与SDK封装
上篇博客<iOS逆向工程之KeyChain与Snoop-it>中已经提到了,App间的数据共享可以使用KeyChian来实现.本篇博客就实战一下呢.开门见山,本篇博客会封装一个登录用的SD ...
- Ajax实现原理,代码封装
都知道实现页面的异步操作需要使用Ajax,那么Ajax到是怎么实现异步操作的呢? 首先需要认识一个对象 --> XMLHttpRequest 对象 --> Ajax的核心.它有许多的属性和 ...
- 用C语言封装OC对象(耐心阅读,非常重要)
用C语言封装OC对象(耐心阅读,非常重要) 本文的主要内容来自这里 前言 做iOS开发的朋友,对OC肯定非常了解,那么大家有没有想过OC中NSInteger,NSObject,NSString这些对象 ...
- 【知识必备】RxJava+Retrofit二次封装最佳结合体验,打造懒人封装框架~
一.写在前面 相信各位看官对retrofit和rxjava已经耳熟能详了,最近一直在学习retrofit+rxjava的各种封装姿势,也结合自己的理解,一步一步的做起来. 骚年,如果你还没有掌握ret ...
- 对百度WebUploader开源上传控件的二次封装,精简前端代码(两句代码搞定上传)
前言 首先声明一下,我这个是对WebUploader开源上传控件的二次封装,底层还是WebUploader实现的,只是为了更简洁的使用他而已. 下面先介绍一下WebUploader 简介: WebUp ...
- 封装集合(Encapsulate Collection)
封装就是将相关的方法或者属性抽象成为一个对象. 封装的意义: 对外隐藏内部实现,接口不变,内部实现自由修改. 只返回需要的数据和方法. 提供一种方式防止数据被修改. 更好的代码复用. 当一个类的属性类 ...
- CSharpGL(29)初步封装Texture和Framebuffer
+BIT祝威+悄悄在此留下版了个权的信息说: CSharpGL(29)初步封装Texture和Framebuffer +BIT祝威+悄悄在此留下版了个权的信息说: Texture和Framebuffe ...
- CSharpGL(7)对VAO和VBO的封装
CSharpGL(7)对VAO和VBO的封装 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharpGL源码中包含10多个独立的Demo,更适合入门参考 ...
- Swift -- 对AFN框架的封装
Swift -- 对AFN框架的封装 一.封装AFN的目的 简单的说: 解耦 日常工作中,我们一般都不会去直接使用AFNetWorking来直接发送网络请求,因为耦合性太强,假设有多个控制器都使用AF ...
随机推荐
- System and method for assigning a message
A processor of a plurality of processors includes a processor core and a message manager. The messag ...
- find the safest road HDU杭电1596【Dijkstra || SPFA】
pid=1596">http://acm.hdu.edu.cn/showproblem.php?pid=1596 Problem Description XX星球有非常多城市,每一个城 ...
- 陈-朱-兴- js写法【案例】:
ajax请求: 一.从服务器端请求数据: var url = '';url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='+ ...
- [GDKOI2010] 圈地计划(网络流)
题2链接:https://www.luogu.org/problemnew/show/P1935 Description 最近房地产商GDOI(Group of Dumbbells Or Idiots ...
- HikariCP--一款高性能的 JDBC 连接池
源码地址:https://github.com/brettwooldridge/HikariCP 使用方法: Java 8 maven artifact: <dependency> < ...
- UVa 140 Bandwidth【枚举排列】
题意:给出n个节点的图,和一个节点的排列,定义节点i的带宽b[i]为i和其相邻节点在排列中的最远的距离,所有的b[i]的最大值为这个图的带宽,给一个图,求出带宽最小的节点排列 看的紫书,紫书上说得很详 ...
- UI Framework-1: Aura Client API
Client API The Aura Client API is an API Aura uses to communicate with the client application using ...
- 四、YOLO-V1原理与实现(you only look once)
可以看成图像分类与定位的结合,给定一张图片,目标检测系统要能够识别出图片的目标并给出其位置,由于图片中目标数是不定的,且要给出目标的精确位置,目标检测相比分类任务更复杂.目标检测的一个实际应用场景就是 ...
- 【Git 一】Linux安装Git
一.Git 的优势 #简单说一下 Git 的优势. 1.版本库本地化,支持离线提交,相对独立不影响协同开发. 2.支持快速切换分支方便合并,比较合并性能好. 3.分布式管理,适应不同的业务场景. 4. ...
- ZOJ-3261 Connections in Galaxy War 并查集 离线操作
题目链接:https://cn.vjudge.net/problem/ZOJ-3261 题意 有n个星星,之间有m条边 现一边询问与x星连通的最大星的编号,一边拆开一些边 思路 一开始是真不会,甚至想 ...