包含标签 C articles

Cpp9 模板

模板 下面是一个针对int的冒泡排序 // _20180301.cpp : Defines the entry point for the console application. // #include "stdafx.h" void Sort(int* arr,int nLength) { int i,k; for (i = 0;ix > base.x && this->y > base.y; } private: int x; int y; }; template void Sort(T* arr,int nLength) { int i,k; for (i = 0;iy) return x; else return y; } M min() {……

Continue reading

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 0; } //反汇编 27: A a1; 0040108D lea ecx,[ebp-14h] 00401090 call @ILT+15(B::B) (00401014) 00401095 mov dword ptr [ebp-4],0 28: A a2; 0040109C lea ecx,[ebp-1Ch] 0040109F call @ILT+15(B::B) (00401014) 004010A4 mov byte ptr [ebp-4],1 29: a1 = a2; 004010A8 lea eax,[ebp-1Ch] 004010AB push eax //a2 作为参数传递 004010AC lea ecx,[ebp-14h] //a1……

Continue reading

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

多态的实现原理 #include "stdafx.h" #include #include class A { public: int x; virtual void Test() { printf("A \n"); } protected: private: }; class B:public A { public: int x; void Test() { printf("B \n"); } protected: private: }; void Fun(A* p) { p->Test(); } int main(int argc, char* argv[]) { A a; B b; Fun(&b); return 0; } //我们发现在这里 调用的test函数 是b的 因为fun方法传入的对象是b b继承自a 这里体现了多态 //反编译 31: void Fun(A* p) 32: { 00401050 push ebp 00401051 mov ebp,esp 00401053 sub esp,40h 00401056 push ebx 00401057 push esi 00401058 push edi 00401059……

Continue reading

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; } 实现数据隐藏 为什么要隐藏数据成员 与前面比较,赋值的时候 合理不合法,手机的电路板也没有暴露在外面啊 根本的目的是可控 不要造相同的轮子 代码……

Continue reading

Cpp5 在堆中创建对象和引用类型

我们可以在什么地方创建对象? 全局变量区 Person p; 栈 void Max() { Person p; } 堆 new 和 delete //在堆中创建对象: Person* p = new Person(); //释放对象占用的内存 delete p; 在堆中创建对象: new``delete 在C语言中我们使用malloc申请堆空间 使用完毕后使用free释放空间 C++: class Person { private: int x; int y; public: Person() { printf("Person()……

Continue reading

Cpp4 类成员的访问控制

好的编程习惯 -定义和实现分开 代码会有更好的可读性 但不是必须的 在头文件中只留下声明代码 Test.h struct sclass { int x; int y; int Bigger(int x,int y); int Max(int x,int y,int z); }; Test.cpp int sclass::Bigger(int x,int y) { if(x>y) { return x; } else { return y; } } int sclass::Max(int x,int y,int z) { return Bigger(Bigger(x,y),z); } 使用: main.cpp #include "Test.h" #include #include int main(int argc,char* arhv[]) { sclass s; return 0; } 上面的实现方式没有问题,但是其实我们的Bigger函数是为了实现MAX函……

Continue reading

Cpp3 继承

什么是继承 struct Person { int age; int sex; }; struct Teacher { int age; int sex; int level; int classId; }; struct Teacher:Person { int level; int classId; }; 总结: 1、什么是继承? 继承就是数据的复制 2、为什么要用继承? 减少重复代码的编写 3、Person 称为父类或者基类 4、Teacher称为子类或者派生类 继承不仅仅局限于父类 struct X { int a; int b; }; struct Y:X { int c; int d; }; struct Z:Y //Z的成员……

Continue reading

Cpp2 构造函数和析构函数

什么是构造函数 #include "stdafx.h" #include struct Sclass { int a; int b; int c; int d; Sclass()//构造函数 { printf("观察这个函数 \n"); } Sclass(int a,int b,int c,int d)//构造函数 { this->a=a; this->b=b; this->c=c; this->d=d; printf("观察这个函数 2\n"); } int Plus() { return a+b+c+d; } }; int main(int argc, char* argv[]) { Sclass s; Sclass s2(1,2,3,4); return 0; } //反汇编: Sclass s; 0040D408 lea ecx,[ebp-10h] 0040D40B call @ILT+5(Sclass::Sclass) (0040100a) Sclass s2(1,2,3,4); 0040D770 push 4 0040D772 push 3 0040D774 push……

Continue reading

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 strcpy(buffer,NAME); #define D return……

Continue reading

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 00401086 mov dword ptr [ebp-8],3 0040108D mov dword ptr [ebp-4],4……

Continue reading