
C++笔记
磁生电
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
vs2019快捷键创建代码片段:代码片段管理Code snippets
最近在学黑马的C++里面提到一句代码片段管理感觉很高效,但是摸索了半天不会设置,老出错。原创 2023-05-09 10:43:37 · 504 阅读 · 1 评论 -
265 STL演讲比赛流程案例
#include"speechManager.h"#include<string>#include<algorithm>#include<deque>#include<functional>#include<numeric>#include<fstream> //构造函数SpeechManager::SpeechManager(){ //在构造函数中调用初始化函数 this->initSpeech();原创 2021-10-04 19:44:37 · 145 阅读 · 0 评论 -
263 交集 并集 差集
#include<iostream>using namespace std;#include<vector>#include<algorithm>//取交集set_intersection//void myPrint(int val){ cout << val << " ";}void test01(){ vector<int>v1; vector<int>v2; for (int i = 0原创 2021-09-30 17:43:41 · 109 阅读 · 0 评论 -
236 STL案例-员工分组
#include<iostream>using namespace std;#include<string>#include<map>#include<vector>#define CEHUA 0#define MEISHU 1#define YANFA 2//创建员工类class Woker{public: string m_name; int m_salary;};//创建员工,并把员工放入vector容器中vo原创 2021-09-26 18:01:40 · 150 阅读 · 0 评论 -
210评委打分案例STL
#include<iostream>using namespace std;#include<string>#include<vector>#include<algorithm>#include<deque>#include<ctime>//评委打分案例:五名选手,十名评委打分,去除最高分最低分,求平均分//创建一个类class Person{public: Person(string name, int s原创 2021-09-25 15:04:56 · 97 阅读 · 0 评论 -
164 职工管理系统代码问题
#pragma once//防止头文件重复包含#include<iostream>//包含输入输出流头文件using namespace std;//使用标准命名空间,使用cout cin#include"worker.h"#include"employee.h"#include"manager.h"#include"boss.h"#include<fstream>#define FILENAME "empFile.txt"class WManager//在头.原创 2021-04-07 19:08:02 · 258 阅读 · 0 评论 -
152 职工管理系统02
#pragma once//防止头文件重复包含#include<iostream>//引入标准io流#include<string>//输出字符串流using namespace std;/*创建职工抽象类:由于职工分为普通员工,经理,老板,可以将三个职工抽象到一个(worker)类中,利用多态对不同职工进行管理(父类中写纯虚函数,子类中继承写具体实现,分别创建头文件和源文件继承父类)职工的属性有:职工编号,姓名,部门编号职工的写为:岗位职责信息描述,获取岗位名称原创 2021-04-05 14:25:39 · 107 阅读 · 0 评论 -
147 职工管理系统01
主程序:#include<iostream>using namespace std;#include"worker manager.h"/*基于多态职工管理系统:公司职工分为:普通员工 、经理、老板、要显示职工编号 岗位 姓名 以及职责信息。普通员工:完成经理交代的问题经理:完成老板交代的问题传达给员工老板:管理公司的经理管理系统需要实现的功能如下:1、退出管理程序:退出当前管理系统2、增加职工信息:实现批量添加职工功能,将信息录入到文件中3、显示职工信息4、删除职原创 2021-03-31 15:50:31 · 123 阅读 · 0 评论 -
145 写文件
#include<iostream>using namespace std;#include<fstream>//二进制文件//二进制方式写文件主要用流对象调用成员写函数write//函数语法:ostream &write(const char* buffer,int len);//创建一个输出流对象ostream ,const char*来写入地址,int len代表长度//自负指针指向内存一段存储空间,len是读写的字节数。////class pers原创 2021-03-31 10:21:12 · 144 阅读 · 0 评论 -
144 读文件
#include<iostream>using namespace std;#include<fstream>#include<string>//文本文件 读文件:////////void test01(){ //1、包含头文件 //2、创建流对象 ifstream ifs; //3、打开文件,并判断是否打开成功 ifs.open("test.txt", ios::in); if (!ifs.is_open()) { cout原创 2021-03-27 17:20:47 · 276 阅读 · 0 评论 -
143 文本文件
#include<iostream>using namespace std;#include<fstream>//文件操作的头文件//1、文件操作://程序运行时产生的数据都是临时的,持续运行结束就会被释放,通过文件可以将数据持久化//对头文件操作需要加头文件<fstream>//2、文件类型//*1文本文件:文件以文本ASCII码存储在计算机里//*2二进制文件:文本以二进制形式存储在计算机里//3、操作文件三大类:ofstrem写文件 ifst.原创 2021-03-26 17:32:21 · 169 阅读 · 0 评论 -
142 多态案例3 电脑组装
#include<iostream>using namespace std;//多态案例3:电脑组装//电脑主要组成部件:CPU(用于计算),显卡(用于显示),内存条(用于存储)//将每个部件封装出抽象基类,部件下有不同的生产厂商,Intel lenovo//创建电脑类,组装电脑工作函数和调用零件接口//测试时创建3态电脑///**************************************************************************/原创 2021-03-26 16:05:32 · 161 阅读 · 0 评论 -
140 虚析构和·纯虚析构
#include<iostream>using namespace std;#include<string>//虚析构和纯虚构://1、在多态使用的时候,父类的的指针或引用会指向子类对象,是无法释放子类的析构代码,如果有子类对象属性开辟到了堆区,//指针在delete的时候走不到子类中的析构函数,那么堆区的数据会造成内存的泄露。//为了解决这个问题就要用到虚析构和纯虚析构//2、虚析构和纯虚构共性://父类指针无法释放子类对象 都需要具体的函数实现//3、虚原创 2021-03-26 11:28:55 · 112 阅读 · 0 评论 -
139 多态案例二 饮品制作
#include<iostream>using namespace std;//多态案例二:饮品制作:煮水 泡水 //////////class AbstractDr{public: //煮水 virtual void Boil() = 0; //冲泡 virtual void Brew() = 0; //倒入杯中 virtual void PourCup() = 0; //加辅料 virtual void AddSth() = 0; //制作饮品: v原创 2021-03-23 15:31:26 · 131 阅读 · 0 评论 -
138 纯虚函数和抽象函数
#include<iostream>using namespace std;//在前面的两个例子中,父类虚函数基本没有用到,所以可以写成纯虚函数//纯虚函数语法: virtual 返回值类型 函数名(参数列表)=0;//当类中有了抽象类,这种类也称抽象类//抽象类特点:1.无法实例化对象 2.子函数必须重写抽象类中的纯虚函数,不然子类是抽象类无法实例化对象class base{public: virtual void func() = 0;};class son :原创 2021-03-23 14:57:41 · 139 阅读 · 0 评论 -
137 多态案例 计算器
#include<iostream>using namespace std;#include<string>//分别用普通写法和多态技术,实现两个数运算的计算器//普通写法class Calculator{public: int Getrul(string oper) { if (oper == "+") { return num1 + num2; } else if (oper == "-") { return num1 - num原创 2021-03-23 10:16:33 · 99 阅读 · 0 评论 -
135 多态
#include<iostream>using namespace std;//多态 是C++面向对象三大特性之一//多态分为//静态多态:函数重载和运算符重载属于静态多态,复用函数名//动态多态:派生类和虚函数实现运行时多态//静态多态和动态多态的区别//静态多态的函数地址早绑定--编译阶段确定函数地址//动态多态的函数地址晚绑定--运行阶段确定函数地址//class Animal//动物类{public: virtual void Speak()//虚函数,在实现原创 2021-03-19 14:47:22 · 78 阅读 · 0 评论 -
134 菱形继承
在这里插入代码片原创 2021-03-19 12:26:04 · 97 阅读 · 0 评论 -
133 多继承语法
#include<iostream>using namespace std;//C++允许一个类继承多个类//多类继承语法:class 子类名:继承方式 父亲1,继承方式 父亲2,继承方式 父亲3...//多继承会引起父类中有同名成员,调用时要加作用于区分//class base1{public: base1() { m_A = 100; } int m_A;};class base2{public: base2() { m_A = 130;原创 2021-03-19 11:47:12 · 165 阅读 · 0 评论 -
132 同名静态成员访问方式
#include<iostream>using namespace std;//问题:静态的同名成员,在子类中应该如何访问?//静态成员和非静态成员同名的处理方式一致:访问子类同名成员,直接俄访问。访问父类同名成员,要加作用域//静态的成员变量特点:静态成员属性:所有对象都共享同一份数据,编译阶段就分配内存,类内声明类外初始化。//静态成员函数:只能访问静态成员变量,不能访问非静态成员变量,只有一分,所有的对象共享同一份静态成员函数////class base{public原创 2021-03-19 09:53:39 · 164 阅读 · 0 评论 -
131 同名成员访问方式
#include<iostream>using namespace std;//问题:当子类和父类中有同名成员,该怎么分别调用不同类下的同名成员//访问子类同名成员 直接访问即可//访问fueling同名成员 要加作用域class base{public: base()//构造函数中赋初值 { m_A = 10; } void func() { cout << "base的成员函数调用!!" << endl; } void func(原创 2021-03-18 15:18:06 · 111 阅读 · 0 评论 -
130 继承中构造和析构函数的顺序
#include<iostream>using namespace std;//子类继承父类后,当创建子类对象,也会调用父类的构造函数//问题:父类和子类的构造和析构顺序谁先谁后class base{public: base() { cout << "base的构造函数" << endl; } ~base() { cout << "base的析构函数!" << endl; }};class son :pub原创 2021-03-18 14:51:40 · 139 阅读 · 0 评论 -
2021-03-18
#includeusing namespace std;//继承中的对象模型//class base{public:int m_A;protected:int m_B;private:int m_C;};class son :public base{public:int m_D;};void test01(){cout << "sizeof(son)= " << sizeof(son) << endl;//父类所有非静态成员属性都.原创 2021-03-18 14:33:10 · 91 阅读 · 0 评论 -
128 继承方式
#include<iostream>using namespace std;//继承方式////class base1//公共继承类{public: int m_A;protected: int m_B;private: int m_C;};class son1:public base1{public: void func() { m_A = 19;//父类中的公共权限继承到子类依然是公共权限 m_B = 10;//父类中的保护权限继承到子类依.原创 2021-03-18 11:04:38 · 80 阅读 · 0 评论 -
127 继承
#include<iostream>using namespace std;//继承:减少重复代码//语法:class 子类名:继承方式 父类名//子类:派生类 父类:基类class BasePage//写公共的页面类{public: void header() { cout << "公共头部:首页、公共课、登录、注册" << endl; } void footer() { cout << "公共底部:帮助中心、站内地图、合原创 2021-03-17 15:29:41 · 130 阅读 · 0 评论 -
126 调用运算符重载
#include<iostream>using namespace std;#include<string>//函数调用运算符:()//重载后的使用方式非常像函数调用,因此也叫仿函数//仿函数没有固定写法,非常灵活//class MyPrint{public: //重载函数调用运算符 void operator()(string test) { cout << test << endl; }};void MyPrin原创 2021-03-17 14:53:01 · 89 阅读 · 0 评论 -
125 关系运算符重载
#include<iostream>using namespace std;#include<string>//关系运算符重载:可以比较新的定义对象//////class person{public: person(string name,int age)//构造函数给属性赋初值 { m_age = age; m_name = name; } //重载==号 bool operator==(person &p) { if (thi原创 2021-03-17 10:58:41 · 142 阅读 · 0 评论 -
124 赋值运算符重载
#include<iostream>using namespace std;//编译器提供的默认函数:operator=对属性进行只拷贝即等号的赋值操作,只要有赋值操作就会用这个函数,只要是只拷贝都会引发深浅拷贝问题//如果有数据创建在堆区,就会引起堆区数据重复释放即浅拷贝问题,要用深拷贝去解决浅拷贝//////class person{public: person(int age)//有参构造函数 { m_age=new int(age);//New在堆区开辟一个地原创 2021-03-16 16:19:48 · 108 阅读 · 1 评论 -
123(02)递减运算符重载
#include<iostream>using namespace std;//递增运算符重载//int a=10;cout<<a++<<endl;->10 ;cout<<a<<endl; ->11;//int b=10;cout<<++b<<endl;->11;cout<<b<<endl;->11;//class MyInteger{ friend ost原创 2021-03-16 15:11:33 · 100 阅读 · 0 评论 -
123 递增运动算符重载
#include<iostream>using namespace std;//递增运算符重载//int a=10;cout<<a++<<endl;->10 ;cout<<a<<endl; ->11;//int b=10;cout<<++b<<endl;->11;cout<<b<<endl;->11;//class MyInteger{ friend ost原创 2021-03-16 15:01:29 · 107 阅读 · 0 评论 -
122 左移运算符重载
#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: //利用成员函数 重载左移运算符 p.op原创 2021-03-15 17:54:37 · 128 阅读 · 0 评论 -
121 运算重载
#include<iostream>using namespace std;//运算符重载//对已有运算符进行重新定义,赋予另一种更年期,来适应不同的数据类型//1.通过成员函数重载+://class Person//{//public:// int m_A;// int m_B;////};//Person p1;//p1.m_A = 10;//p1.m_B = 12;//Person p2;//p2.m_A = 10;//p2.m_B = 12;//Per原创 2021-03-15 13:58:38 · 66 阅读 · 0 评论 -
120 类的成员函数做友元
#include<iostream>using namespace std;#include<string>//成员函数作友元////// ///// //class Building;class GoodGay{public: GoodGay();//构造函数 void visit();//创建一个visit函数可以防问building中的私有成员 void visit2();//visit2函数不可以防问building中的私有成员 Building原创 2021-03-14 21:22:56 · 80 阅读 · 0 评论 -
119 类做友元
#include<iostream>using namespace std;#include<string>//类做友元:类可以防问类里面的私有属性//////class Building;//class GoodGay{public: GoodGay();public: void visit();//参观函数访问Building中的属性 Building* building;};//class Building{ friend class原创 2021-03-13 21:16:09 · 75 阅读 · 0 评论 -
118 友元
#include<iostream>using namespace std;#include<string>class building{ friend void goodGay(building* build);//表示goodGag是building的好朋友可以防问building中的私有属性public: building() { m_sittingR = "客厅"; m_badR = "卧室"; }public: string m_sitting原创 2021-03-11 20:21:11 · 77 阅读 · 0 评论 -
117 const修饰成员函数
#include<iostream>using namespace std;//常函数//1.成员函数加const后,称这个函数为常函数//2.常函数内不可以修改成员属性//3.成员属性声明时加关键字mutable后在在常函数中依然可以修改class person{public: void showPerson()const { //m_A = 100;//在成员函数没有const修饰时,成员函数有默认的this指针修饰, //this指针是常量指针,指向不可以修改但原创 2021-03-11 19:49:05 · 119 阅读 · 0 评论 -
116 控制还真只想成员属性
#include<iostream>using namespace std;//空指针指向成员函数//////class person{public: void ShowCname() { cout << "this is person class" << endl; } void ShowAge() { if (this == NULL) { return; } //报错的原因时传入的指针为空,没有创建任何实质的对象是原创 2021-03-11 19:12:42 · 85 阅读 · 0 评论 -
115 this指针
#include<iostream>using namespace std;//this指针用途//1.区分同名变量和参数//2.返回对象本身用*this////class person{public: person(int age)//构造函数 { this->age = age;//用指针this区分同名成员变量与传递参数 , this指针指向被调成员函数所属对象 } int age; person& aadAge(person &p)原创 2021-03-11 17:10:29 · 86 阅读 · 0 评论 -
114 成员对象和成员函数分开存储
#include<iostream>using namespace std;//成员变量和成员函数分开存储//只有非静态成员变量属于类上,静态成员变量和静态成员函数函数不属于类的对象,非静态成员函数也不属于类对象它也只有一份与非静态成员函数是分开存的,//////class person{ int m_A;//非静态成员变量属于类对象上 static int m_B;//静态成员变量:类内声明类外初始化 不属于类的对象上 void func() {}//非静态成员函原创 2021-03-11 16:12:37 · 93 阅读 · 0 评论 -
113 静态成员函数与变量
#include<iostream>using namespace std;//静态成员函数//所有对象共享一个函数//静态成员函数只能防问静态成员变量class person{public: static void func()//静态成员函数 { m_A = 101;//静态成员函数可以防问静态成员变量 //m_B = 120;//静态成员函数不能防问非静态成员变量 cout << "static void func调用" << endl原创 2021-03-10 21:28:02 · 78 阅读 · 0 评论