Cpp8 运算符重载和深浅拷贝

深浅拷贝 相同类型间可以直接拷贝 // _20180212.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include class A { private: int* a; public: A() { a = new int[10]; } virtual ~A() { delete a; printf("析构 A \n"); } }; int main(int argc,char* argv[]) { A a1; A a2; a1 = a2; return …

Cpp7 C++的多态实现 — 虚表

多态的实现原理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 #include "stdafx.h" #include #include class A { public: int x; virtual void Test() { …

Cpp6 封装、继承和多态

继承 子类从父类继承成员变量 子类从父类继承成员函数 #include "stdafx.h" class Person { public: int Age; int Sex; void Word() { printf("Person:Work"); } }; class Teacher:public Person { public: int Level; }; int main() { Teacher t; t.Age = -1; //合法但是不合理 t.Sex = 2; t.Level = 3; return 0; } 实现数据隐藏 为什么要隐藏数据成员 与前面比较,赋值的时候 合理不合法, …

C语言23 终极篇 预处理之宏定义、条件编译和文件包含

什么是预处理 预处理一般是指在程序源代码被转换为二进制代码之前,由预处理器对程序源代码文本进行处理,处理后的结果再由编译器进一步编译。 预处理功能主要包括宏定义,文件包含,条件编译三部分 宏定义 简单宏: #define 标识符 字符序列 #define FALSE 0 #define NAME "LUODAOYI" #define __IN #define __OUT //极端例子 #define NAME "LUODAOYI" #define A int method() { #define B char buffer[0x10]; #define C …

链表

实现思想 :通过指针把要元素串起来 简单结构图 struct Student { char name[10]; int score; } struct Node { Student Element; Node* next; } 每一个节点包含此节点信息和下一个节点的指针 有点:插入删除方便 缺点:查找麻烦 基本元素实现 //方法/函数 int InitList();//初始化链表 int IsEmpty(); //判断链表是否为空 空返回1 非空0 void Clear(Node* p); //清空链表 ini Find(int Index,Student* element); //根据索引获 …

Cpp1 封装和this指针

封装 C语言和C++语言的区别 C++是对C的补充扩展,C原有的语法C++都支持,并在此基础上扩展了一些新的语法: 继承、封装、多态、模板等等 结构体可以作为参数传递吗 struct Student { int a; int b; int c; int d; } //分析这个函数是如何传递参数的 int Plus(student s) { return s.a+s.b+s.c+s.d; } 23: Student s = {1,2,3,4}; 00401078 mov dword ptr [ebp-10h],1 0040107F mov dword ptr [ebp-0Ch],2 …

C语言22 调用约定和函数指针

函数调用约定 就是告诉编译器:怎么传递参数,怎么传递返回值,怎么平衡堆栈 int method(int x,int y) { return x+y; } //调用 method(1,2); 常见的几种调用约定: 调用约定 参数压栈顺序 平衡堆栈 __cdecl 从右至左入栈 调用者清理栈 __stdcall 从右至左入栈 自身清理堆栈 __fastcall ECX/EDX 传送前两个,剩下:从右至左入栈 自身清理堆栈 int __stdcall method(int x,int y) { return x+y; } //调用 method(1,2); //观察反汇编堆栈变化 PS:一般情况下自 …

C语言21 指针数组与数组指针

指针数组的定义 char arr[10]; //10 char char* arr[10]; //10 指针(char*) Point* arr[10]; //10 指针(Point*) int******** arr[10]; 指针数组的赋值 char* a = "Hello"; char* b = "World"; //方式1: char* arr[2]= {a,b}; //方式2: char* arr[2]; arr[0]=a; arr[1]=b; 结构体指针数组 struct Point { int x; int y; } Point p; //8字节 一个结构体 Point …

C语言20 结构体指针

探测结构体指针的特性 struct Point { int x; int y; } ++、– 加法与减法 指针相减 使用结构体指针 //创建结构体 Point p; p.x=10; p.y=20; //声明结构体指针 Point* ps; //为结构体指针赋值 ps = &p; //通过指针读取数据 printf("%d \n",ps->x); //通过指针修改数据 ps->y=100; printf("%d\n",ps=>y); 结构体指针一定要指向结构体吗? struct Point { int x; int y; } int arr[10]={1,2,3,4,5,6,7,8,9,10} …

C语言19 指针取值的两种方式

一级指针和多级指针 int i =100; int* p1 = &i; int** p2 = &p1; int*** p3 = &p2; int**** p4 = &p3; int***** p5 = &p4 int****** p6 = &p5; int******* p7 = &p6; int****** px1 = *(p7); int***** px2 = *(px1); int**** px3 = *(px2); int*** px4 = *(px3); int** px5 = *(px4); int* px6 = *(px5); int px7 = *(px6); int px7 …

