C++編程起步項目

員工信息管理系統

需求

Employee.h

#pragma once#include<iostream>
#include<string>using namespace std;class Employee {
public:int id; // 編號string name; // 姓名string position; // 崗位int deptId; // 部門編號Employee();Employee(int id, string name, string position, int deptId);~Employee();virtual string responsibilities() const { return "員工職責!"; };void setName(string name);
};class PuTong : public Employee {
public:PuTong(int id, string name, string position, int deptId);virtual string responsibilities() const;
};
class JingLi : public Employee {
public:JingLi(int id, string name, string position, int deptId);virtual string responsibilities() const;
};
class LaoBan : public Employee {
public:LaoBan(int id, string name, string position, int deptId);virtual string responsibilities() const;
};

Employee.cpp

#include"Employee.h"void Employee::setName(string name) {this->name = name;
}Employee::Employee() :id(0), name(""), position(""), deptId(0) {
}Employee::Employee(int id, string name, string position, int deptId) {this->id = id;this->name = name;this->position = position;this->deptId = deptId;
}Employee::~Employee() {}PuTong::PuTong(int id, string name, string position, int deptId) {this->id = id;this->name = name;this->position = position;this->deptId = deptId;
}
string PuTong::responsibilities() const {return "完成經理交給的任務.";
}JingLi::JingLi(int id, string name, string position, int deptId) {this->id = id;this->name = name;this->position = position;this->deptId = deptId;
}
string JingLi::responsibilities() const {return "完成老板交給的任務,并下發任務給員工.";
}LaoBan::LaoBan(int id, string name, string position, int deptId) {this->id = id;this->name = name;this->position = position;this->deptId = deptId;
}
string LaoBan::responsibilities() const {return "管理公司所有事務.";
}

EmployeeManager.cpp

