C++
qq_26774439
个人学习记录
展开
-
C++初级入门
#include <iostream> using namespace std;//没有他的话,下面要改成std::cout int main() { cout << "Hello world!" << endl; return 0; } 编译器mingw开发工具codelock.endl也是换行,不过它比\n多一个清除缓存流的效果。 ...原创 2019-05-07 17:27:34 · 334 阅读 · 0 评论 -
C++重载重写重定义
函数重载 必须在同一个类中进行 子类无法重载父类的函数,父类同名函数将被名称覆盖 重载是在编译期间根据参数类型和个数决定函数调用 函数重写 必须发生于父类与子类之间 并且父类与子类中的函数必须有完全相同的原型 使用virtual声明之后能够产生多态(如果不使用virtual,那叫重定义) 多态是在运行期间根据具体对象的类型决定函数调用 如果子类发生了重写,则用parent::fu...原创 2019-05-11 12:55:34 · 97 阅读 · 0 评论 -
C++多态原理
C++中多态的实现原理 当类中声明虚函数时,编译器会在类中生成一个虚函数表 虚函数表是一个存储类成员函数指针的数据结构 虚函数表是由编译器自动生成与维护的 virtual成员函数会被编译器放入虚函数表中 存在虚函数时,每个对象中都有一个指向虚函数表的指针(vptr指针) 通过虚函数表指针VPTR调用重写函数是在程序运行时进行的,因此需要通过寻址操作才能确定真正应该调用的函数。而...原创 2019-05-11 15:53:48 · 122 阅读 · 0 评论 -
C++虚函数(抽象类)实现面向接口编程
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; //抽象类1 class Interface1 { public: //纯虚函数 virtual void func(int a, int b) = 0; virtual void func2(int a) = 0; }; ...原创 2019-05-11 18:10:03 · 240 阅读 · 0 评论 -
C++函数指针做函数的思想
#define _CRT_SECURE_NO_WARNINGS #include <iostream> //函数指针做函数参数 using namespace std; int add(int a,int b){ cout<<"add"<<a+b<<endl; return a+b; } int add2(int a,int b){ ...原创 2019-05-11 20:36:53 · 117 阅读 · 0 评论 -
C++模板泛型的使用
#define _CRT_SECURE_NO_WARNINGS #include <iostream> //函数指针做函数参数 using namespace std; //提前声明我是模板,泛型。在调入参数时先定义不能直接传入。 template <typename T> void mySwap(T &a, T &b) { T temp =...原创 2019-05-12 00:11:29 · 687 阅读 · 0 评论 -
C++map用法
#include <iostream> #include <map> using namespace std; int main(){ map<int,string> map1; map1.insert(map<int,string>::value_type(1, "Anna"));//map的插入 map1.insert(map<in...原创 2019-05-16 17:36:20 · 2546 阅读 · 0 评论 -
C++类的基本模板
#define _CRT_SECURE_NO_WARNINGS #include <iostream> //函数指针做函数参数 using namespace std; //单个类的基本模板 template <typename T> class A{ private : T a; public : A(T a=0){ this->a=a; co...原创 2019-05-12 18:18:00 · 115 阅读 · 0 评论 -
C++中类型转换
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; //static_cast int main(void) { double PI = 3.14159265; //int num = PI; int num2 = (int)PI; //C语言的转换方式 int num3 =...原创 2019-05-12 18:53:50 · 87 阅读 · 0 评论 -
C++虚析构函数
#include <iostream> using namespace std; //虚析构函数 class A{ public : char *p; A(){ p=new char[66]; cout<<"A的构造"<<endl; } virtual ~A(){//没有virtual的话下方deleteq函数在引入b的的对象时不会...原创 2019-05-11 12:15:21 · 77 阅读 · 0 评论 -
C++多态初步
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; class Parent { public: Parent(int a) { this->a = a; } virtual void print() //给一个成员函数 定义为 虚函数。 { cout <...原创 2019-05-10 23:06:06 · 86 阅读 · 0 评论 -
C++面向对象初步
.h头文件 #ifndef TEACHER_H #define TEACHER_H class teacher { public: int age; public: void setage(int age); int getage(); }; #endif // TEACHER_H 下方是对应的cpp文件。将方法分开写的。与Java不一样。本质可以写一起的...原创 2019-05-09 00:32:52 · 100 阅读 · 0 评论 -
C++stl vector容器的初步使用
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include "vector" #include "algorithm" //异常的栈解旋 using namespace std; class teacher{ public : int a; public : teacher(int a){ th...原创 2019-05-13 23:58:00 · 107 阅读 · 0 评论 -
C++中new与delete
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; class Test { public: Test() { m_a = 0; m_b = 0; cout << "0,0 构造" << endl; } Test(int a, int b)...原创 2019-05-09 15:23:29 · 74 阅读 · 0 评论 -
C++静态成员变量与函数
#include <iostream> using namespace std; class A{ public : void get(){ cout<<c<<endl; } void add(){ c=c+1; } static void getstatic(){ //注意静态函数中不能有普...原创 2019-05-09 16:59:03 · 115 阅读 · 0 评论 -
C++中this的用法与解析
#include <iostream> using namespace std; class A{ public : int a; public : A(int a){//相当于A(A* this,int a) this->a=a;//之前用.的不行 (隐藏了指针) .指针调用内部变量,用箭头 } void print(){ cout<&l...原创 2019-05-09 19:07:36 · 714 阅读 · 0 评论 -
C++友元函数
#include <iostream> //友元函数 using namespace std; class A{ private : int a; public : friend void modify(A *pa,int b);//加个friend友元函数,本来外部函数是不可以访问内部函数的私有成员变量的。 A(int a){ this->a=a; ...原创 2019-05-09 23:40:34 · 83 阅读 · 0 评论 -
C++运算符重载
#include <iostream> //运算符重载 using namespace std; class A{ public : //如果是私有的下面必须用友元函数,因为调用了内部私有成员。 //friend A operator+(A &a1,A &a2) int a; public : A(){ } A(in...原创 2019-05-09 23:41:34 · 95 阅读 · 0 评论 -
C++中的继承
继承的写法class child:(private,public,protected)parent.和Java一样,访问机制。 C++中的继承方式会影响子类的对外访问属性 public继承:父类成员在子类中保持原有访问级别 private继承:父类成员在子类中变为private成员 protected继承:父类中public成员会变成protected 父类中protected成员...原创 2019-05-10 19:54:32 · 383 阅读 · 0 评论 -
C++异常
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; void divide(int x,int y){ if(y==0){ throw x; } cout<<"执行了除法"<<endl; } int main(){ try { divide...原创 2019-05-13 14:57:50 · 80 阅读 · 0 评论