C-C3程序猿
看c3程序员C++视频的一些笔记
111
在 C语言 的 switch(开关语句)中,break 语句还可用来在执行完一个 case(分支)后立即跳出当前 switch 结构。
输入输出流
cout是个对象,既不是关键字,也不是函数。
可以连续输出
自动识别类型
cin同理
C中不能定义2个相同的变量和函数,会出现重定义
1 2 3 4 5 6 7 8 9 10 11 12 #include <iostream> using namespace std;int main () { int a = 12 ; char b = 'v' ; float c = 34.56 ; cin >> a >> b >> c; cout << a <<' ' << b <<' ' << c << endl; return 0 ; }
命名空间
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 #include <iostream> using namespace std; namespace stu { void sort () { cout << 23 << endl; } } namespace stu1{ void sort () { cout << 56 <<endl; } } using namespace stu;using namespace stu1;int main () { stu::sort (); stu1::sort (); return 0 ; }
image-20211001101337224
结构体
C语言中结构体看【C++教程】_哔哩哔哩_bilibili
C++中结构体中可以放函数
声明结构体变量不用struct关键字
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <iostream> using namespace std; struct first { int a; void fun () { cout << "Hello wolrd" ; } }; int main () { first stu; stu.a = 23 ; cout << stu.a << endl; stu.fun (); return 0 ; }
new & delete
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 #include <iostream> using namespace std;int main () { int *p = new int ; float *p1 = new float (134.56 ); *p = 23 ; cout << *p << endl << *p1; delete p; delete p1; return 0 ; } ------------------------------------------------------------------------ #include <iostream> using namespace std;int * test01 () { int *p = new int (10 ); return p; } void test02 () { int *arr = new int [10 ]; for (int i = 0 ; i < 10 ; i++) { arr[i] = i + 10 ; } for (int i = 0 ; i < 10 ; i++) { cout << arr[i] << endl; } delete []arr; } int main () { int *p1 = test01 (); cout << *test01 () << ' ' << *p1 << endl; test02 (); system ("pause" ); return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include <iostream> #include <string.h> using namespace std;int main () { int *p = new int [5 ]; memset (p,0 ,5 *sizeof (int )); p[0 ] = 1 ; p[2 ] = 3 ; cout<< p[0 ] << endl << p[2 ] << endl<< p[4 ]; delete [] p; }
引用
引用给变量起别名,typedef给类型起别名
1 2 3 4 5 6 7 8 9 10 11 12 13 #include <iostream> #include <string.h> using namespace std;int main () { int a = 12 ; int &c = a; c = 14 ; cout << a << endl; return 0 ; }
image-20211002165551868
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 #include <iostream> #include <string.h> using namespace std;int main () { const int &a = 12 ; const char &b = 'v' ; cout << a << endl << b << endl; int arr[10 ]; int (&p)[10 ] = arr; p[0 ] = 100 ; cout << arr[0 ] << endl; int arr2[2 ][3 ]; int (&p2)[2 ][3 ] = arr2; p2[2 ][3 ] = 67 ; cout << arr2[2 ][3 ]<<endl; int c = 78 ; int *point = &c; cout << *point<<endl; int * (&p3) = point; *p3 = 97 ; cout << c; return 0 ; }
引用做参数
传递参数的过程也是初始化的过程
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 #include <iostream> #include <string.h> using namespace std;void fun (int &a) { a = 19 ; } void fun1 (int a) { cout << a<<endl; } void fun2 (int *a) { *a = 46 ; } int main () { int b = 14 ; fun (b); cout << b<<endl; fun2 (&b); cout<<b; return 0 ; }
for循环
1 2 3 4 5 6 7 8 9 10 11 12 #include <iostream> #include <string.h> using namespace std;int main () { for (int i = 0 ; i < 5 ; i++) { cout<<i <<endl; } return
函数缺省值
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 #include <iostream> using namespace std;void fun3 (int a = 45 , float g = 67.98 ) ;void fun (int a = 12 , char b = 'm' ) { cout << a <<' ' << b << endl; } void fun2 (int a , char b ,float f = 123.78 ) { cout << a <<' ' << b <<' ' << f <<endl; } int main () { fun (); fun2 (54 ,'y' ); fun (50 ,'r' ); fun3 (); return 0 ; } void fun3 (int a , float g ) { cout << a <<' ' << g << endl; }
函数重载
同一作用域内,函数名字相同,参数列表不同(参数类型或参数个数不同)
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 #include <iostream> using namespace std;void show (int a ) { cout << a << endl; } void show (float f ) { cout << f << endl; } void show (char c) { cout << c << endl; } int main () { show (23 ); show (43.56f ); show ('h' ); return 0 ; }
函数返回值不作为函数重载的条件
1 2 3 int fun (int a) void fun (int a)
类
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 #include <iostream> using namespace std;class Cpeople { public : int a; void fun () { cout << a << endl; } }; int main () { Cpeople op; op.a = 15 ; op.fun (); Cpeople *op1 = new Cpeople; op1->a = 56 ; op1->fun (); delete op1; return 0 ; }
友元
友元函数,不相关的函数去使用类内私有成员
友元类,不同类使用另一个类私有成员
image-20211004120911813
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 #include <iostream> using namespace std;class Cpeople { public : int a; private : void fun () { a = 54 ; cout << a << endl; } protected : void fun_pro () { a = 54 ; cout << a << endl; } friend void fun2 () ; friend int main () ; friend class CP ; }; class Cchild : public Cpeople{ public : void fun6 () { fun_pro (); } }; void fun2 () { Cpeople op2; op2.fun (); } class CP { public : Cpeople op3; void fun3 () { op3.fun (); } }; int main () { Cpeople op; op.fun (); CP op4; op4.fun3 (); Cchild op_child; op_child.fun6 (); return 0 ; }
类和int一样,都是一种数据类型。只有用类声明对象时,才会开辟出一块空间
成员函数
构造函数:自动调用
在对象创建时候,调用。作用,==初始化类内的变量,赋值==。不要在构造函数内部加不相干的代码
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 #include <iostream> using namespace std;class CStu { public : int a; float f; CStu () { a = 14 ; f = 53.98f ; } }; int main () { CStu student1; CStu *stu2 = new CStu; cout << stu2->f << endl; cout << student1.a <<' ' << student1.f << endl; return 0 ; }
==类中的函数,可以类内声明,类外定义==
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 #include <iostream> #include <string> using namespace std;class Cstudent { public : string stu_name; int stu_id; public : void show () { cout << "The name of the student:" << stu_name <<endl; cout << "The id of the student:" << stu_id <<endl; } void set_name (string name) { stu_name = name; } void set_id (int id) { stu_id = id; } }; int main () { Cstudent stu; stu.set_id (15 ); stu.set_name ("张三" ); stu.show (); return 0 ; }
成员属性私有化
用公共的函数接口来访问
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 #include <iostream> #include <string> using namespace std;class Cperson { public : void set_name (string name) { C_name = name; } string get_name () { return C_name; } int get_age () { C_age = 18 ; return C_age; } private : string C_name; int C_age; string C_sex; }; int main () { Cperson person1; person1.set_name ("张三" ); cout << "The name is " << person1.get_name () << endl; cout << "The age is " << person1.get_age () << endl; return 0 ; }
this指针
==对象存在,this指针才存在==
==this指针的作用域在类内,系统默认传递给函数的隐含参数,只能在类内成员函数内部使用==
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 #include <iostream> using namespace std;class Cstu { public : int a; public : void show () { cout << a << endl; } Cstu (int a) { this ->a = a; this ->show (); } Cstu* getaddr () { return this ; } }; int main () { Cstu stu1 (12 ) ; stu1.show (); Cstu*p = stu1.getaddr (); cout << p <<endl; p->show (); Cstu stu2 (17 ) ; Cstu*p2 = stu2.getaddr (); cout << p2 << endl; return 0 ; }
static&const
==static可以使用类名作用域调用,说明是存在于类中的,和对象没有关系,没有声明对象时,也存在静态成员,在创建类的时候就给静态成员分配了空间。是类本身的属性,和对象没有关系==
只有静态常量整型数据成员才能在类中初始化
静态函数 常函数
静态变量 常变量
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 72 #include <iostream> using namespace std;class Cstu { public : static int a; static const int b = 17 ; Cstu () { a = 12 ; } }; int Cstu::a = 19 ;int main () { cout << Cstu::a << endl; Cstu stu1; cout << stu1.a << endl; cout << stu1.b ; return 0 ; } ------------------------------------------------------------------ #include <iostream> using namespace std;class Cstu { public : static int a; static void fun () { cout << "I am static function." << endl; } void fun2 () { cout << "I am function." << endl; } Cstu () { a = 12 ; } }; int Cstu::a = 19 ;int main () { cout << Cstu::a << endl; Cstu::fun (); Cstu::fun2 (); Cstu stu1; cout << stu1.a << endl; stu1.fun (); return 0 ; }
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 #include <iostream> using namespace std;class Cperson { public : ~Cperson () { cout << "Cperson的默认析构函数" << endl; } Cperson (int age) { c_age = age; cout << "Cperson的有参构造函数" << endl; } int c_age; }; void test01 () { Cperson per1 (13 ) ; Cperson per2 (per1) ; cout << "age of per2 is " << per2.c_age << endl; } void test02 () { Cperson per3 (98 ) ; Cperson per4 (per3) ; cout << "age of per4 is " << per4.c_age << endl; } int main () { test01 (); test02 (); system ("pause" ); return 0 }
浅拷贝&深拷贝
==如果属性由在堆区开辟的,一定要自己提供拷贝构造函数,放置浅拷贝带来的问题==
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 #include <iostream> using namespace std;class Cperson { public : Cperson () { cout << "Cperson的默认构造函数" << endl; } ~Cperson () { if (c_height != NULL ) { delete c_height; } cout << "Cperson的默认析构函数" << endl; } Cperson (int age, int height) { c_height = new int (height); c_age = age; cout << "Cperson的有参构造函数" << endl; } Cperson (const Cperson& a) { c_age = a.c_age; c_height = new int (*a.c_height); cout << "cperson的拷贝构造函数" << endl; } public : int c_age; int *c_height; }; void test01 () { Cperson per1 (19 ,179 ) ; Cperson per2 (per1) ; cout << "age of per1 is " << per1.c_age << "height of per1 is " << *per1.c_height << endl; cout << "age of per2 is " << per2.c_age << "height of per2 is " << *per2.c_height << endl; } int main () { test01 (); system ("pause" ); return 0 ; }
构造函数初始化列表
Cperson():C_a(a),C_b(b),C_c(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 #include <iostream> using namespace std;class Cperson { public : Cperson (int a,int b,int c) : C_a (a),C_b (b),C_c (c) { } public : int C_a; int C_b; int C_c; }; void test02 () { Cperson per2 (45 ,76 ,43 ) ; cout << "C_a: " << per2.C_a <<endl; cout << "C_b: " << per2.C_b <<endl; cout << "C_c: " << per2.C_c <<endl; } int main () { test02 (); system ("pause" ); return 0 ; }
类作对象
==构造函数的顺序是:先构造内层的,在构造外层的,析构函数的执行顺序服从“先进后出”的规则,与构造函数顺序相反。==
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 #include <iostream> using namespace std;#include <string> class Cphone { public : Cphone (string name) { cout << "phone" << endl; C_PhoneNmae = name; } ~Cphone () { cout << "phone destructor" << endl; } public : string C_PhoneNmae; }; class Cperson { public : string c_name; Cphone c_phone; public : Cperson (string name,string phone):c_name (name),c_phone (phone) { cout << "person " << endl; } ~Cperson () { cout << "person destructor" << endl; } }; void test01 () { Cperson per1 ("zhangsan" ,"xiaomi" ) ; cout << per1.c_name << " have " << per1.c_phone.C_PhoneNmae << endl; } int main () { test01 (); system ("pause" ); return 0 ; }
image-20211018233256498
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 #include <iostream> using namespace std;class Cperson { public : int c_age; static int c_name; void fun () { } static void fun1 () {} }; void test01 () { Cperson per1; cout << "size of per1 is " << sizeof (per1) << endl; } int main () { test01 (); system ("pause" ); return 0 ; }
空指针访问成员函数
类中的成员属性默认在前面有this-> .而person*p = NULL 语句并没有创建对象,所以是无法访问对象的,所以this指针是不存在的,
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 #include <iostream> using namespace std;class Cperson { public : int c_age; void showName () { cout << "this is the class name" << endl; } void showAge () { if (this == NULL ) { return ; } cout << this ->c_age << endl; } }; void test01 () { Cperson *p = NULL ; p->showName (); } int main () { test01 (); system ("pause" ); return 0 ; }
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 #include <iostream> using namespace std;#include <string> class CBuilding { public : CBuilding (); public : string c_livingroom; private : string c_bedroom; friend class CGoodgay ; friend void goodgay::visit () ; }; CBuilding::CBuilding () { c_livingroom = "living room" ; c_bedroom = "bed room" ; } class CGoodgay { public : CBuilding * building1; CGoodgay (); void visit () ; }; CGoodgay::CGoodgay () { building1 = new CBuilding; } void CGoodgay::visit () { cout << "Good gay is looking at " << building1->c_livingroom << endl; cout << "Good gay is looking at " << building1->c_bedroom << endl; } void test01 () { CGoodgay gay1; gay1.visit (); } int main () { test01 (); system ("pause" ); return 0 ; }
image-20211019172544775
image-20211019172649352
运算符重载
image-20211019233541178
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 #include <iostream> using namespace std;class CPerson { public : CPerson operator +(CPerson &p) { CPerson temp; temp.c_a = this ->c_a + p.c_a; temp.c_b = this ->c_b + p.c_b; return temp; } public : int c_a; int c_b; }; void test01 () { CPerson per1; per1.c_a = 34 ; per1.c_b = 54 ; CPerson per2; per2.c_a = 54 ; per2.c_b = 74 ; CPerson per3; per3 = per1 + per2; cout << "per3.c_a =" << per3.c_a << endl; cout << "per3.c_b =" << per3.c_b << endl; } Cperson p3 = p1.operator +(p2) 简写:p3 = p1+p2; 全局函数重载本质调用 Cperson p3 = operator +(p1,p2) 简写: p3 = p1+p2 int main (){ test01 (); system ("pause" ); return 0 ; }
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 #include <iostream> using namespace std;class Cperson { public : int c_a; int c_b; Cperson () { c_a = 89 ; c_b = 93 ; } }; ostream & operator <<(ostream &cout ,Cperson &p) { cout << "c_a: " << p.c_a << endl; cout << "c_b: " << p.c_b << endl; return cout; } void test01 () { Cperson per1; cout << per1 << endl; } int main () { test01 (); system ("pause" ); return 0 ; }
递增运算符重载
image-20211020135253168
继承
好处:减少重复的代码
语法:class 子类:继承方式 父类
子类又叫派生类
父类又叫基类
image-20211020164955371
父类中的非静态成员变量都会被继承下去,无论是否私有还是公有
子类与父类中的同名函数,同名变量。默认会调用子类中的,子类中的会隐藏掉父类中所有的同名函数
image-20211020174208051
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 #include <iostream> using namespace std;class CFather { public : CFather () { c_a = 34 ; } public : int c_a; }; class CSon :public CFather{ public : int c_a; CSon () { c_a = 56 ; } }; void test01 () { CSon son1; cout << son1.c_a << endl; cout << son1.CFather::c_a << endl; } int main () { test01 (); system ("pause" ); return 0 ; }
image-20211020214242953
多态
image-20211020220806664
重载:函数名相同,参数不同
重写:函数返回值,函数名,形参列表都相同
image-20211020221534209
image-20211020221707835
image-20211020221741333
==成员函数不属于对象==
==不管是什么类型的指针,都占4个字节==
image-20211021202239635
image-20211021202658926
image-20211021203623112
image-20211021203952490
image-20211021204111880
==通过父类的指针,可以调用不同的子类对象的函数或成员==
image-20211021220439165
image-20211021221722313
image-20211021225354564
image-20211021225639639
image-20211021230353091
image-20211021230635796
image-20211021231644579
image-20211022105401533
image-20211022110526056
image-20211022111543036
image-20211022111709836
image-20211022121233984
image-20211024224340411
image-20211024224522966
image-20211024224613026
image-20211024224747037
image-20211024224808615
image-20211024224924888
==接口都是一样的,由于对象不同,就显示出多态的意义了 ==
一个接口,有多种形态
image-20211026102816318
image-20211026103235801
image-20211026103307827
image-20211026170316284
image-20211026170448680
image-20211026170501131
image-20211026172757953
image-20211026173241390
image-20211026174105364
如何判断数据是否为空
EOF文件的结尾
image-20211026174241876
image-20211026174807707
image-20211026224231250
image-20211026224303886
image-20211026224329145
image-20211026230436353
image-20211026230456972
image-20211027092044192
image-20211027092222524
image-20211027092242701
image-20211027105929960
image-20211027105946262
模板
image-20211030172724604
image-20211030173054674
image-20211030173746509
image-20211031101246245
image-20211031101741998
image-20211031104651606
image-20211031104853068
image-20211031110403838
image-20211031110431568
image-20211031110841773
image-20211031111537579
image-20211031112511357
image-20211031112810468
image-20211031112838946
算法
冒泡排序
image-20211110222527770
image-20211110224058824