#pragma once#include<iostream>
#include<string>
#include<fstream>#include "outer.h"using namespace std;static const int MAX_EMP_COUNT = 10;
//Employee emp[MAX_EMP_COUNT]; // 數組 類似結構體數組
Employee* emp = new Employee[MAX_EMP_COUNT]; // 動態分配的數組
Employee** p_emp = new Employee* [MAX_EMP_COUNT]; // Employee指針數組 Employee為抽象類時可以實現多態
//auto emp = make_unique<Employee[]>(MAX_EMP_COUNT);
static int emp_size = 0; // 員工數 限制emp訪問區間
static const string DATA = "data.dat";static void add_emp(Employee& e) {if (emp_size == MAX_EMP_COUNT) { // 擴容cout << "已經存滿了!" << endl;}else {emp[emp_size] = e;p_emp[emp_size] = &e;emp_size++;}
}
static void print_emp(const Employee& e) {cout << "員工[" << e.id << "] ->"<< " 姓名: " << e.name<< " 崗位: " << e.position<< " 部門編號: " << e.deptId<< " 職責:" << e.responsibilities()<< endl;
}
static void print_emp(const Employee* const e) {cout << "員工[" << e->id << "] ->"<< " 姓名: " << e->name<< " 崗位: " << e->position<< " 部門編號: " << e->deptId<< " 職責:" << e->responsibilities()<< endl;
}
static void show_all_emp() {if (emp_size <= 0) {return;}cout << "所有員工信息如下:" << endl;for (int i = 0; i < emp_size; i++) {Employee e = emp[i];print_emp(&e);}
}
static Employee& search_emp(int id) {for (int i = 0; i < emp_size; i++) {if (emp[i].id == id) {return emp[i];}}cout << "沒有找到" << endl;exit(-1);
}
static Employee& search_emp(string name) {for (int i = 0; i < emp_size; i++) {if (emp[i].name == name) {return emp[i];}}cout << "沒有找到" << endl;exit(-1);
}
static bool del_emp(int id) {for (int i = 0; i < emp_size; i++) {if (emp[i].id == id) {while (i < emp_size - 1) {emp[i] = emp[i + 1];i++;}emp_size--;return true;}}return false;
}
static void modify_emp(int id, string name) {Employee& e = search_emp(id);e.name = name;
}static void add() {int id; // 編號string name; // 姓名string position; // 崗位int deptId; // 部門編號while (true) {cout << "輸入-1隨時退出!" << endl;cout << "請輸入編號:";cin >> id;if (id == -1) {break;}//Employee &x = search_emp(id);cout << "請輸入姓名:";cin >> name;if (name == "-1") {break;}pos: cout << "請輸入崗位[1:普通 2:經理 3:老板]:";cin >> position;if (position == "-1") {break;}Employee* e;if (position == "1") {e = new PuTong(id, name, position, 0);}else if (position == "2") {e = new JingLi(id, name, position, 0);}else if (position == "3") {e = new LaoBan(id, name, position, 0);}else {cout << "無效輸入!" << endl;goto pos;}cout << "請輸入部門編號:";cin >> deptId;if (deptId == -1) {break;}//Employee* e = new Employee(id, name, position, deptId);//Employee e(id, name, position, deptId);e->deptId = deptId; add_emp(*e);show_all_emp();cout << "選擇繼續添加還是退出:0 表示退出,1 表示繼續添加. ";int select;cin >> select;if (!select) {break;}}
}
static void del() {cout << "選擇要刪除的員工的編號:";int id;cin >> id;bool d = del_emp(id);if (d) {cout << "刪除成功!" << endl;}else {cout << "刪除失敗!" << endl;}show_all_emp();
}
static void modify() {cout << "選擇要修改的員工的編號:";int id;cin >> id;Employee &e = search_emp(id);cout << "修改姓名:";string name;cin >> name;modify_emp(id, name);show_all_emp();
}
static void search() {cout << "輸入1按編號查找,輸入2按姓名查找.";int select;cin >> select;if (select == 1) {int id;cout << "輸入編號:";cin >> id;Employee &e = search_emp(id);cout << "查找到的員工信息如下:" << endl;print_emp(&e);} else if(select == 2) {string name;cout << "輸入姓名:";cin >> name;Employee& e = search_emp(name);cout << "查找到的員工信息如下:" << endl;print_emp(&e);}
}
static int comp(const void *e1, const void *e2) {const Employee s1 = *static_cast<const Employee*>(e1);const Employee s2 = *static_cast<const Employee*>(e2);return s1.id - s2.id;//return ((Employee*)e1)->id - ((Employee*)e2)->id;
}
// 泛型比較方法,比較id
template<typename T>
static int comp_id(const void* e1, const void* e2) {const T t1 = *static_cast<const T*>(e1);const T t2 = *static_cast<const T*>(e2);return t1.id - t2.id;
}
static void sort() {qsort(emp, emp_size, sizeof(Employee), comp_id<Employee>);
}static void init() {fstream in(DATA, ios::in);Employee* e = new Employee;while (in.read((char*)e, sizeof(Employee))) {emp[emp_size++] = *e;}//p_emp = new Employee* [emp_size];for (int i = 0; i < emp_size; i++) {p_emp[i] = &emp[i];}//int i = 0;//while (getline(in)) {//	int id, deptId;//	string name, position;//	in >> id >> name >> position >> deptId;//	Employee e(id, name, position, deptId);//	emp[i] = e;//}
}
static void store_file() {cout << "文件同步..." << endl;fstream out(DATA, ios::out);for (int i = 0; i < emp_size; i++) {Employee e = emp[i];out.write((const char*)&e, sizeof(Employee));//out << e.id << " "//	<< e.name << " "//	<< e.position << " "//	<< e.deptId//	<< endl;}
}
static void clear_file(const string name) {fstream out(DATA, ios::out|ios::trunc);out.close();
}
static void clear() {cout << "操作不可恢復!確定清空數據嗎?" << endl;cout << "輸入數字0取消!" << endl;int n;cin >> n;if (!n) {return;}emp_size = 0;delete[] emp; // 刪除動態分配的數組emp = new Employee[MAX_EMP_COUNT];//emp = NULL;//emp = make_unique<Employee[]>(MAX_EMP_COUNT);clear_file(DATA);
}static void menu() {cout << "================================" << endl;cout << "0、退出系統" << endl;cout << "1、增加" << endl;cout << "2、顯示所有員工信息" << endl;cout << "3、刪除" << endl;cout << "4、修改員工姓名" << endl;cout << "5、查找" << endl;cout << "6、按編號排序" << endl;cout << "7、清空" << endl;cout << "================================" << endl;
}void start() {init();int flag = 1;int cls = 0;
label: while (flag) {show_all_emp();menu();cout << "輸入要執行的操作 : ";int select;cin >> select;switch(select) {case 0:flag = 0;goto label;case 1:add();store_file();break;case 2:show_all_emp();break;case 3:del();store_file();break;case 4:modify();store_file();break;case 5:search();break;case 6:sort();store_file();system("cls");goto label;case 7:clear();break;default:cout << "無效選項" << endl;}system("pause");system("cls");}cout << "退出系統,歡迎使用!" << endl;
}

outer.h

#pragma once#include "Employee.h"// 員工信息管理系統
void start();// 測試多態
void test_polymorphism();

測試多態

Worker.h

#pragma once#include<string>using namespace std;class Worker {
public:int id; // 編號string name; // 姓名string position; // 崗位//Worker();//Worker(int id, string name, string position);virtual string showInfo() const = 0;
};class Worker1 : public Worker {
public:Worker1(int id, string name, string position);virtual string showInfo() const;
};class Worker2 : public Worker {
public:Worker2(int id, string name, string position);virtual string showInfo() const;
};

Worker.cpp

#include<string>#include"Worker.h"using namespace std;Worker1::Worker1(int id, string name, string position) {this->id = id;this->name = name;this->position = position;
}
string Worker1::showInfo() const {return "完成經理交給的任務.";
}Worker2::Worker2(int id, string name, string position) {this->id = id;this->name = name;this->position = position;
}
string Worker2::showInfo() const {return "完成老板交給的任務,并下發任務給員工.";
}

