# c++ **Repository Path**: wei13013451000/cplusplus ## Basic Information - **Project Name**: c++ - **Description**: c++学习 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-09-10 - **Last Updated**: 2024-09-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # c++ #### 介绍 c++学习 #### 参考视频 [黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难](https://www.bilibili.com/video/BV1et411b73Z?p=70&vd_source=f3f44d906db1a0fb2477daf8ce30918a) #### 1.结构体 1. 结构体案例1 ```c++ #include #include #include using namespace std; /** * 1.结构体案例1 * 要求: * 学校正在做毕设项目,每名老师带领5个学生,总共有3名老师,需求如下 设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员 学生的成员有姓名、考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值 最终打印出老师数据以及老师所带的学生数据。 */ struct Student { string name; int age; int score; }; struct Teacher { int id; string name; int age; struct Student sArr[5]; }; void catePace(Teacher tArr[], int len) { string endfix = "ABCDE"; for (int i = 0; i < len; i++) { // 添加教师 tArr[i].name = "Teacher_"; tArr[i].name += endfix[i]; for (int j = 0; j < 5; j++) { tArr[i].sArr[j].name = "Student_"; tArr[i].sArr[j].name += endfix[j]; int random = rand() % 61 + 40; tArr[i].sArr[j].score = random; } } } void printInfo(Teacher tArr[], int len) { for (int i = 0; i < len; i++) { cout << "老师的姓名: " << tArr[i].name << endl; for (int j = 0; j < 5; j++) { cout << "\t学生的姓名: " << tArr[i].sArr[j].name << " 学生的分数: " << tArr[i].sArr[j].score << endl; } } } int main() { // 随机数种子 srand((unsigned int)time(NULL)); struct Teacher tArr[3]; int len = sizeof(tArr) / sizeof(tArr[0]); catePace(tArr, len); printInfo(tArr, len); // 老师的姓名: Teacher_A // 学生的姓名: Student_A 学生的分数: 83 // 学生的姓名: Student_B 学生的分数: 100 // 学生的姓名: Student_C 学生的分数: 70 // 学生的姓名: Student_D 学生的分数: 85 // 学生的姓名: Student_E 学生的分数: 42 // 老师的姓名: Teacher_B // 学生的姓名: Student_A 学生的分数: 40 // 学生的姓名: Student_B 学生的分数: 94 // 学生的姓名: Student_C 学生的分数: 40 // 学生的姓名: Student_D 学生的分数: 48 // 学生的姓名: Student_E 学生的分数: 79 // 老师的姓名: Teacher_C // 学生的姓名: Student_A 学生的分数: 46 // 学生的姓名: Student_B 学生的分数: 40 // 学生的姓名: Student_C 学生的分数: 81 // 学生的姓名: Student_D 学生的分数: 74 // 学生的姓名: Student_E 学生的分数: 78 return 0; } ``` 2. 结构体案例2 ```c++ #include #include #include using namespace std; /** * 结构体案例2 * 要求: * 设计一个英雄的结构体,包括成员姓名,年龄,性别:创建结构体数组,数组中存放5名英雄。 * 通过冒泡排序算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果。 */ struct Hero { string name; int age; string gender; }; void addHero(struct Hero heroArr[], int len) { string endFix = "ABCDE"; for (int i = 0; i < len; i++) { int randAge = rand() % 50 + 20; heroArr[i].name = "Hero_"; heroArr[i].name += endFix[i]; heroArr[i].age = randAge; heroArr[i].gender = "male"; } } void bubbleSort(struct Hero heroArr[], int len) { for (int i = 0; i < len - 1; i++) { for (int j = 0; j < len - i - 1; j++) { if (heroArr[j].age > heroArr[j + 1].age) { struct Hero tmp = heroArr[j]; heroArr[j] = heroArr[j + 1]; heroArr[j + 1] = tmp; } } } } void printInfo(struct Hero heroArr[], int len) { for (int i = 0; i < len; i++) { cout << "英雄名称: " << heroArr[i].name << " 英雄年龄: " << heroArr[i].age << " 英雄性别: " << heroArr[i].gender << endl; } } int main() { // 随机数种子 srand((unsigned int)time(NULL)); // 1.存放5名英雄的数组 struct Hero heroArr[5]; // 计算数组长度 int len = sizeof(heroArr) / sizeof(heroArr[0]); // 2.添加5名英雄到数组 addHero(heroArr, len); // 3.冒泡排序 bubbleSort(heroArr, len); // 4.打印数据 printInfo(heroArr, len); // 英雄名称: Hero_B 英雄年龄: 23 英雄性别: male // 英雄名称: Hero_D 英雄年龄: 35 英雄性别: male // 英雄名称: Hero_E 英雄年龄: 47 英雄性别: male // 英雄名称: Hero_C 英雄年龄: 58 英雄性别: male // 英雄名称: Hero_A 英雄年龄: 67 英雄性别: male return 0; } ``` #### 2.面相对象 ```c++ #include #include using namespace std; // 2.设计一个学生类,属性有姓名,学号,可以给学号和姓名赋值,可以显示学号和姓名 class Student { // 访问权限 public: // 属性 int id; // 学号 string name; // 姓名 // 方法 void setName(string n) { name = n; } void setId(int i) { id = i; } void setData(int i,string n){ id = i; name=n; } void showData() { cout << "学生id:" << id << endl; cout << "学生姓名:" << name << endl; } }; int main() { Student s1; // s1.setName("小明"); // s1.setId(1); s1.setData(11,"李四"); s1.showData(); // 学生id:11 // 学生姓名:李四 return 0; } ``` ```c++ // 面向对象 案例2 点和圆的关系 /** * 点和圆的三种关系 * 1.在圆内 * 2.在圆上 * 3.在圆外 * 要求:给出圆的半径,坐标点(x,y),计算圆与点的关系 * 公式: * ((x1-x2)^2+(y1-y2)^2) == r^2 在圆上 * ((x1-x2)^2+(y1-y2)^2) < r^2 在圆内 * ((x1-x2)^2+(y1-y2)^2) > r^2 在圆外 */ #include #include using namespace std; class Point { private: int p_x; int p_y; public: void setX(int x) { p_x = x; } void setY(int y) { p_y = y; } int getX() { return p_x; } int getY() { return p_y; } }; class Circle { private: int c_r; // 半径 Point c_p; // 圆心 public: void setR(int r) { c_r = r; } int getR() { return c_r; } void setP(Point p) { c_p = p; } Point getP() { return c_p; } }; // 计算位置 void caculateLocation(Point &p, Circle &c) { // 圆心 Point p1 = c.getP(); int x1 = p1.getX(); int y1 = p1.getY(); // 点 int x2 = p.getX(); int y2 = p.getY(); // 半径 int r = c.getR(); int ret = ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); cout << "r*r:" << r * r << endl; cout << "ret:" << ret << endl; if (r * r == ret) { cout << "点" << x2 << "," << y2 << "在圆上" << endl; } else if (r * r < ret) { cout << "点" << x2 << "," << y2 << "在圆外" << endl; } else { cout << "点" << x2 << "," << y2 << "在圆内" << endl; } } int main() { // 点 Point p; p.setX(10); p.setY(10); Point p2; p2.setX(10); p2.setY(9); Point p3; p3.setX(10); p3.setY(11); // 圆 Circle c; // 圆心 Point p4; p4.setX(10); p4.setY(0); c.setP(p4); // 半径 c.setR(10); caculateLocation(p, c); caculateLocation(p2, c); caculateLocation(p3, c); // r*r:100 // ret:100 // 点10,10在圆上 // r*r:100 // ret:81 // 点10,9在圆内 // r*r:100 // ret:121 // 点10,11在圆外 return 0; } ``` ##### 拆分头文件 ![image-20240914225957218](README/image-20240914225957218.png) ![image-20240914230008419](README/image-20240914230008419.png) ![image-20240914230023833](README/image-20240914230023833.png) vscode需要编译命令`g++ 5oop2.cpp Circle.cpp Point.cpp -o 5oop2` #### 3.多态 1. 案例1 ```c++ #include #include using namespace std; class AbstractMakeDrink{ public: virtual void zhushui()=0;//煮水 virtual void jiaru()=0;//加入东西 virtual void daoru()=0;//倒入杯中 void ok(){ zhushui(); jiaru(); daoru(); } }; class Coffee :public AbstractMakeDrink{ virtual void zhushui(){ cout <<"煮矿泉水"<ok(); }; void test(){ //制作咖啡 doWork(new Coffee); //制作茶水 doWork(new Tea); }; int main(){ test(); return 0; }; ``` 2. 案例2 ```c++ #include #include using namespace std; // cpu class AbstructCpu { public: virtual void calculator() = 0; // 计算 }; // 显卡 class AbstructVideoCard { public: virtual void display() = 0; // 显示 }; // 内存条 class AbstructMemory { public: virtual void storage() = 0; // 存储 }; // 电脑 class Computer { private: AbstructCpu *c_cpu; AbstructVideoCard *c_videoCard; AbstructMemory *c_memory; public: Computer(AbstructCpu *cpu, AbstructMemory *memory, AbstructVideoCard *videoCard) { c_cpu = cpu; c_videoCard = videoCard; c_memory = memory; } ~Computer() { cout << "执行Computer的析构函数" << endl; if (c_cpu != NULL) { delete c_cpu; c_cpu = NULL; } if (c_memory != NULL) { delete c_memory; c_memory = NULL; } if (c_videoCard != NULL) { delete c_videoCard; c_videoCard = NULL; } } void work() { c_cpu->calculator(); c_videoCard->display(); c_memory->storage(); } }; // 厂商生产零件 class InterCpu : public AbstructCpu { public: virtual void calculator() { cout << "Inter Cpu 开始计算!" << endl; } }; class InterMomory : public AbstructMemory { public: virtual void storage() { cout << "Inter 内存条 开始存储!" << endl; } }; class LenovoVidoCard : public AbstructVideoCard { public: virtual void display() { cout << "lenovo 显卡 开始显示!" << endl; } }; // 组装电脑工作 void test() { // 生产零件 InterCpu *cpu = new InterCpu; InterMomory *memory = new InterMomory; LenovoVidoCard *videoCard = new LenovoVidoCard; // 组装第一台电脑 Computer *computer1 = new Computer(cpu, memory, videoCard); // 电脑工作 computer1->work(); delete computer1; cout << "--------------------------------" << endl; // 组装第二台电脑 Computer *computer2 = new Computer(new InterCpu, new InterMomory, new LenovoVidoCard); // 电脑工作 computer2->work(); delete computer2; }; int main() { test(); return 0; } ``` #### 4.职工管理系统 职工管理系统可以用来管理公司内所有员工的信息 本教程主要利用C++来实现一个基于多态的职工管理系统 公司中职工分为三类:普通员工、经理、老板,显示信息时,需要显示职工编号、职工姓名、职工岗位、以及职责 普通员工职责:完成经理交给的任务 经理职贵:完成老板交给的任务,并下发任务给员工 老板职责:管理公司所有事务 管理系统中需要实现的功能如下: ·退出管理序:退出当前管理系统 ·增加职工信息:实现批量添加职工功能,将信息录入到文件中,职工信息为:职工编号、姓名、部门编号 ·显示职工信息:显示公司内部所有职工的信息 ·删除离职职工:按照编号删除指定的职工 ·修改职工信息:按照编号修改职工个人信息 ·查找职工信息:按照职工的编号或者职工的姓名进行查找相关的人员信息 ·按照编号排序:按照职工编号,进行排序,排序规则由用户指定 ·清空所有文档:清空文件中记录的所有职工信息(清空前需要再次确认,防止误删) ![image-20240918161539839](README/image-20240918161539839.png)