合理设计类与类的组合关系,即一个类要嵌套另一个类的对象作为成员变量,根据问题的需要可以是同一个类的多个对象(可以使用对象数组),也可以是不同类的多个不同对象。要求要对生成的对象的数据成员进行初始化、修改、获取、输出等基本操作,还应能完成其他一些合理的功能操作。让所设计的功能发挥作用。
参考1:公司类嵌套员工类对象
员工类数据成员:姓名、年龄、工号、工资等;
公司类数据成员:公司名、公司相关信息、公司员工工资支出、公司收入;
基本操作:对数据成员的初始化、输出显示等操作;
其他操作:统计公司所有员工工资支出等。


#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();
};
#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()
{
}
#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();
};
#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()
{
}
#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;
}
}
}



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