- 博客(50)
- 收藏
- 关注
转载 C++类成员的访问权限
C++通过 public、protected、private 三个关键字来控制成员变量和成员函数的访问权限,它们分别表示公有的、受保护的、私有的,被称为成员访问限定符。所谓访问权限,就是你能不能使用该类中的成员。Java、C# 程序员注意,C++ 中的 public、private、protected 只能修饰类的成员,不能修饰类,C++中的类没有共有私有之分。在类的内部(定义类的代码内部
2017-08-07 19:16:37
3205
原创 互斥锁
#include #include #include #include #include #include int tickets = 100;pthread_mutex_t mutex;void delay(){int k1 = rand()%10000+1;while (k1){int k2 = rand()%10000+
2017-08-07 19:13:46
284
转载 函数重载的二义性
网址:: http://blog.youkuaiyun.com/czg13548930186/article/details/76060954?locationNum=8&fps=1
2017-08-07 19:12:57
551
原创 函数的重载
在实际开发中,有时候我们需要实现几个功能类似的函数,只是有些细节不同。例如希望交换两个变量的值,这两个变量有多种类型,可以是 int、float、char、bool等,我们需要通过参数把变量的地址传入函数内部。在C语言中,程序员往往需要分别设计出三个不同名的函数,其函数原型与下面类似: void swap1(int *a, int *b); //交换int 变量的值v
2017-08-07 19:12:14
240
原创 默认参数
在C++中,定义函数时可以给形参指定一个默认的值,这样调用函数时如果没有给这个形参赋值(没有对应的实参),那么就使用这个默认的值。也就是说,调用函数时可以省略有默认值的参数。如果用户指定了参数的值,那么就使用用户指定的值,否则使用参数的默认值。 所谓默认参数,指的是当函数调用中省略了实参时自动使用的一个值,这个值就是给形参指定的默认值。下面是一个简单的示例:#includeusin
2017-08-07 19:10:53
482
转载 内联函数代替宏
使用宏的一个经典例子是求一个数的平方,如下所示:#include using namespace std;#define SQ(y) y*yint main(){ int n, sq; cin>>n; sq = SQ(n); cout return 0;}运行结果:9↙81 从表面上看这个宏定义是正确的,但当
2017-08-07 19:09:18
1021
原创 内联函数inline
指定内联函数的方法很简单,只需要在函数定义处增加 inline关键字。请看下面的例子: #include using namespace std;//内联函数,交换两个数的值inline void swap(int *a, int *b){ int temp; temp = *a; *a = *b; *b = temp;}in
2017-08-07 19:08:35
248
原创 malloc和new,free和delete
在C语言中,动态分配内存用malloc() 函数,释放内存用free() 函数。如下所示:两者都是在堆上建立的int *p = (int*) malloc( sizeof(int) * 10 ); //分配10个int型的内存空间free(p); //释放内存 在C++中,这两个函数仍然可以使用,但是C++又新增了两个关键字,new和 delete:new用
2017-08-07 19:07:35
201
原创 C与C++的相同点与不同点
相同:C++ 是在C语言的基础上改进的,C语言的很多语法在C++ 中依然广泛使用,例如:C++ 仍然使用 char、short、int、long、float、double等基本数据类型;C++ 仍然使用 if...else、while、for、switch、break等分支或循环结构;C++ 仍然使用 +、-、*、/、%、++、--、、>>等运算符;C++ 仍然
2017-08-07 19:06:47
12090
转载 C++面向对象的三大特征——多态(静态多态)
1 #include 2 using namespace std; 3 4 //宏多态;a,b可以不同类型 5 #define sum(a,b) ((a) + (b)) 6 7 class Base 8 { 9 public:10 void Print() //不需
2017-08-06 23:29:51
260
原创 C++面向对象的三大特征——多态(动态多态)
1 #include 2 using namespace std; 3 4 class Base 5 { 6 public: 7 virtual void Print() = 0; 8 virtual ~Base(){} 9 };10 11 clas
2017-08-06 23:28:44
248
原创 线程——消费者生产者
#include #include #include #include int resources;pthread_mutex_t mutex;void delay(){unsigned int x,y;x = rand()%10000+1;while (x > 0){y = rand()%10000+1;while (y
2017-08-06 23:25:38
191
原创 数据结构——循环链表
#include #include #define OK 0#define ERROR -1#define MALLOC_ERROR -2 typedef int ElementType;typedef struct node{ElementType data; // 结点的数据struct node *
2017-08-06 23:24:02
457
原创 数据结构——栈
#include #define STACK_SIZE 100#define OK 0 #define ERROR -1 typedef int DATATYPE;typedef struct{DATATYPE data[STACK_SIZE]; // 栈数组int top; // 栈顶下
2017-08-06 23:23:14
211
原创 数据结构——队列
#include #define QUEUE_SIZE 100#define OK 0 #define ERROR -1 typedef int DataType;typedef struct{DataType data[QUEUE_SIZE];int rear, front;}SeqQueue;
2017-08-06 23:21:44
200
原创 数据结构——链表
#include #include #define OK 0#define ERROR -1#define MALLOC_ERROR -2 typedef int ElementType;typedef struct node{ElementType data; // 结点的数据struct node *
2017-08-06 23:20:59
185
原创 文件复制系统调用版
#include #include #include #include #include #include int main(){char buffer[512] = "Hello Linux\n";char buf_read[512] = {0};int fd_read;int fd_write;ssize_t nbytes;char
2017-08-06 23:20:09
454
转载 数据库操作——sqlite3_get_table
#include #include int main(){sqlite3 * db = NULL; int result = sqlite3_open("student.db", &db);if (result != SQLITE_OK){ printf ("1open student.db error!\n");return -1;}
2017-08-06 23:18:43
2022
转载 数据库操作 ——sqlite3_exec
#include #include int LoadMyInfo( void * para, int n_column, char ** column_value, char ** column_name ){ //para是你在 sqlite3_exec 里传入的 void * 参数//通过para参数,你可以传入一些特殊的指针(比如类指针、结构指针),然后在这里面强
2017-08-06 23:17:50
2475
原创 进程通信之共享内存2
#include "6.3 shm_common.h"#include #include #include #include int main(){int shmid;struct share_block *pshare;char buffer[MAX_SZIE] = {0};int running = 1;int ret;// 创建
2017-08-05 17:12:11
240
原创 文件的拷贝
#include #include int main(){FILE *file_in = fopen("bb.c", "r+");if (file_in == NULL){perror("fopen bb.c");return 0;}FILE *file_out = fopen("bbb.c", "w+");if (file_out == N
2017-08-05 17:11:17
160
原创 进程通信之消息队列
#include #include #include #include #include struct msg_buf{int mtype;char data[255];};int main(){key_t key;int msgid;int ret;struct msg_buf msgbuf;key = ftok
2017-08-05 17:08:24
300
原创 两个有序链表2
#include #include #define INIT_SIZE 100 // 顺序表初始化大小#define INCESS_SIZE 20 // 顺序表满后的每次扩充大小#define OK 0#define ERROR -1#define MALLOC_ERROR -2typed
2017-08-05 17:07:31
147
原创 UDP客户端
#include #include #include #include #include #include #include #define SERV_PORT 9877#define BUF_MAX_SIZE 1024int main(int argc, char *argv[]){ socklen_t cli_ad
2017-08-05 17:06:36
264
原创 UDP服务器
#include #include #include #include #include #include #include #include #define SERV_PORT 9877#define BUF_MAX_SIZE 1024int main(){ int lis_sock_fd, con_sock_f
2017-08-05 17:06:05
171
原创 进程通信之共享内存
#include #include #include #include #define MAX_SZIE 1024struct share_block{int flag;char buffer[MAX_SZIE];};int main(){int shmid;struct share_block *pshare;cha
2017-08-05 17:05:11
171
原创 进程通信之管道
#include #include #include #include #include int main(){int pipe_fd[2];pid_t pid;char buf_r[100];char *p_wbuf;int r_num;memset(buf_r, 0, sizeof(buf_r));// 创建管道if (p
2017-08-05 17:03:12
150
原创 服务器与客户端 —— client
#include #include #include #include #include #include #include #define SERV_PORT 9000#define BUFF_SIZE 1024struct user{int socketfd;char name[20];char toName[20];in
2017-08-05 17:02:26
337
原创 服务器与客户端简单通信 ——server
#include #include #include #include #include #include #include #define SERV_PORT 9000#define BUFF_SIZE 1024struct user{int socketfd;char name[20];char toName[20];
2017-08-05 17:01:22
350
原创 递归函数
#include "stdio.h"void fun(int i){if(i >0){fun(i/2);}printf("%d\n",i);} int main(){fun(10);return 0;}输出值为 0 1 2 5
2017-08-04 14:25:54
275
原创 有序链表拼接
Node * Merge(Node *head1 , Node *head2) { if ( head1 == NULL) return head2 ; if ( head2 == NULL) return head1 ; Node *head = NULL ; Node *p1 = NULL; Node *p2 = NULL; if ( he
2017-08-04 14:24:39
236
原创 柔性数组
struct Node{int size;char data[0]; };其中的data[0]是对的,是柔性数组。对于编译器来说,数组名只是个符号,他不会占用任何空间,在结构体中只代表一个偏移量,代表一个不可修改的地址常量。必须是结构体最后一个成员。
2017-08-04 14:23:40
197
原创 内联函数
内联函数具有一般函数的特性,他与一般函数不同之处只在于函数调用的处理。一般函数进行调用时要将程序执行权转到被调用函数中,然后在返回调用他的函数中;而内敛函数在调用时,是将调用表达式用内联函数体来替换。内联函数使用注意要点1.在内敛函数内不允许用循环语句和开关语句。若有则编译器将该函数视为普通函数那样昌盛函数调用代码,递归函数(自己调用自己)是不能作为内敛函数的。内联函数只适用于1—
2017-08-04 14:22:27
7989
原创 内存
int main(){char *p = "hello world";return 0; }p和“hello world”储存在内存哪个区域答案: 栈,只读储存区(1)从静态存储区域分配:内存在程序编译时就已经分配好这块内存在程序的整个运行期间都存在。速度快、不容易出错,因为有系统会善后。例如全局变量, static变量等。(
2017-08-04 14:21:45
263
转载 静态成员和静态成员函数
静态成员函数的特点:1.static数据成员在类内的内部声明,但只能在类的外部定义,在类的外部不能指定static,在类的定义时候惊醒初始化;2.static数据成员只能在课的外部进行初始化(特例:当整形const static数据成员被常量表达式初始化,就可以在类内进行初始化,但还需要在外部进行定义)。3.static数据成员可以是该成员所属的类类型,而非st
2017-08-04 14:19:41
238
原创 二分法
二分法查找:a是查找的数组,二分法查找的前提条件是a数据的排序是有序的。key是待查找的变量,n是数组a的长度。int binary( int *a, int key, int n ){ int left = 0, right = n - 1, mid = 0; mid = ( left + right ) / 2; while( left {
2017-08-04 14:18:13
151
转载 三次握手,四次挥手
第一次握手:客户端发送一个TCP标志位SYN=1、ACK=0的数据包给服务器,并随机会产生一个Sequence number=3233.当服务器接收到这个数据后,服务器由SYN=1可知客户端是想要建立连接; 第二次握手:服务器要对客户端的联机请求进行确认,向客户端发送应答号ACK=1、SYN=1、确认号Acknowledge number=3234,此值是客户端的序列号加1,还会产
2017-08-04 14:14:28
173
转载 TCP协议与UDP协议的区别
TCP协议与UDP协议的区别 首先咱们弄清楚,TCP协议和UCP协议与TCP/IP协议的联系,很多人犯糊涂了,一直都是说TCP/IP协议与UDP协议的区别,我觉得这是没有从本质上弄清楚网络通信!TCP/IP协议是一个协议簇。里面包括很多协议的。UDP只是其中的一个。之所以命名为TCP/IP协议,因为TCP,IP协议是两个很重要的协议,就用他两命名了。TCP/IP协议集包括应用层,
2017-08-04 14:13:03
150
转载 static在c++中
第一个例子,通过类名调用静态成员函数和非静态成员函数 class Point { public: void init() { } static void output() { } }; void main() { Point::init();
2017-08-04 14:11:12
152
原创 C++练习
对一个5位数的任意整数,求出其降序数。例如,整数是82319,则其降序数是98321。算法提示:将整数的各位数分解到一维整型数组a中,再将a数组中的元素按降序排序,最后输出a数组元素值。试建立一个类DescendNUM,用于完成该功能。具体要求如下:(1)私有数据成员int n:存放5位数的整数。int a[5]:存放其元素的降序排列值。(2)公有成员函数DescendNUM(
2017-01-20 21:20:07
332
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人