笔记 C++ C++面向对象编程 小楼夜听雨 2024-05-10 2025-01-03 内存分区模型 C++程序在执行时,将内存大方向划分为4个区域
代码区:存放函数体的二进制代码,由操作系统进行管理的
全局区:存放全局变量和静态变量以及常量
栈区:由编译器自动分配释放, 存放函数的参数值,局部变量等
堆区:由程序员分配和释放,若程序员不释放,程序结束时由操作系统回收
内存四区意义:
不同区域存放的数据,赋予不同的生命周期, 给我们更大的灵活编程
程序运行前 在程序编译后,生成了exe可执行程序,未执行该程序前 分为两个区域
代码区:
存放 CPU 执行的机器指令
代码区是共享 的,共享的目的是对于频繁被执行的程序,只需要在内存中有一份代码即可
代码区是只读 的,使其只读的原因是防止程序意外地修改了它的指令
全局区:
全局变量和静态变量存放在此.
全局区还包含了常量区, 字符串常量和其他常量也存放在此.
该区域的数据在程序结束后由操作系统释放.
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 #include <iostream> using namespace std;int c = 0 ;int d = 0 ;const int g = 0 ;int main () { int a = 0 ; int b = 0 ; cout << "局部变量a的地址为:" << (int )&a << endl; cout << "局部变量b的地址为:" << (int )&b << endl; cout << "全局变量c的地址为:" << (int )&c << endl; cout << "全局变量d的地址为:" << (int )&d << endl; static int e = 0 ; static int f = 0 ; cout << "静态变量e的地址为:" << (int )&e << endl; cout << "静态变量f的地址为:" << (int )&f << endl; const int h = 0 ; cout << "字符串常量的地址为:" << (int )&"123456" << endl; cout << "全局常量的地址为:" << (int )&g << endl; cout << "局部常量的地址为:" << (int )&h << endl; system ("pause" ); return 0 ; }
总结:
C++中在程序运行前分为全局区和代码区
代码区特点是共享和只读
全局区中存放全局变量、静态变量、常量
常量区中存放 const修饰的全局常量 和 字符串常量
程序运行后 栈区:
由编译器自动分配释放, 存放函数的参数值,局部变量等
注意事项:不要返回局部变量的地址,栈区开辟的数据由编译器自动释放
堆区:
由程序员分配释放,若程序员不释放,程序结束时由操作系统回收
在C++中主要利用new在堆区开辟内存
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 #include <iostream> using namespace std;int * func (int b) { b = 100 ; int a = 10 ; return &a; } int * func1 () { int * p = new int (100 ); return p; } int main2 () { int b=0 ; int * p = func (b); cout << *p << endl; cout << *p << endl; int * p1 = func1 (); cout << *p1 << endl; cout << *p1 << endl; system ("pause" ); return 0 ; }
总结:
堆区数据由程序员管理开辟和释放
堆区数据利用new关键字进行开辟内存
new操作符和在堆区开辟数组 C++中利用new操作符在堆区开辟数据
堆区开辟的数据,由程序员手动开辟,手动释放,释放利用操作符 delete
释放堆区数组的时候,要加[] delete[]arr;
语法: new 数据类型
利用new创建的数据,会返回该数据对应的类型的指针
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 #include <iostream> using namespace std;int * func10 () { int * p = new int (5201314 ); return p; } void test01 () { int * p = func10 (); cout << *p << endl; cout << *p << endl; cout << *p << endl; delete p; } void test02 () { int * arr = new int [10 ]; for (int a = 0 ; a < 10 ; a++) { arr[a] = a+100 ; } for (int a = 0 ; a < 10 ; a++) { cout << arr[a]<<endl; } delete []arr; } int main () { test01 (); test02 (); system ("pause" ); return 0 ; }
引用 引用的基本使用 **作用: **给变量起别名
语法: 数据类型 &别名 = 原名
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <iostream> using namespace std;int main0 () { int a = 10 ; int & b = a; b = 20 ; cout << a << endl; system ("pause" ); return 0 ; }
引用注意事项
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> using namespace std;int main1 () { int a = 10 ; int & b = a; b = 20 ; cout << a << endl; 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 #include <iostream> using namespace std;void A (int & a, int & b) { int temp; temp = a; a = b; b = temp; } int main3 () { int a = 10 ; int b = 20 ; A (a, b); cout << a << endl; cout << b << endl; 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 #include <iostream> using namespace std;int & test02 () { int a = 0 ; return a; } int & test03 () { static int a = 0 ; return a; } int main4 () { int & ref = test02 (); cout << ref << endl; cout << ref << endl; int & ref2 = test03 (); cout << ref2 << endl; cout << ref2 << endl; test03 () = 1000 ; cout << ref2 << endl; cout << ref2 << endl; system ("pause" ); return 0 ; }
引用的本质 本质:引用的本质在c++内部实现是一个指针常量.
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 void func (int & ref) { ref = 100 ; } int main () { int a = 10 ; int & ref = a; ref = 20 ; cout << "a:" << a << endl; cout << "ref:" << ref << endl; func (a); return 0 ; }
结论:C++推荐用引用技术,因为语法方便,引用本质是指针常量,但是所有的指针操作编译器都帮我们做了
常量引用 作用: 常量引用主要用来修饰形参,防止误操作
在函数形参列表中,可以加const修饰形参,防止形参改变实参
示例:
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 #include <iostream> using namespace std;void ab (const int & a) { cout << a << endl; } int main () { int a = 10 ; const int & ref = 10 ; ab (a); system ("pause" ); return 0 ; }
函数提高 函数默认参数 在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 #include <iostream> using namespace std;int func (int a, int b=20 , int c=30 ) { return a + b + c; } int func2 (int a, int b=30 ) ;int main0 () { cout << func (10 ) << endl; system ("pause" ); return 0 ; }
注意:如果某个位置已经有了默认值,那么从这个位置往后,从左到右都必须有默认值
注意:如果函数声明有默认参数,函数实现就不能有默认参数(会重复定义默认参数,声明和实现只能一个有默认参数)
函数占位参数 C++中函数的形参列表里可以有占位参数,用来做占位,调用函数时必须填补该位置
语法: 返回值类型 函数名 (数据类型){}
在现阶段函数的占位参数存在意义不大,但是后面的课程中会用到该技术
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <iostream> using namespace std;void func4 (int a,int ,int =10 ) {}int main1 () { 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 #include <iostream> using namespace std;void func5 () { cout << "funf5的调用" << endl; } void func5 (int a) { cout << "funf5(int a)的调用2" << endl; } void func5 (double a) { cout << "funf5(double a)的调用3" << endl; } void func5 (double a,int b) { cout << "funf5(double a,int b)的调用3" << endl; } void func5 ( int b,double a) { cout << "funf5( int b,double a)的调用3" << endl; } void A () {cout << "funf5的调用" << endl;}int main4 () { func5 (); func5 (10 ); func5 (10.0 ); func5 (10.0 ,1 ); func5 (1 ,10.0 ); 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 #include <iostream> using namespace std;void func6 (int & a) { cout << "func6(int& a)的引用" << endl; } void func6 (const int & a) { cout << "func6(const int& a)的引用" << endl; } void func7 (int a) { cout << "func(int a)的引用" << endl; } \ void func7 (int a,int b=10 ) { cout << "func(int a,int b=10)的引用" << endl; } int main () { int a = 10 ; func6 (a); func6 (10 ); func7 (a,10 ); system ("pause" ); return 0 ; }
注意:有引用这个类型的形参的时候不能定义相同类型的形参
类和对象 C++面向对象的三大特性为:封装、继承、多态
C++认为万事万物都皆为对象,对象上有其属性和行为
属性和行为,统一称为成员 属性 也叫 成员属性 成员变量 行为 也叫 成员函数 成员方法
例如:
人可以作为对象,属性有姓名、年龄、身高、体重…,行为有走、跑、跳、吃饭、唱歌…
车也可以作为对象,属性有轮胎、方向盘、车灯…,行为有载人、放音乐、放空调…
具有相同性质的对象,我们可以抽象称为类,人属于人类,车属于车类
封装 封装是C++面向对象三大特性之一
封装的意义:
将属性和行为作为一个整体,表现生活中的事物
将属性和行为加以权限控制
封装的意义 封装意义一:设计类 在设计类的时候,属性和行为写在一起,表现事物
语法: class 类名{ 访问权限: 属性 / 行为 };
示例1: 设计一个圆类,求圆的周长
示例代码:
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; const double PI = 3.14 ;class Circle { public : int r; double ZC () { return 2 * PI * r; } }; int main2 () { Circle c1; c1.r = 10 ; cout << "圆的周长为:" << c1.ZC () << endl; system ("pause" ); return 0 ; }
一般情况下,我们把类的声明部分写在自定义的头文件上,把类的实现部分写在一个单独的源文件(.cpp)内,以简化主函数源文件的代码数量,详细内容看案例练习部分
封装意义二:封装的权限控制 类在设计时,可以把属性和行为放在不同的权限下,加以控制
访问权限有三种:
public 公共权限 成员类内外都可访问
protected 保护权限 成员类内可以访问,类外不可访问 在继承中,子可以访问父中的保护内容
private 私有权限 成员类内可以访问,类外不可访问 在继承中,子不可以访问父中的私有内容
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;#include <string> class parson { public : string name; protected : int car_id; private : int password; public : void func () { name = "张三" ; car_id = 123456 ; password = 123654 ; } }; int main3 () { parson p1; p1.name = "王五" ; system ("pause" ); return 0 ; }
struct和class区别 在C++中 struct和class唯一的区别 就在于 默认的访问权限不同
区别:
struct 默认权限为公共
class 默认权限为私有
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;#include <string> class C1 { int a; }; struct B1 { int a; }; int main0 () { C1 c1; B1 b1; b1.a = 100 ; system ("pause" ); return 0 ; }
成员属性设置为私有 优点1: 将所有成员属性设置为私有,可以自己控制读写权限
优点2: 对于写权限,我们可以检测数据的有效性
示例:
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;#include <string> class person { public : void setname (string name1) { name = name1; } string getname () { return name; } int getage () { return age; } void setidol (string idol2) { idol = idol2; } void setage (int age1) { if (age1 < 0 || age1>150 ) { cout << "输入有误" << endl; return ; } } private : string name; int age=18 ; string idol; }; int main7 () { person p1; p1.setname ("张三" ); p1.setage (156 ); system ("pause" ); ret
对象的初始化和清理
生活中我们买的电子产品都基本会有出厂设置,在某一天我们不用时候也会删除一些自己信息数据保证安全
C++中的面向对象来源于生活,每个对象也都会有初始设置以及 对象销毁前的清理数据的设置。
构造函数和析构函数 对象的初始化和清理 也是两个非常重要的安全问题
一个对象或者变量没有初始状态,对其使用后果是未知
同样的使用完一个对象或变量,没有及时清理,也会造成一定的安全问题
c++利用了构造函数 和析构函数 解决上述问题,这两个函数将会被编译器自动调用,完成对象初始化和清理工作。
对象的初始化和清理工作是编译器强制要我们做的事情,因此如果我们不提供构造和析构,编译器会提供
编译器提供的构造函数和析构函数是空实现。
构造函数:主要作用在于创建对象时为对象的成员属性赋值,构造函数由编译器自动调用,无须手动调用。
析构函数:主要作用在于对象销毁前 系统自动调用,执行一些清理工作。
构造函数语法: 类名(){}
构造函数,没有返回值也不写void
函数名称与类名相同
构造函数可以有参数,因此可以发生重载
程序在调用对象时候会自动调用构造,无须手动调用,而且只会调用一次
析构函数语法: ~类名(){}
析构函数,没有返回值也不写void
函数名称与类名相同,在名称前加上符号 ~
析构函数不可以有参数,因此不可以发生重载
程序在对象销毁前会自动调用析构,无须手动调用,而且只会调用一次
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 person { public : person () { cout << "person构造函数的调用1111" << endl; } ~person () { cout << "person析构函数的调用" << endl; } }; void ABC () { person p1; } int main00 () { ABC (); person p2; 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 70 71 72 73 74 75 76 #include <iostream> using namespace std;class person { public : person () {cout << "person无参构造函数的调用" << endl;} person (int a) { age = a; cout << "person有参构造函数的调用" << endl; } person (const person& p) { cout << "person拷贝构造函数的调用" << endl; age = p.age; } ~person () { cout << "person析构函数的调用" << endl;} int age;}; void test1 () { person p; person p2 (10 ) ; person p3 (p2) ; cout << "p2的年龄:" << p2.age << endl; cout << "p3的年龄:" << p3.age << endl; person p1; person p4 = person (10 ); person p5 = person (p2); person (10 ); person (p6); cout << "aaa" << endl; person p7 = 10 ; person p8 = p7; } int main01 () { test1 (); system ("pause" ); return 0 ; }
注意: 拷贝函数必须是引用传递,否则会无限递归;
拷贝构造函数调用时机 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 72 73 74 75 76 77 78 79 80 81 82 83 #include <iostream> using namespace std;class person1 { public : person1 () { cout << "person无参构造函数的调用" << endl; } person1 (int a) { age = a; cout << "person有参构造函数的调用" << endl; } person1 (const person1& p) { age = p.age; cout << "person拷贝构造函数的调用" << endl; } ~person1 () { cout << "person析构函数的调用" << endl; } int age; }; void test01 () { person1 p1 (20 ) ; person1 p2 (p1) ; cout << "p2的年龄:" << p2.age << endl; } void aaa (person1 a) {} void test02 () { person1 p3; aaa (p3); } person1 bbb () { person1 p5 (20 ) ; cout << "p5的年龄:" << p5.age << endl; cout << (int )&p5; return person1 (p5); } void test03 () { person1 p4 = bbb (); cout << "p4的年龄:" << p4.age << endl; p4.age = 30 ; cout << (int )&p4; } int main004 () { test03 (); system ("pause" ); return 0 ; }
构造函数调用规则 默认情况下,c++编译器至少给一个类添加3个函数
1.默认构造函数(无参,函数体为空)
2.默认析构函数(无参,函数体为空)
3.默认拷贝构造函数,对属性进行值拷贝
构造函数调用规则如下:
如果用户定义有参构造函数,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 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 person2 { public : person2 (const person2& p) { age = p.age; cout << "person拷贝构造函数的调用" << endl; } ~person2 () { cout << "person析构函数的调用" << endl; } int age; }; void test001 () { } void test002 () { } int main002 () { test001 (); 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 #include <iostream> using namespace std;class person3 { public : person3 () { cout << "person构造函数的调用" << endl; } person3 (int age1,int heighit1) { age = age1; height = new int (heighit1); cout << "person有参构造函数的调用" << endl; } ~person3 () { if (height != NULL ) { delete height; height = NULL ; } cout << "person析构函数的调用" << endl; } person3 (const person3& p) { height = new int (*p.height); age = p.age; } int age; int * height; }; void sest003 () { person3 p1 (20 ,160 ) ; person3 p2 (p1) ; cout << "p2的年龄:" << p2.age << endl; cout << "p2的身高:" << *p2.height << endl; } int main005 () { sest003 (); system ("pause" ); return 0 ; }
总结:如果属性有在堆区开辟的,一定要自己提供拷贝构造函数,防止浅拷贝带来的问题
初始化列表 作用:
C++提供了初始化列表语法,用来初始化属性
语法: 构造函数():属性1(值1),属性2(值2)... {}
示例:
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 #include <iostream> using namespace std;class person4 { public : person4 () :a1 (10 ), b1 (20 ), c1 (30 ) { } person4 (int a, int b, int c) :a1 (a), b1 (b), c1 (c) { } int a1; int b1; int c1; }; void sest005 () { person4 p; person4 p2 (30 ,20 ,10 ) ; cout << p.a1 << endl; cout << p.b1 << endl; cout << p.c1 << endl; cout << p2.a1 << endl; cout << p2.b1 << endl; cout << p2.c1 << endl; } int main006 () { sest005 (); system ("pause" ); return 0 ; }
类对象作为类成员 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 #include <iostream> using namespace std;#include <string> class phone { public : phone (string name) { name1 = name; cout << "手机的构造函数调用" << endl; } string name1; ~phone () { cout << "手机析构函数的调用" << endl; } }; class person5 { public : person5 (string name1, string name2):name (name1),sj (name2) { cout << "人的构造函数调用" << endl; } ~person5 () { cout << "人析构函数的调用" << endl; } string name; phone sj; }; void sest006 () { person5 p1 ("张三" , "苹果15" ) ; cout << p1.name << p1.sj.name1 << endl; } int main007 () { sest006 (); system ("pause" ); return 0 ; }
静态成员 静态成员就是在成员变量和成员函数前加上关键字static,称为静态成员
静态成员分为:
静态成员变量
所有对象共享同一份数据
在编译阶段分配内存
类内声明,类外初始化
静态成员函数
所有对象共享同一个函数
静态成员函数只能访问静态成员变量
示例1 : 静态成员变量(末影箱里面的物品的数据)
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 #include <iostream> using namespace std;#include <string> class phone { public : static int a; private : static int b; }; int phone::a=100 ;int phone::b=200 ;void sest007 () { phone p1; cout << p1.a << endl; phone p2; p2.a = 200 ; cout << p1.a << endl; cout << p2.a << endl; } void sest008 () { phone p; cout << p.a << endl; cout << phone::a << endl; } int main008 () { sest008 (); system ("pause" ); return 0 ; }
示例2: 静态成员函数(末影箱,只能访问末影箱物品的数据,不能访问箱子的数据)
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;#include <string> class phone { public : static void func () { c = 300 ; cout << "静态函数的调用" << endl; } static int c; int e; private : static int d; static void func2 () { cout << "静态函数的调用" << endl; } }; int phone::c = 100 ;int phone::d = 200 ;void sest009 () { phone p1; p1.func (); phone::func (); } int main () { sest009 (); system ("pause" ); return 0 ; }
C++对象模型和this指针 成员变量和成员函数分开存储 在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 #include <iostream> using namespace std;class person { int a; static int b; void func () {} static void func2 () {} }; int person::b = 10 ;void test0 () { person p; cout << "空对象的内存多大:" << sizeof (p) << endl; } void test1 () { person p; cout << "有int的对象的内存多大:" << sizeof (p) << endl; } int main0 () { test1 (); system ("pause" ); return 0 ; }
this指针概念 通过上面我们知道在C++中成员变量和成员函数是分开存储的
每一个非静态成员函数只会诞生一份函数实例,也就是说多个同类型的对象会共用一块代码
那么问题是:这一块代码是如何区分那个对象调用自己的呢?
c++通过提供特殊的对象指针,this指针,解决上述问题。this指针指向被调用的成员函数所属的对象
this指针是隐含每一个非静态成员函数内的一种指针
this指针不需要定义,直接使用即可
this指针的用途:
当形参和成员变量同名时,可用this指针来区分
在类的非静态成员函数中返回对象本身,可使用return *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 45 46 47 48 49 50 51 52 #include <iostream> using namespace std;class person { public : person (int age) { this ->age = age; } int age; person& add (person &p) { this ->age +=p.age; return *this ; } }; void test3 () { person p1 (18 ) ; cout << "p1的年龄:" << p1.age << endl; } void test2 () { person p2 (10 ) ; person p3 (20 ) ; p3.add (p2).add (p2).add (p2); cout << "p3的年龄:" << p3.age << endl; } int main1 () { test2 (); system ("pause" ); return 0 ; }
空指针访问成员函数 C++中空指针也是可以调用成员函数的,但是也要注意有没有用到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 45 46 47 48 #include <iostream> using namespace std;class person { public : void ShowClassName () { cout << "这个类的名称叫 person" << endl; } void ShowAge () { if (this == NULL ) { return ; } cout << "这个人的年龄:" << age << endl; } int age; }; void test4 () { person* p = NULL ; p->ShowAge (); p->ShowClassName (); } void test5 () {} int main2 () { test4 (); system ("pause" ); return 0 ; }
const修饰成员函数 常函数:
成员函数后加const后我们称为这个函数为常函数
常函数内不可以修改成员属性
成员属性声明时加关键字mutable后,在常函数中依然可以修改
常对象:
声明对象前加const称该对象为常对象
常对象只能调用常函数
示例:
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 person { public : void showage () const { age1 = 100 ; } int age; mutable int age1; void showage2 () { } }; void test6 () { const person p; p.age1 = 500 ; } void test7 () {} int main () { test6 (); system ("pause" ); return 0 ; }
友元 生活中你的家有客厅(Public),有你的卧室(Private)
客厅所有来的客人都可以进去,但是你的卧室是私有的,也就是说只有你能进去
但是呢,你也可以允许你的好闺蜜好基友进去。
在程序里,有些私有属性 也想让类外特殊的一些函数或者类进行访问,就需要用到友元的技术
友元的目的就是让一个函数或者类 访问另一个类中私有成员
友元的关键字为 friend
友元的三种实现
全局函数做友元 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 #include <iostream> using namespace std;#include <string> class buiding { friend void goodgay (buiding* buiding) ; public : buiding () { m_sittingroom = "客厅" ; m_bedroom="卧室" ; } public : string m_sittingroom; private : string m_bedroom; }; void goodgay (buiding *buiding) { cout << "好基友的全局函数正在访问:" << buiding->m_sittingroom << endl; cout << "好基友的全局函数正在访问:" << buiding->m_bedroom << endl; } void test01 () { buiding b1; goodgay (&b1); } int main00 () { 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 buiding ;class goodgay { public : goodgay (); void visit () ; buiding * buiding1; }; class buiding { friend class goodgay ; public : buiding (); public : string m_sittingroom; private : string m_bedroom; }; buiding::buiding () { m_sittingroom = "客厅" ; m_bedroom = "卧室" ; } goodgay::goodgay () { buiding1 = new buiding; } void goodgay::visit () { cout<< "好基友的类正在访问:" <<buiding1->m_sittingroom << endl; cout << "好基友的类正在访问:" << buiding1->m_bedroom << endl; }; void test02 () { goodgay gg; gg.visit (); } 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 58 59 60 61 62 63 64 65 66 67 68 69 #include <iostream> using namespace std;#include <string> class buiding ;class goodgay { public : goodgay (); void visit () ; void visit2 () ; buiding* buiding1; }; class buiding { friend void goodgay::visit () ; public : buiding (); public : string m_sittingroom; private : string m_bedroom; }; buiding::buiding () { m_sittingroom = "客厅" ; m_bedroom = "卧室" ; } goodgay::goodgay () { buiding1 = new buiding; } void goodgay::visit () { cout << "好基友的成员函数正在访问:" << buiding1->m_sittingroom << endl; cout << "好基友的成员函数正在访问:" << buiding1->m_bedroom << endl; }; void goodgay::visit2 () { cout << "好基友的成员函数2正在访问:" << buiding1->m_sittingroom << endl; }; void test02 () { goodgay gg; gg.visit (); gg.visit2 (); } 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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 #include <iostream> using namespace std;class person { public : int m_a; int m_b; }; person operator +(person &p1,person &p2) { person temp; temp.m_a = p1.m_a + p2.m_a; temp.m_b = p1.m_b + p2.m_b; return temp; } person operator +(person& p1, int p2) { person temp; temp.m_a = p1.m_a + p2; temp.m_b = p1.m_b + p2; return temp; } void test01 () { person p1; p1.m_a = 10 ; p1.m_b = 20 ; person p2; p2.m_a = 10 ; p2.m_b = 20 ; person p3 = p1 + p2; cout << "p3.m_a=" << p3.m_a << endl; cout << "p3.m_b=" << p3.m_b<< endl; person p4 = p1 + 100 ; cout << "p4.m_a=" << p4.m_a << endl; cout << "p4.m_b=" << p4.m_b << endl; } int main () { test01 (); system ("pause" ); return 0 ; }
总结1:对于内置的数据类型的表达式的的运算符是不可能改变的
总结2:不要滥用运算符重载
左移运算符重载 作用:可以输出自定义数据类型
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 #include <iostream> using namespace std;class person { friend ostream& operator <<(ostream& cout, person& p); public : person (int a, int b) { m_a = a; m_b = b; } private : int m_a; int m_b; }; ostream & operator <<(ostream &cout, person& p) { cout << p.m_a << endl; cout << p.m_b << endl; return cout; } void test01 () { person p1 (10 ,20 ) ; cout << p1 <<endl; } 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 64 65 66 67 68 69 70 #include <iostream> using namespace std;class MyInteger { friend ostream& operator << (ostream& cout, MyInteger p); public : MyInteger () { m_a = 0 ; } MyInteger& operator ++() { m_a++; return *this ; } MyInteger operator ++(int ) { MyInteger temp = *this ; m_a++; return temp; } private : int m_a; }; ostream& operator << (ostream & cout, MyInteger p) { cout << p.m_a << endl; return cout; } void test () { MyInteger p; cout << ++(++p) << endl; cout << p << endl; } void test1 () { MyInteger p; cout << p++ << endl; cout << p << endl; } int main () { test1 (); system ("pause" ); return 0 ; }
总结: 前置递增返回引用,后置递增返回值
赋值运算符重载 c++编译器至少给一个类添加4个函数
默认构造函数(无参,函数体为空)
默认析构函数(无参,函数体为空)
默认拷贝构造函数,对属性进行值拷贝
赋值运算符 operator=, 对属性进行值拷贝
如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题
示例:
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 #include <iostream> using namespace std;class person { public : person (int age) { m_age = new int (age); } ~person () { cout <<1 ; if (m_age != NULL ) { delete m_age; m_age = NULL ; } } person& operator =(person &p) { if (m_age != NULL ) { delete m_age; m_age = NULL ; } m_age = new int (*p.m_age); return *this ; } int * m_age; }; void test () { person p1 (18 ) ; person p2 (20 ) ; person p3 (30 ) ; p3=p2 = p1; cout << "p1的年龄是:" << *p1.m_age << endl; cout << "p2的年龄是:" << *p2.m_age << endl; cout << "p3的年龄是:" << *p3.m_age << endl; } int main () { test (); 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 #include <iostream> using namespace std;#include <string> class person { public : person (string name,int age) { m_name = name; m_age = age; } int m_age; string m_name; bool operator ==(person& p) { if (m_age == p.m_age && m_name == p.m_name) { return true ; } else return false ; } bool operator !=(person& p) { if (m_age != p.m_age|| m_name != p.m_name) { return true ; } else return false ; } }; void test () { person p1 ("张三" ,30 ) ; person p2 ("李四" ,40 ) ; person p3 ("王五" ,50 ) ; if (p1 == p2) { cout << "相等" << endl; } else cout << "不相等" << endl; if (p1 != p3) { cout << "不相等" << endl; } else cout << "相等" << endl; } int main () { test (); 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 #include <iostream> using namespace std;#include <string> class MyPrint { public : void operator () (string test) { cout << test << endl; } }; void test2 (string test) { cout << test << endl; } void test () { MyPrint m1; m1 ("123" ); test2 ("456" ); } class MyAdd { public : int operator () (int a, int b) { int c = a + b; return c; } }; void test3 () { MyAdd A1; cout << A1 (10 , 20 ) << endl; cout <<MyAdd () (10 , 20 ) << endl; } int main () { test3 (); 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 #include <iostream> using namespace std;class java { public : void header () { cout << "首页、公开课、登录、注册...(公共头部)" << endl; } void foorter () { cout << "帮助中心、交流合作、站内地图....(公共底部)" << endl; } void laft () { cout << "c++、java、python....(公共分类列表)" << endl; } void content () { cout << "Java的学科视频" << endl; } }; class Python { public : void header () { cout << "首页、公开课、登录、注册...(公共头部)" << endl; } void foorter () { cout << "帮助中心、交流合作、站内地图....(公共底部)" << endl; } void laft () { cout << "c++、java、python....(公共分类列表)" << endl; } void content () { cout << "Python的学科视频" << endl; } }; class basepage { public : void header () { cout << "首页、公开课、登录、注册...(公共头部)" << endl; } void foorter () { cout << "帮助中心、交流合作、站内地图....(公共底部)" << endl; } void laft () { cout << "c++、java、python....(公共分类列表)" << endl; } }; class java1 :public basepage{ public : void content () { cout << "Java的学科视频" << endl; } }; class python1 :public basepage{ public : void content () { cout << "python的学科视频" << endl; } }; void test () { cout << "Java的下载视频页面如下:" << endl; java1 ja; ja.header (); ja.laft (); ja.content (); ja.foorter (); cout << endl; } void test1 () { cout << "Python的下载视频页面如下:" << endl; python1 py; py.header (); py.laft (); py.content (); py.foorter (); } int main () { test (); test1 (); system ("pause" ); return 0 ; }
总结:
继承的好处:可以减少重复的代码
class A : public B;
A 类称为子类 或 派生类
B 类称为父类 或 基类
派生类中的成员,包含两大部分 :
一类是从基类继承过来的,一类是自己增加的成员。
从基类继承过过来的表现其共性,而新增的成员体现了其个性。
继承方式 继承的语法:class 子类 : 继承方式 父类
继承方式一共有三种:
示例:
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 73 74 75 76 77 78 79 80 81 82 83 84 #include <iostream> using namespace std;class baes1 { public : int m_A; protected : int m_B; private : int m_C; }; class son1 :public baes1{ public : void func () { m_A = 10 ; m_B = 20 ; } }; class son2 :protected baes1{ public : void func () { m_A = 10 ; m_B = 20 ; } }; class son3 :private baes1{ void func () { m_A = 10 ; m_B = 20 ; } }; class grandson :public son3 { void func () { } }; void test () { son1 s1; s1.m_A = 100 ; son2 s2; son3 s3; } int main () { test (); 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 #include <iostream> using namespace std;class baes { public : int m_A; protected : int m_B; private : int m_C; }; class son :public baes{ public : int m_D; }; void test () { cout << sizeof (son) << endl; } int main () { test (); system ("pause" ); return 0 ; }
利用工具查看
在开始菜单里可以找到VS的开发人员命令提示符
打开之后先跳cpp所在的转盘符 如 C:
cd 粘贴cpp所在地址 (cd是跳转
输入dir
cl /d1 reportSingleClassLayout想查看的类名 cpp的部分名 加TAB键补全cpp
之后就可以得到:
结论: 父类中私有成员也是被子类继承下去了,只是由编译器给隐藏后访问不到
继承中构造和析构顺序 子类继承父类后,当创建子类对象,也会调用父类的构造函数
问题:父类和子类的构造和析构顺序是谁先谁后?
示例:
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 #include <iostream> using namespace std;class baes { public : baes () { cout << "父类的构造函数" << endl; } ~baes () { cout << "父类的析构函数" << endl; } }; class son :public baes{ public : son () { cout << "子类的构造函数" << endl; } ~son () { cout << "子类的析构函数" << endl; } }; void test () { son s1; } int main () { test (); 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 #include <iostream> using namespace std;#include <string> class baes { public : string m_name="张三" ; int phone=188133 ; void abc () { cout << "我是练习时长两年半的个人练习生蔡徐坤" << endl; } void abc (int a) { a = 100 ; cout << "我是练习时长两年半的个人练习生坤坤" << endl; } }; class son :public baes{ public : string m_name="李四" ; int phone=123456 ; void abc () { cout << "卧槽,是鸡哥!" << endl; } }; void test () { son s1; cout<<s1.baes::m_name<<endl; cout<<s1.baes::phone<<endl; cout<<s1.m_name<<endl; cout<<s1.phone<<endl; s1.baes::abc (); s1.abc (); s1.abc (); s1.baes::abc (1 ); } int main () { test (); 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 #include <iostream> using namespace std;#include <string> class baes { public : static string a; static void b () { cout << "这是父类静态函数" << endl; } static void b (int a) { cout << "这是父类重载静态函数" << endl; } }; class son :public baes{ public : static string a; static void b () { cout << "这是子类静态函数" << endl; } }; string baes::a = "这是父类静态变量" ; string son::a = "这是子类静态变量" ; void test () { cout << "通过对象访问" << endl; son s1; s1.b (); s1.baes::b (); s1.baes::b (2 ); cout << s1.a <<endl; cout << s1.baes::a <<endl; cout << "通过类名访问" << endl; son::b (); son::baes::b (); son::baes::b (20 ); cout << son::a << endl; cout << son::baes::a << endl; } int main () { test (); system ("pause" ); return 0 ; }
总结:同名静态成员处理方式和非静态处理方式一样,只不过有两种访问的方式(通过对象 和 通过类名)
多继承语法 C++允许一个类继承多个类
语法: class 子类 :继承方式 父类1 , 继承方式 父类2...
多继承可能会引发父类中有同名成员出现,需要加作用域区分
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 #include <iostream> using namespace std;#include <string> class baes1 { public : int A = 100 ; }; class baes2 { public : int A = 200 ; int B = 200 ; }; class son :public baes1,public baes2{ public : int C = 300 ; int D = 300 ; }; void test () { son s1; cout << s1.B << endl; cout << s1.baes1::A << endl; } int main () { test (); 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 #include <iostream> using namespace std;#include <string> class animal { public : int age; }; class sheep :virtual public animal{ }; class tuo :virtual public animal{ }; class sheeptuo :public sheep, public tuo{ }; void test () { sheeptuo st; st.sheep::age = 18 ; st.tuo::age = 28 ; cout << st.age << endl; } int main () { test (); system ("pause" ); return 0 ; }
总结:
菱形继承带来的主要问题是子类继承两份相同的数据,导致资源浪费以及毫无意义
利用虚继承可以解决菱形继承问题
子类再继承的时候也是传的虚基类指针,无论多少成员都是通过指针找到的都是一开始继承的对象的唯一值
多态 多态的基本概念 多态是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 #include <iostream> using namespace std;class animal { public : virtual void speak () { cout << "动物在说话" << endl; } }; class cat :public animal { public : void speak () { cout << "小猫在说话" << endl; } }; class dog :public animal{ public : void speak () { cout << "小狗在说话" << endl; } }; void dospeak (animal Animal) { Animal.speak (); } void test () { dog d1; cat c1; dospeak (d1); dospeak (c1); } int main () { test (); system ("pause" ); return 0 ; }
在父类定义了一个虚函数的时候,会生成一个虚函数(表)指针和虚函数表
指针在类内,内存为4,表不在类内,需要指针指向
虚函数表在记录了指向该类中所有虚函数的指针
继承的时候,子类也会继承虚函数表和虚函数(表)指针
但是在子类中重写虚函数的时候,子类中虚函数表中原本父类的虚函数的指针就会被覆盖
但是父类中的虚函数表不变
当父类的指针或者引用指向子类对象的时候,会通过虚函数(表)指针来确定被传入类中的虚函数表
如果被传入类中的虚函数表没有重写,就会通过里面原本记录的父类的虚函数的指针找到虚函数并且执行
如果被传入类中的虚函数表已经重写,就会通过里面子类的虚函数的指针找到虚函数并且执行
以父类的指针或者引用进行传入以本人的理解就是
1.以父类的虚函数作为公共接口,即使子类中虚函数是私有的也可以通过父类的虚函数进行访问执行(已检验可实现)
2.如果以值传递的方式,那么只是传递了一个副本,编译器会认为是父类的对象,不会进行多态,直接执行父类的函数
3.子类继承父类,子类中必然有虚函数,调用虚函数需要通过虚函数表指针,父子类的虚函数表指针指向的虚函数表不同,但指针名相同?当传入的是子类的时候,会以子类的虚函数表指针为准,找到执行子类虚函数(指针名是否相同还没有在网上找到答案,但是我通过开发人员命令提示符中是相同的)
纯虚函数和抽象类 在多态中,通常父类中虚函数的实现是毫无意义的,主要都是调用子类重写的内容
因此可以将虚函数改为纯虚函数
纯虚函数语法:virtual 返回值类型 函数名 (参数列表)= 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 #include <iostream> using namespace std;class animal { public : virtual void speak () = 0 ; }; class sheep :public animal{ virtual void speak () { cout << "12456" << endl; }; }; void test () { sheep a1; } void test2 (animal& a) { a.speak (); } int main () { sheep s1; test2 (s1); system ("pause" ); return 0 ; }
虚析构和纯虚析构 多态使用时,如果子类中有属性开辟到堆区,那么父类指针在释放时无法调用到子类的析构代码
解决方式:将父类中的析构函数改为虚析构 或者纯虚析构
虚析构和纯虚析构共性:
可以解决父类指针释放子类对象
都需要有具体的函数实现
虚析构和纯虚析构区别:
如果是纯虚析构,该类属于抽象类,无法实例化对象(类内声明,类外实现)
虚析构语法:
纯虚析构语法:
1 2 virtual ~类名() = 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 70 71 72 73 74 75 76 77 78 79 80 #include <iostream> using namespace std;#include <string> class animal { public : animal () { cout<< "父类的构造函数调用" << endl; } virtual void speak () = 0 ; virtual ~animal () = 0 ; }; animal::~animal () { cout << "父类的析构函数调用" << endl; }; class dog :public animal{ public : dog (string name) { cout << "子类的构造函数调用" << endl; m_name = new string (name); } void speak () { cout <<*m_name<< "小狗在说话" << endl; } string* m_name; ~dog () { if (m_name != NULL ) { cout << "子类的析构函数调用" << endl; delete m_name; m_name = NULL ; } } }; void dospeak () { animal* animal1 = new dog ("卧槽" ); animal1->speak (); delete animal1; } int main () { dospeak (); system ("pause" ); return 0 ; }
总结:
虚析构或纯虚析构就是用来解决通过父类指针释放子类对象
如果子类中没有堆区数据,可以不写为虚析构或纯虚析构
拥有纯虚析构函数的类也属于抽象类
如果子类在堆区中不销毁,那么因为子类而出现的父类也不会销毁
案例 多态有3个案例-在案例练习区
计算器
制作饮品
组装电脑
文件操作 程序运行时产生的数据都属于临时数据,程序一旦运行结束都会被释放
通过文件可以将数据持久化
C++中对文件操作需要包含头文件 < fstream >
文件类型分为两种:
文本文件 - 文件以文本的ASCII码 形式存储在计算机中
二进制文件 - 文件以文本的二进制 形式存储在计算机中,用户一般不能直接读懂它们
操作文件的三大类:
ofstream:写操作
ifstream: 读操作
fstream : 读写操作
文本文件 写文件 写文件步骤如下:
包含头文件
#include
创建流对象
ofstream ofs;
打开文件
ofs.open(“文件路径”,打开方式);
写数据
ofs << “写入的数据”;
关闭文件
ofs.close();
文件打开方式:
打开方式
解释
ios::in
为读文件而打开文件
ios::out
为写文件而打开文件
ios::ate
初始位置:文件尾
ios::app
追加方式写文件
ios::trunc
如果文件存在先删除,再创建
ios::binary
二进制方式
注意: 文件打开方式可以配合使用,利用|操作符
例如: 用二进制方式写文件 ios::binary | ios:: out
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 #include <iostream> using namespace std;#include <fstream> void test () { ofstream ofs; ofs.open ("test.txt" , ios::out); ofs << "姓名;张三" << endl; ofs << "性别:男" << endl; ofs << "电话:123456789" << endl; ofs.close (); } int main () { test (); system ("pause" ); return 0 ; }#include <iostream> using namespace std;#include <fstream> void test () { ofstream ofs; ofs.open ("test.txt" , ios::out); ofs << "姓名;张三" << endl; ofs << "性别:男" << endl; ofs << "电话:123456789" << endl; ofs.close (); } int main () { test (); system ("pause" ); return 0 ; }
总结:
文件操作必须包含头文件 fstream
读文件可以利用 ofstream ,或者fstream类
打开文件时候需要指定操作文件的路径,以及打开方式
利用<<可以向文件中写数据
操作完毕,要关闭文件
读文件 读文件与写文件步骤相似,但是读取方式相对于比较多
读文件步骤如下:
包含头文件
#include
创建流对象
ifstream ifs;
打开文件并判断文件是否打开成功
ifs.open(“文件路径”,打开方式);
读数据
四种方式读取
关闭文件
ifs.close();
示例:
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 #include <iostream> #include <fstream> using namespace std;#include <string> void test () { ifstream ifs; ifs.open ("test.txt" , ios::in); if (!ifs.is_open ()) { cout << "文件打开失败" << endl; return ; } char c; while ((c = ifs.get ()) != EOF) { cout << c; } ifs.close (); } int main () { test (); system ("pause" ); return 0 ; }
总结:
读文件可以利用 ifstream ,或者fstream类
利用is_open函数可以判断文件是否打开成功
close 关闭文件
二进制文件 以二进制的方式对文件进行读写操作
打开方式要指定为 ios::binary
写文件 二进制方式写文件主要利用流对象调用成员函数write
函数原型 :ostream& write(const char * buffer,int len);
参数解释:字符指针buffer指向内存中一段存储空间。len是读写的字节数
示例:
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 #include <iostream> #include <fstream> using namespace std;#include <string> class person { public : char m_name[64 ]; int m_age; }; void test () { ofstream osf ("person.txt" , ios::out | ios::binary) ; person p1 = { "张三" ,18 }; osf.write ((const char *)&p1, sizeof (person)); osf.close (); } int main () { test (); system ("pause" ); return 0 ; }
总结:
文件输出流对象 可以通过write函数,以二进制方式写数据
读文件 二进制方式读文件主要利用流对象调用成员函数read
函数原型:istream& read(char *buffer,int len);
参数解释:字符指针buffer指向内存中一段存储空间。len是读写的字节数
示例:
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 #include <iostream> #include <fstream> using namespace std;#include <string> class person { public : char m_name[64 ]; int m_age; }; void test () { ifstream isf; isf.open ("person.txt" , ios::in | ios::binary); if (!isf.is_open ()) { cout << "文件打开失败" << endl; return ; } person p1; isf.read ((char *)&p1, sizeof (p1)); cout << p1.m_name << endl; cout << p1.m_age << endl; isf.close (); } int main () { test (); system ("pause" ); return 0 ; }
文件输入流对象 可以通过read函数,以二进制方式读数据
————————————————————————————————
核心篇完结撒花