C语言18 指针与字符串

字符串的几种表示方式有什么区别? char str[6] = {"A","B","C","D","E","F"}; //"\0" 或者0 --堆栈中 局部变量 char str[] = "ABCDE"; //编译器末尾填0 常量区 char* str= "ABCDE"; //常量区 //打印 printf("%s \n",str); //字符串打印时 遇到 0 才会停止 常用的字符串函数 1、int strlen(char* s) 返回值是字符串S的长度,不包括结束符"/0" 弊端:字符串中英文混合的话。返回的长度有问题,因为中文占用两个字节 2、char* strcpy(char* …

C语言17 数组参数传递

基本类型参数传递 int x =1; void plus(int p) { p = p+1; } printf("%d \n",x); //x的值是多少? 数组作为参数 编写一个函数,能够打印任意整形数组的值 void PrintArray(int arr[],int nLength) {

C语言16 取值运算符

“*”的几种用途 乘法运算符 int x = 1; int y = 2; int z = x * y; 定义新的类型 char x; char* x; 取值运算符 指针类型的变量 int* a =(int*)1; printf("%x \n",(a +1)); 10: int a =(int*)1; 00401028 mov dword ptr [ebp-4],1 11: printf("%x \n",*(a +1)); 0040102F mov eax,dword ptr [ebp-4] 00401032 mov ecx,dword ptr [eax+4] 00401035 push ecx …

C语言15 取地址符号&的使用

&符号是取地址符,任何变量都可以使用&来获取地址,但不能用在常量上 struct Point { int x; int y; }; char a; short b; int c; Point p; printf("%p %p %p %p \n",&a,&b,&c,&p); printf("%x %x %x %x \n",&a,&b,&c,&p); printf("%x \n",&10); 16: char a; 17: short b; 18: int c; 19: Point p; 20: 21: printf("%p %p %p %p \n",&a,&b,&c,&p); 0040D408 …

C语言14 指针类型

C语言14 指针类型 指针是C语言中的一种数据类型, 定义带 *类型的变量 char x; char* x; short y; short* y; int z; int* z; float f; float* f; double d; double* d; Student st; Student* st; 总结: 任何带有*的变量类型的标准写法:变量类型* 变量名 任何类型都可以带* 加上* 以后是新的类型,统称为“指针类型” *可以使任意多个。 指针变量赋值 char* x; char*** x; char******* x; short* ~~ int* ~~ Student* ~~ x …

结构体 字节对齐的练习

分析结构体大小 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 struct S1 { char c1; double d; char c2; char c3; }; struct S2 { char c1; char c2; char c3; double d; }; struct S3 { char c1; S1 s; char c2; char c3; }; 分析结构体大小 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #pragma pack(1) struct S1 { char …

C语言 13 结构体数组

结构体数组的定义 类型 变量名[常量表达式]; //定义结构体类型 struct stStudent { int Age; int Level; } //定义结构体变量 struct stStudent st; //定义结构体数组 struct stStudent arr[10]; //或者 stStudent arr[10]; 结构体数组初始化 struct stStudent{ int Age; int Level; }; struct stStudent arr[5] = {{0,0},{1,1},{2,2},{3,3},{4,4}}; //或者 arr[0].Age=100; …

C语言12 字节对齐

#include char x; short y; int check() { x =1; y=2; return 0; } int main(int argc, char* argv[]) { check(); system("pause"); return 0; } //上述代码中 check 函数的反汇编 13: x =1; 00401038 C6 05 E2 55 42 00 01 mov byte ptr [x (004255e2)],1 14: y=2; 0040103F 66 C7 05 E0 55 42 00 mov word ptr [y (004255e0)],offset …

C语言11 结构体

思考: 当需要一个容器能够存储1个字节,你会怎么做? //char 当需要一个容器能够存储4个字节,你会怎么做? //int 当需要一个容器能够存储100个2个字节的数据,你会怎么做? //short arr[100] 当需要一个容器能够存储5个数据,这5个数据中有1字节的,2字节的有10字节的。。。你会怎么做??

C语言10 多维数组

多维数组的定义 比如一个班有5个组,每个组有9个人 int arr[45] 或者 intarr[5*9] 或者 int arr[5][9] 比如一个县有5个学校,每个学校有3个年级,每个年级有4个班,每个班有5个组,每个组有9个人 int arr[53459] 或者int arr[5][3][4][5][9] int arr[5][3][4][5][9] ,又成为多维数组. 多维数组的初始化 int arr[3][4] = { {1,2,3,4}, {5,6,7,8}, {9,7,6,5} }; 多维数组的存储方式 int arr[3][4]; 反汇编对比 8: int arr[3*4] = …