03 2021 档案
摘要:#include#include#includeusing namespace std;/* 4.7.6 多态案例3--电脑组装 案例描述: 电脑主要组成部件为 CPU(用于计算),显卡(用于显示),内存条(用于存储)...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.7.5 虚析构和纯虚析构 多态使用时,如果子类中有属性开辟到堆区,那么父类指针在释放时无法调用到子类的析构代码 解决方式:将父类...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.7.4 多态案例2--制作饮品 案例描述: 制作饮品的大致流程为:煮水 - 冲泡 - 倒入杯中 - 加入辅料 ...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.7.3 纯虚函数和抽象类 在多态中,通常父类中虚函数的实现是毫无意义的,主要都是调用子类重写的内容,因此可以将虚函数改为纯虚函数 ...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.7.2 多态案例--计算器类 案例描述: 分别利用普通写法和多态技术,设计实现两个操作数进行运算的计算器类 ...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.7 多态 4.7.1 多态的基本概念 多态分两类: 静态多态:函数重载和运算符重载属于静态多态,复用函数名 ...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.6.8 菱形继承 两个派生类继承同一个基类,又有某个类同时继承者两个派生类,这种继承被称为菱形继承,或者钻石继承 例如:羊继...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.6.7 多继承语法 实际开发中不建议使用多继承 多继承可能引发父类中有同名成员出现,需要加作用域区分 class ...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.6.6 继承同名静态成员处理方式 静态成员和非静态成员出现同名,处理方式一致: 访问子类同名成员,直接访问即可 ...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.6.5 继承同名成员处理方式 访问子类同名成员,直接访问即可 访问父类同名成员,需要加作用域*/class Base{publi...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.6.4 继承中构造和析构顺序 子类继承父类后,当创建子类对象时,也会调用父类的构造函数 结论:先构造父类在构造子类,析构顺序与构...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.6.3 继承中的对象模型*/class Base{public: int a;protected: int b;private: int ...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.6.2 继承方式 公共继承 保护继承 私有继承*/class Base{public: int a;prot...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.6 继承 4.6.1 继承的基本语法 继承优点:减少重复代码 语法: class 子类 :继承方式 父类{} ...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.5.6 函数调用运算符重载 函数调用运算符()也可以重载 由于重载后使用的方式很像函数的调用,故也称为仿函数 仿函...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.5.5 关系运算符重载 可以让连个自定义类型的对象进行对比操作*/class Person{public: string name; ...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.5.4 赋值运算符重载 C++编译器至少给一个类添加4个函数 默认构造函数,无参,函数体为空(4.2.4) ...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.5.3 递增运算符重载 可通过此实现自己的整型数据 分两种:前置、后置 总结: 前置递增返回引用,后置递增返回值*/cla...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.5.2 左移运算符重载 可以输出自定义的数据类型*/class Person{public: int a; int b; /...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.5 运算符重载 对已有运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型 4.5.1 加号运算符重载 实现两个自定义...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.4.3 成员函数做友元*/class Building;class Goodgay{public: Goodgay(); // 构造声明 voi...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.4.2 类做友元*/class Building; // 声明Building类,后续会写,防止Goodgay类中引用时报错class Goodgay{pu...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.4 友元 让一个函数或类可以访问另一个类中的私有成员 关键字friend 三种实现: 全局函...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.3.4 const修饰成员函数 常函数:成员函数后加const(修饰的是this指针,令指针指向的值也不可修改) 常函数...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.3.3 空指针访问成员函数 C++中空指针是可以调用成员函数的 但是注意有没有用到this指针(有坑) 若用到了t...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.3.2 this指针 this指针是隐含每一个非静态成员函数内的一种指针 其不需定义,可直接使用 用途: ...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.3.1 成员变量和成员函数分开存储 C++中,类内的成员变量和成员函数分开存储 只有非静态成员变量才属于类的对象上*/class...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.2 对象的初始化和清理 4.2.8 静态成员 静态成员变量 所有对象共享同一份数据 在编译阶...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.2 对象的初始化和清理 4.2.7 类对象作为类成员 称为对象成员 当其他类对象作为本类成员时,先构造其他类对象,再构造自...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.2 对象的初始化和清理 4.2.6 初始化列表 用于初始化属性*/class Person{public: int a; i...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.2 对象的初始化和清理 4.2.5 深拷贝与浅拷贝 面试经典问题,有坑 浅拷贝:简单的赋值拷贝操作 深拷贝:...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.2 对象的初始化和清理 4.2.4 构造函数调用规则 默认情况下创建一个类,C++编译器至少给该类添加3个函数: ...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.2 对象的初始化和清理 4.2.3 拷贝构造函数调用时机 三种情况下会调用: 使用一个已创建完毕的对象来初始化一个新对象 ...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.2 对象的初始化和清理 4.2.2 构造函数的分类及调用 按参数分:有参构造、无参构造(默认构造) 按类型分:普通构造、拷...
阅读全文
摘要:#include#include#includeusing namespace std;/* 4.2 对象的初始化和清理 4.2.1构造函数和析构函数 构造函数-初始化 类名(){} 可以有参数,可重载;没有返回值,不...
阅读全文
摘要:point.h /*#ifndef POINT_H_INCLUDED#define POINT_H_INCLUDED#endif // POINT_H_INCLUDED*/#pragma once#includeusing namespace std;clas...
阅读全文
摘要:#include#includeusing namespace std;/* 4.1 封装 案例*2*/// -----------------------------------------------------------------...
阅读全文
摘要:#include#includeusing namespace std;/* 4.1 封装 成员属性设置为私有的优点: 可以自己控制读写权限 对于写权限,可以检测数据有效性*/class Pers...
阅读全文
摘要:#includeusing namespace std;/* 4.1 封装 访问权限: 公共 public 其中的成员在类内+类外均可访问 保护 protected ...
阅读全文
摘要:#include#includeusing namespace std;#define PI 3.14/* 4.1 封装 C++面向对象三大特性:封装、继承、多态 封装意义: 设计类时可以将属性和行为作为...
阅读全文
摘要:#includeusing namespace std;/* 三 函数提高 3.3 函数重载 函数名可以相同以提高复用性 函数重载需满足的条件: 同意作用域下 函数名相同 ...
阅读全文
摘要:#includeusing namespace std;/* 三 函数提高 3.2 函数占位参数 当前学习阶段,占位参数传入后,也接收不到;现在用不到,后续课程用到*/void func(int a, int){ // 第二个int即为占位参...
阅读全文
摘要:#includeusing namespace std;/* 三 函数提高 3.1 函数默认参数*/// 注意,若某个位置已有了默认参数,则从该参数位置往后的每个参数都必须有默认值,例func(int a, int b, int c=30, d=4...
阅读全文
摘要:#includeusing namespace std;/* 二 引用 2.6 常量引用 主要用作修饰形参,防止误操作 函数形参列表中,可以加const修饰形参,防止形参改变实参*/void show_value(int...
阅读全文
摘要:#includeusing namespace std;/* 二 引用 2.5 引用的本质 在C++内部实现了一个指针常量*/void func(int & re){ // 自动转换为:int * const re = &a; ...
阅读全文
摘要:#includeusing namespace std;/* 二 引用 2.4 引用做函数返回值 注意:不要返回局部变量引用 用法:函数调用作为左值*/int & test_1(){ // 引用方式返回 int a...
阅读全文
摘要:#includeusing namespace std;/* 二 引用 2.3 引用做函数参数 作用:函数传参时,可以利用引用让形参修饰实参 优点:可以简化指针修改实参(在此方面可以代替指针)*/void swap_1(...
阅读全文
摘要:#includeusing namespace std;/* 二 引用 2.1&2.2 引用的语法和注意事项 作用:给变量取别名 语法:数据类型 &别名 = 原名 注意:引用必须初始化,且初始化后不可更改*...
阅读全文
摘要:#includeusing namespace std;/* 1.3 new操作符 利用new在堆区创建数据时,会返回该数据对应的指针 new出来的堆区数据,手动释放用delete*/int * func(){ int * ...
阅读全文
摘要:#includeusing namespace std;/* 1.2程序运行后 栈区 存放函数的参数、局部变量 注意:不要返回局部变量的地址,栈区开辟的数据由编译器自动释放 堆区 ...
阅读全文
摘要:#includeusing namespace std;/* 一 内存分区模型 C++程序在执行时,将内存分为4个区域 1 代码区 存放函数体的二进制代码,由OS管理 2 全局区 ...
阅读全文
摘要:#include#include#includeusing namespace std;#define MAX 1000struct Person{ string name; int sex; // 1男2女 int age; stri...
阅读全文
摘要:#include#includeusing namespace std;/* 结构体-案例#2 三国英雄:5名英雄,通过冒泡排序按照年龄升序排列*/struct Hero{ string name; int age; string...
阅读全文
摘要:#include#include#include //随机数用到#include //时间用到using namespace std;/* 结构体-案例#1 三个老师,每个老师带五个学生做毕设*/struct student{ strin...
阅读全文
摘要:#include#includeusing namespace std;/* 结构体中const的使用场景 用const来防止误操作*/struct student{ string name; int age; int score...
阅读全文
摘要:#include#includeusing namespace std;/* 结构体做函数参数 值传递&地址传递*/struct student{ string name; int age; int score;};voi...
阅读全文
摘要:#include#includeusing namespace std;/* 结构体嵌套结构体*/struct student{ string name; int age; int score;};struct teacher{ ...
阅读全文
摘要:#include#includeusing namespace std;/* 结构体指针 可以通过操作符'->'访问结构体中的成员(属性)*/struct Student{ string name; int age; int sc...
阅读全文
摘要:#include#includeusing namespace std;/* 结构体数组 将自定义的结构体放入数组中 方便维护*/struct Student{ string name; int age; int score;};...
阅读全文
摘要:#include#includeusing namespace std;/* 结构体 一些数据类型的集合 用户自定义的数据类型 允许存放不同的数据类型*/// 结构体定义struct Student{ string name; ...
阅读全文
摘要:#includeusing namespace std;int main(){ //3 跳转语句 //3-2 continue //循环语句中,跳过本次循环中余下未执行的语句,直接执行下一次循环 //区别:break会直接退出循环而不再...
阅读全文
摘要:#includeusing namespace std;int main(){ //2 循环结构 //2-4 嵌套循环 for (int i=0; i 列数> select; switch (select){ case 1: ...
阅读全文
摘要:#includeusing namespace std;int main(){ //2 循环结构 //2-3 for(起始表达式0;条件表达式1;末尾循环体2){循环语句3} //起始表达式仅执行1次;执行顺序:0 123 123 123 ....
阅读全文
摘要:#includeusing namespace std;int main(){ //2 循环结构 //2-2 do-while int num = 0; do{ cout << num << endl; nu...
阅读全文
摘要:#include#include#include //随机数用到#include //时间用到using namespace std;int main(){ //2 循环结构 //2-1 while int num = 0; whi...
阅读全文
摘要:#include#includeusing namespace std;int main(){ //程序流程结构:顺序结构 选择结构 循环结构 //1 选择结构 //1-1单行if语句 if(){} //1-2多行if语句 if(){}...
阅读全文
摘要:#include#includeusing namespace std;int main(){ /* 二维数组的数组名 查看二维数组所占内存空间 获取二维数组首地址 */ // 1 int ar...
阅读全文
摘要:#includeusing namespace std;int main(){ /* 算法--冒泡排序(实现升序) 比较相邻元素,若第一个>第二个,则交换 对每一组相邻元素执行上句操作,完成后,找到第一个最大值 ...
阅读全文
摘要:#includeusing namespace std;int main(){ /* 数组特点: 放在一块连续内存空间中 数组中每个元素的数据类型相同 下标从0开始索引 */ //数组定...
阅读全文
摘要:#includeusing namespace std;/* 函数的声明,作用是提前告诉编译器函数的存在 函数声明可以有多次(但一般写一次就行),函数定义只能有一次*///函数声明int get_max_1(int a, int b);//函数定义...
阅读全文
摘要:#includeusing namespace std;/* 常见函数样式: 无参无返 有参无返 无参有返 无参无返*/// 1void test1(){ cout << "this is test1" << endl;}/...
阅读全文
摘要:#includeusing namespace std;/* 函数定义5个要点: 返回值类型 函数名 参数列表 函数体语句 return表达式 返回值类型 函数名(参数列表){ 函数体语句 ...
阅读全文
摘要:#includeusing namespace std;/* 综合案例:指针+数组+函数 封装一个函数,利用冒泡排序,实现对整型数组的升序排列*/void bubble_sort(int * arr, int len){ // 形参指针arr接收数...
阅读全文
摘要:#includeusing namespace std;void swap1(int a, int b){ int temp = a; a = b; b = temp; cout << "swap1 a=" << a << endl; ...
阅读全文
摘要:#includeusing namespace std;int main(){ /* 指针的定义和使用 */ int a = 10; //定义指针:数据类型 * 指针变量名 int * p; //让指针记录变量...
阅读全文
摘要:#includeusing namespace std;int main(){ /* 指针和数组 */ int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; cout << "数组第...
阅读全文