WorkerManager.cpp

#include<iostream>
#include<string>#include"Worker.h"using namespace std;static int init_size = 1;
static Worker** worker_arry = new Worker*[init_size];
static int worker_size = 0;static void add(Worker* w) {if (worker_size == init_size) {Worker** w_arr = new Worker * [2 * init_size];for (int i = 0; i < worker_size; i++) {w_arr[i] = worker_arry[i];}delete[] worker_arry;worker_arry = w_arr;}worker_arry[worker_size++] = w;
}
static void add() {cout << "分別輸入id、name、position[1:普通、2:經理]." << endl;Worker* w = NULL;int id;cout << "輸入id: ";cin >> id;string name;cout << "輸入name: ";cin >> name;string position;
pos: cout << "輸入position: ";cin >> position;if (position == "1") {w = new Worker1(id, name, position);}else if (position == "2") {w = new Worker2(id, name, position);}else {cout << "position輸入有誤!" << endl;goto pos;}add(w);
}
static void print_worker(const Worker* const w) {cout << "id: " << w->id<< " name: " << w->name<< " position: " << w->position<< " 職責: " << w->showInfo()<< endl;
}
static void print_worker_array() {for (int i = 0; i < worker_size; i++) {Worker* w = worker_arry[i];print_worker(w);}
}void test_polymorphism() {bool flag = true;while (flag) {int s;cout << "輸入0退出.  " << "輸入1添加." << endl;cin >> s;switch (s){case 0:system("pause");exit(1);//flag = false;//break;case 1:add();print_worker_array();default:break;}}
}

測試類

int main(int argc, char *argv[]) 
{// 面向對象設計start();test_polymorphism();return 1;
}

下一步:C++標準庫

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/80614.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/80614.shtml
英文地址,請注明出處:http://en.pswp.cn/web/80614.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Linux的MySQL頭文件和找不到頭文件問題解決

頭文件 #include <iostream> #include <mysql_driver.h> #include <mysql_connection.h> #include <cppconn/statement.h> #include <cppconn/resultset.h> #include <cppconn/prepared_statement.h> #include <cppconn/exception.h&g…

[ linux-系統 ] 命令行參數 | 環境變量

命令行參數 命令行參數是指用戶在啟動程序時通過命令行傳遞給程序的參數。這些參數可以用于控制程序的行為、傳遞輸入數據或配置選項。 在 C/C 中&#xff0c;命令行參數通過 main 函數的參數傳遞 命令行參數列表 argc:參數的個數 argv[]&#xff1a;參數的清單 為什么要…

新書速覽|鴻蒙HarmonyOS NEXT開發之路 卷2:從入門到應用篇

《鴻蒙HarmonyOS NEXT開發之路 卷2&#xff1a;從入門到應用篇》 01 本書內容 《鴻蒙HarmonyOS NEXT開發之路 卷2&#xff1a;從入門到應用篇》是一本深度聚焦HarmonyOS NEXT應用開發的全方位指導書&#xff0c;內容遵循由淺入深的原則展開。全書分為基礎知識、應用開發進階和…

經典密碼學和現代密碼學的結構及其主要區別(1)凱撒密碼——附py代碼

密碼學是一門通過使用代碼和密碼來保護信息的藝術與科學&#xff0c;其歷史可以追溯到數千年前。古典密碼學代表了這一古老學科早期的篇章。早在計算機和現代加密算法出現之前&#xff0c;歷史上的各個文明就依靠巧妙的方法來保護機密、安全通信以及獲取戰略優勢。 古典密碼學…

Python60日基礎學習打卡D30

回顧&#xff1a; 導入官方庫的三種手段導入自定義庫/模塊的方式導入庫/模塊的核心邏輯&#xff1a;找到根目錄&#xff08;python解釋器的目錄和終端的目錄不一致&#xff09; # 直接導入 from random import randint print(randint(1, 10)) # 導入自定義庫 import module m…

Linux利用多線程和線程同步實現一個簡單的聊天服務器

1. 概述 本文實現一個基于TCP/IP的簡單多人聊天室程序。它包含一個服務器端和一個客戶端&#xff1a;服務器能夠接收多個客戶端的連接&#xff0c;并將任何一個客戶端發來的消息廣播給所有其他連接的客戶端&#xff1b;客戶端則可以連接到服務器&#xff0c;發送消息并接收來自…

ubuntu系統 | dify+ollama+deepseek搭建本地應用

1、安裝 Ollama 下載并安裝 Ollama (llm) wangqiangwangqiang:~$ curl -fsSL https://ollama.ai/install.sh | bash >>> Installing ollama to /usr/local >>> Downloading Linux amd64 bundle0.3% curl -fsSL https://ollama.ai/install.sh &#xff08;下…

從紙質契約到智能契約:AI如何改寫信任規則與商業效率??——從智能合約到監管科技,一場顛覆傳統商業邏輯的技術革命

一、傳統合同的“低效困境”&#xff1a;耗時、昂貴、風險失控 近年來&#xff0c;全球商業環境加速向數字化轉型&#xff0c;但合同管理卻成為企業效率的“阿喀琉斯之踵”。據國際商會&#xff08;International Chamber of Commerce&#xff09;數據顯示&#xff0c;全球企業…

【機器學習|學習筆記】基于生成對抗網絡的孿生框架(GAN-based Siamese framework,GSF)詳解,附代碼。

【機器學習|學習筆記】基于生成對抗網絡的孿生框架(GAN-based Siamese framework,GSF)詳解,附代碼。 【機器學習|學習筆記】基于生成對抗網絡的孿生框架(GAN-based Siamese framework,GSF)詳解,附代碼。 文章目錄 【機器學習|學習筆記】基于生成對抗網絡的孿生框架(G…

UEFI Spec 學習筆記---33 - Human Interface Infrastructure Overview---33.2.6 Strings

33.2.6 Strings UEFI 環境中的 string 是使用 UCS-2 格式定義&#xff0c;每個字符由 16bit 數據表示。對于用戶界面&#xff0c;strings 也是一種可以安裝到 HIIdatabase 的一種數據。 為了本土化&#xff0c;每個 string 通過一個唯一標識符來識別&#xff0c;而每一個標識…

Stable Diffusion 學習筆記02

模型下載網站&#xff1a; 1&#xff0c;LiblibAI-哩布哩布AI - 中國領先的AI創作平臺 2&#xff0c;Civitai: The Home of Open-Source Generative AI 模型的安裝&#xff1a; 將下載的sd模型放置在sd1.5的文件內即可&#xff0c;重啟客戶端可用。 外掛VAE模型&#xff1a…

并發編程(5)

拋異常時會釋放鎖。 當線程在 synchronized 塊內部拋出異常時&#xff0c;會自動釋放對象鎖。 public class ExceptionUnlockDemo {private static final Object lock new Object();public static void main(String[] args) {Thread t1 new Thread(() -> {synchronized …

貴州某建筑物擋墻自動化監測

1. 項目簡介 某建筑物位于貴州省某縣城區內&#xff0c;靠近縣城主干道&#xff0c;周邊配套學校、醫院、商貿城。建筑物臨近鳳凰湖、芙蓉江等水系&#xff0c;主打“湖景生態宜居”。改建筑物總占地面積&#xff1a;約5.3萬平方米&#xff1b;總建筑面積&#xff1a;約15萬平…

6個月Python學習計劃:從入門到AI實戰(前端開發者進階指南)

作者&#xff1a;一名前端開發者的進階日志 計劃時長&#xff1a;6個月 每日學習時間&#xff1a;2小時 覆蓋方向&#xff1a;Python基礎、爬蟲開發、數據分析、后端開發、人工智能、深度學習 &#x1f4cc; 目錄 學習目標總覽每日時間分配建議第1月&#xff1a;Python基礎與編…

【FAQ】HarmonyOS SDK 閉源開放能力 —Vision Kit (3)

1.問題描述&#xff1a; 通過CardRecognition識別身份證拍照拿到的照片地址&#xff0c;使用該方法獲取不到圖片文件&#xff0c;請問如何解決&#xff1f; 解決方案&#xff1a; //卡證識別實現頁&#xff0c;文件名為CardDemoPage&#xff0c;需被引入至入口頁 import { …

AI全域智能監控系統重構商業清潔管理范式——從被動響應到主動預防的監控效能革命

一、四維立體監控網絡技術架構 1. 人員行為監控 - 融合人臉識別、骨骼追蹤與RFID工牌技術&#xff0c;身份識別準確率99.97% - 支持15米超距夜間紅外監控&#xff08;精度0.01lux&#xff09; 2. 作業過程監控 - UWB厘米級定位技術&#xff08;誤差&#xff1c;0.3米&…

安全強化的Linux

SElinux簡介 SELinux是security-Enhanced Linux的縮寫,意思是安全強化的linux SELinux主要由美國國家安全局(NSA)開發,當初開發的目的是為了避免資源的誤用。傳統的訪問控制在我們開啟權限后,系統進程可以直接訪問 當我們對權限設置不嚴謹時,這種訪問方式就是系統的安全漏洞 在…

機器學習第十六講:K-means → 自動把超市顧客分成不同消費群體

機器學習第十六講&#xff1a;K-means → 自動把超市顧客分成不同消費群體 資料取自《零基礎學機器學習》。 查看總目錄&#xff1a;學習大綱 關于DeepSeek本地部署指南可以看下我之前寫的文章&#xff1a;DeepSeek R1本地與線上滿血版部署&#xff1a;超詳細手把手指南 K-me…

spring中yml配置上下文與tomcat等外部容器不一致問題

結論&#xff1a;外部優先級大于內部 在 application.yml 中配置了&#xff1a; server:port: 8080servlet:context-path: /demo這表示你的 Spring Boot 應用的上下文路徑&#xff08;context-path&#xff09;是 /demo&#xff0c;即訪問你的服務時&#xff0c;URL 必須以 /d…

論文研讀——《AnomalyGPT:使用大型視覺語言模型檢測工業異常》

這篇論文提出了 AnomalyGPT&#xff0c;一個基于大型視覺語言模型的工業異常檢測框架&#xff0c;首次將通用多模態對話能力引入工業視覺場景&#xff0c;通過引入圖像解碼器增強像素級感知&#xff0c;設計 Prompt 學習器實現任務自適應控制&#xff0c;并利用合成異常樣本解決…