• C++作业2:类与类关系设计


    题目描述

    合理设计类与类的组合关系,即一个类要嵌套另一个类的对象作为成员变量,根据问题的需要可以是同一个类的多个对象(可以使用对象数组),也可以是不同类的多个不同对象。要求要对生成的对象的数据成员进行初始化、修改、获取、输出等基本操作,还应能完成其他一些合理的功能操作。让所设计的功能发挥作用。

    参考设计

    参考1:公司类嵌套员工类对象
    员工类数据成员:姓名、年龄、工号、工资等;
    公司类数据成员:公司名、公司相关信息、公司员工工资支出、公司收入;
    基本操作:对数据成员的初始化、输出显示等操作;
    其他操作:统计公司所有员工工资支出等。

    程序流程图

    在这里插入图片描述

    类视图

    在这里插入图片描述

    参考代码

    //公司类定义: CCompany.h

    #pragma once
    #include "CStaff.h"
    #include <bits/stdc++.h>
    class CCompany
    {
    private:
    	CStaff* m_nStaff[25];
    	string m_nCompanyName;
    	int n;
    public:
    	CCompany();
    	CCompany(CStaff* nStaff, string Name, string Age, string Gender, string ID, string Number, string EnTime);
    	void SetCompanyName(string CompanyName);
    
    	void SetNum(int n);
    	int GetNum();
    
    	void SetStff(CStaff* nStaff, int i);
    	CStaff* GetStaff(int i);
    	void showInfo();
    	~CCompany();
    };
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    //公司类实现:CCompany.cpp

    #include "CCompany.h"
    #include "CStaff.h"
    #include <bits/stdc++.h>
    CCompany::CCompany()
    {
    	this->m_nStaff[0] = nullptr;
    	this-> m_nCompanyName = "";
    	this->n = -1;
    }
    
    CCompany::CCompany(CStaff* nStaff, string Name, string Age, string Gender, string ID, string Number, string EnTime)
    {
    	nStaff->SetName(Name);
    	nStaff->SetAge(Age);
    	nStaff->SetGender(Gender);
    	nStaff->SetID(ID);
    	nStaff->SetNumber(Number);
    	nStaff->SetTime(EnTime);
    }
    
    void CCompany::SetCompanyName(string CompanyName)
    {
    	this->m_nCompanyName = CompanyName;
    }
    
    CStaff* CCompany::GetStaff(int i)
    {
    	return this->m_nStaff[i];
    	return nullptr;
    }
    
    void CCompany::showInfo()
    {
    	cout << "公司信息为:" << endl
    		<< "公司名称:"
    		<< this->m_nCompanyName << endl
    		<< "公司员工数:"
    		<< this->GetNum() << endl;
    }
    
    void CCompany::SetNum(int n)
    {
    	this->n = n;
    }
    
    int CCompany::GetNum()
    {
    	return this->n;
    	return 0;
    }
    
    void CCompany::SetStff(CStaff* nStaff, int i)
    {
    	this->m_nStaff[i] = nStaff;
    }
    
    CCompany::~CCompany()
    {
    }
    
    
    • 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

    //员工类定义: CStaff.h

    #pragma once
    #include <string>
    using namespace std;
    class CStaff
    {
    private:
    	string m_nName;//姓名
    	string m_nAge;//年龄
    	string m_nGender;//性别
    	string m_nID;//身份证
    	string m_nNumber;//工号
    	string m_nEnTime;//入职时间
    	string m_nPosition;//职位
    public:
    	CStaff();
    	CStaff(string Name, string Age, string Gender, string ID, string Number, string EnTime, string Position);
    	void SetAllinformation(string Name, string Age, string Gender, string ID, string Number, string EnTime, string Position);
    	void SetName(string nName);
    	void SetAge(string Age);
    	void SetGender(string Gender);
    	void SetID(string ID);
    	void SetNumber(string Number);
    	void SetTime(string EnTime);
    	void SetPosition(string Position);
    	string ShowInformation();
    	~CStaff();
    };
    
    
    • 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

    //员工类实现:CStaff.cpp

    #include "CStaff.h"
    #include <bits/stdc++.h>
    
    CStaff::CStaff()
    {
    }
    
    
    
    CStaff::CStaff(string Name, string Age, string Gender, string ID, string Number, string EnTime, string Position)
    {
    	this->m_nName = Name;
    	this->m_nAge = Age;
    	this->m_nGender = Gender;
    	this->m_nID = ID;
    	this->m_nNumber = Number;
    	this->m_nEnTime = EnTime;
    	this->m_nPosition = Position;
    }
    
    void CStaff::SetAllinformation(string Name, string Age, string Gender, string ID, string Number, string EnTime, string Position)
    {
    	this->m_nName = Name;
    	this->m_nAge = Age;
    	this->m_nGender = Gender;
    	this->m_nID = ID;
    	this->m_nNumber = Number;
    	this->m_nEnTime = EnTime;
    	this->m_nPosition = Position;
    }
    
    void CStaff::SetName(string nName)
    {
    	this->m_nName = nName;
    }
    
    void CStaff::SetAge(string Age)
    {
    	this->m_nAge = Age;
    }
    
    void CStaff::SetGender(string Gender)
    {
    	this->m_nGender = Gender;
    }
    
    void CStaff::SetID(string ID)
    {
    	this->m_nID = ID;
    }
    
    void CStaff::SetNumber(string Number)
    {
    	this->m_nNumber = Number;
    }
    
    void CStaff::SetTime(string EnTime)
    {
    	this->m_nEnTime = EnTime;
    }
    
    void CStaff::SetPosition(string Position)
    {
    	this->m_nPosition = Position;
    }
    
    string CStaff::ShowInformation()
    {
    	
    	cout << "姓名:" << this->m_nName << endl << "年龄:" << this->m_nAge << endl << "性别:" << this->m_nGender << endl << "身份证号:" << this->m_nID << endl << "工号:" << this->m_nNumber << endl << "入职时间:" << this->m_nEnTime << endl;
    	return string();
    }
    
    CStaff::~CStaff()
    {
    
    }
    
    
    • 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 "CStaff.h"
    #include <bits/stdc++.h>
    #include "CCompany.h"
    //"小明",20,"男", "12345678987654321","512020xxxx","2020-09-01"
    //= new CStaff(nName, nAge, nGender, nID, nNumber, nEnTime, nPosition)
    using namespace std;
    
    
    void SetInfo(CStaff *staff) {
    	string nName;//姓名
    	string nAge;//年龄
    	string nGender;//性别
    	string nID;//身份证
    	string nNumber;//工号
    	string nEnTime;//入职时间
    	string nPosition;//职位
    
    	cout << "请输入姓名: ";			cin >> nName;
    	cout << "请输入年龄: ";			cin >> nAge;
    	cout << "请输入性别: ";			cin >> nGender;
    	cout << "请输入身份证号: ";		cin >> nID;
    	cout << "请输入工号: ";			cin >> nNumber;
    	cout << "请输入入职时间: ";		cin >> nEnTime;
    	cout << "请输入职位:";			cin >> nPosition;
    	staff->SetAllinformation(nName, nAge, nGender, nID, nNumber, nEnTime, nPosition);
    	cout << endl;
    }
    int main()
    {
    	void Menu();
    	void ShowAll(CCompany Future);
    	void ExitSystem(CCompany * Future);
    	void ChangeInformation(CCompany & Future);
    
    	CCompany Future;
    	int choice = 0;
    	while (true)
    	{
    		Menu();
    		cout << "情输入您的选择:" << endl;
    		cin >> choice;//接收用户的选择
    		switch (choice)
    		{
    		case 0:  //退出系统
    			ExitSystem(&Future);
    			break;
    		case 1:
    		{
    			string nCompanyName;
    			int Num;
    			cout << "请输入公司的名字:" << endl;
    			cin >> nCompanyName;
    			Future.SetCompanyName(nCompanyName);
    			cout << "请输入公司中的员工人数:" << endl;
    			cin >> Num;
    			Future.SetNum(Num);
    			system("pause");
    			system("cls");
    			break;
    		}
    		case 2:
    		{
    			if (Future.GetNum() != -1)
    			{
    				cout << "设置员工信息" << endl;
    				for (int i = 0; i < Future.GetNum(); i++)
    				{
    					cout << "请输入第 " << i + 1 << "位员工的信息" << endl;
    					cout << "员工编号为:" << i + 1 << endl;
    					CStaff* staff = new CStaff();
    					SetInfo(staff);
    					Future.SetStff(staff, i);
    				}
    			}
    			else {
    				cout << "Error!" << endl <<  "请先设置公司信息!" << endl;
    			}
    			system("pause");
    			system("cls");
    			break;
    		}
    		case 3:
    		{
    			ChangeInformation(Future);
    			system("pause");
    			system("cls");
    			break;
    		}
    		case 4:
    		{
    			ShowAll(Future);
    			system("pause");
    			system("cls");
    			break;
    		}
    		default:
    			break;
    		}
    	}
    }
    
    
    void ShowI(int i, CCompany Future)
    {
    	cout << "该员工信息:" << endl;
    	Future.GetStaff(i)->ShowInformation();
    	cout << endl;
    }
    
    void ShowAll(CCompany Future)
    {
    	if (Future.GetNum() == -1) {
    		cout << "Error!" << endl << "未设置公司信息!" << endl;
    	}
    	else {
    		Future.showInfo();
    		cout << endl;
    		if (Future.GetNum() == 0 || Future.GetStaff(0) == nullptr) {
    			cout << "没有员工信息!" << endl;
    		}
    		else {
    			cout << "员工信息:" << endl;
    			for (int i = 0; i < Future.GetNum(); i++)\
    			{
    				cout << "第 " << i + 1 << "位员工的信息为:" << endl;
    				Future.GetStaff(i)->ShowInformation();
    				cout << endl;
    			}
    		}
    	}
    }
    void Menu() {
    	cout << "————————————————————————" << endl;
    	cout << "——————  欢迎使用员工管理系统!——————" << endl;
    	cout << "———————— 0.退出管理程序 ————————" << endl;
    	cout << "———————— 1.设置公司信息 ————————" << endl;
    	cout << "———————— 2.设置员工信息 ————————" << endl;
    	cout << "———————— 3.修改员工信息 ————————" << endl;
    	cout << "———————— 4.显示所有信息 ————————" << endl;
    	cout << "————————————————————————" << endl;
    	cout << endl;
    }
    
    void ExitSystem(CCompany* Future)
    {
    	for (int i = 0; i < Future->GetNum(); i++)
    	{
    		if (Future->GetStaff(i) != nullptr)
    		{
    			delete Future->GetStaff(i);
    		}
    	}
    	cout << "欢迎下次使用!" << endl;
    	system("pause");
    	exit(0);
    }
    
    void ChangeInformation(CCompany& Future)
    {
    	if (Future.GetNum() == -1) {
    		cout << "Error!" << endl << "未设置公司信息!" << endl;
    	}
    	else if (Future.GetNum() == 0 || Future.GetStaff(0) == nullptr) {
    		cout << "没有员工信息!" << endl;
    	}
    	else {
    		int select;
    		cout << "请输入员工编号" << endl;
    		cin >> select;
    		string nName;//姓名
    		string nAge;//年龄
    		string nGender;//性别
    		string nID;//身份证
    		string nNumber;//工号
    		string nEnTime;//入职时间
    		string nPosition;//职位
    		cout << "修改:" << endl << "1.姓名" << endl << "2.年龄" << endl << "3.性别" << endl << "4.身份证" << endl << "5.工号" << endl << "6.入职时间" << endl << "7.职位" << endl;
    		int t;
    		cin >> t;
    		cout << "请输入:";
    		switch (t) {
    		case 1:cin >> nName; Future.GetStaff(select - 1)->SetName(nName); break;
    		case 2:cin >> nAge; Future.GetStaff(select - 1)->SetAge(nAge); break;
    		case 3:cin >> nGender; Future.GetStaff(select - 1)->SetGender(nGender); break;
    		case 4:cin >> nID; Future.GetStaff(select - 1)->SetID(nID); break;
    		case 5:cin >> nNumber; Future.GetStaff(select - 1)->SetNumber(nNumber); break;
    		case 6:cin >> nEnTime; Future.GetStaff(select - 1)->SetTime(nEnTime); break;
    		case 7:cin >> nPosition; Future.GetStaff(select - 1)->SetPosition(nPosition); break;
    		default:printf("error\n"); break;
    		}
    	}
    }
    
    
    • 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
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193

    运行结果

    执行

    在这里插入图片描述

    显示信息

    显示信息

    修改

    在这里插入图片描述

    说明

    个人能力有限,仅供参考,共同学习!

  • 相关阅读:
    构建强大监控系统:使用Linux Zabbix和cpolar实现远程访问
    一文带你了解”数据分箱“技术
    Flutter 08 三棵树(Widgets、Elements和RenderObjects)
    分布式搜索———黑马旅游
    小程序源码:纯头像微信小程序源码下载,多分类头像自动采集
    基于springboot+vue开发的教师工作量管理系
    网络安全-典型的恶意代码
    AI 时代的企业级安全合规策略
    RestEasy入门小程序
    Turtlebot2简单控制
  • 原文地址:https://blog.csdn.net/qq_29711355/article/details/125456255