C++练习20:类的使用

这是一个C++编程练习,涉及创建一个雇员类,包含ID、工资率和最大工作小时数属性,并实现输入、修改和显示雇员数据的功能。程序还包含一个菜单,允许用户选择添加、修改、删除雇员或退出。用户可以交互地输入多个雇员的信息并进行操作。
摘要由CSDN通过智能技术生成

C++练习20:类的使用

题目

a. 构造一个雇员class。每个员工由一个整数ID号、双精度工资率和员工每周应工作的最大小时数。该类提供的服务有,为新雇员输入数据的功能,为新雇员更改数据的功能,以及为新雇员显示现有数据的功能。

b. 要求为三个雇员输入数据,然后显示输入的数据。

c. 修改程序,使其包含一个为用户提供以下选择的菜单:

  1. 添加员工
  2. 修改员工数据
  3. 删除员工
  4. 退出菜单

程序应该启动一个操作来实现用户的选择。

代码

#include <iostream>
#include <vector>
using namespace std;
 
class Employee
{
	private: 
		int employeeID;
		int maxHours;
		double payRate;
	
	public:
		Employee(int = 0, int = 0, double = 1);  // 默认值设置 
		void setID(int id);
		void setMaxHours(int h);
		void setPayRate(double rate);
		void showData();
};

Employee::Employee(int id, int h, double rate)
{
	employeeID = id;
	maxHours = h;
	payRate = rate;
}

void Employee::setID(int id)
{
	employeeID = id;
}

void Employee::setMaxHours(int h)
{
	maxHours = h;
}

void Employee::setPayRate(double rate)
{
	payRate = rate;
}

void Employee::showData()
{
	cout << "Employee ID: " << employeeID << ", max working hours: " 
	<< maxHours << ", pay rate: " << payRate << endl;
}

// test function
int main()
{
	vector<Employee> emp(0);
	int empLength = 0;
	while(true)
	{
		cout << "1. Add an employee\n"
			 <<	"2. Modify employee data\n"
			 << "3. Delete an employee\n"
			 << "4. Exit this menu \n";
			 
		int input;
		cin >> input;
		
		if(input == 1)
		{
			emp.resize(++empLength);
			int tmp;
			double tmpDouble;
		
			cout << "------------------\n";
			cout << "Input employee ID: ";
			cin >> tmp;
		
			emp[empLength - 1].setID(tmp);
		
			cout << "Input max working hours: ";
			cin >> tmp;
			emp[empLength - 1].setMaxHours(tmp);
	
			cout << "Input payRate: ";
			cin >> tmpDouble;
			emp[empLength - 1].setPayRate(tmpDouble);
		}
		else if(input == 2)
		{
			int ind;
			int tmp;
			double tmpDouble;
		
			cout << "------------------\n";
			cout << "Input the storing index of the employee: ";
			cin >> ind;
		
			if(ind < 0 || ind >= empLength)
			{
				cout << "Invalid index input!";
				exit(1);
			}
		
			cout << "Input new employee ID: ";
			cin >> tmp;
			emp[ind].setID(tmp);
		
			cout << "Input new max working hours: ";
			cin >> tmp;
			emp[ind].setMaxHours(tmp);
	
			cout << "Input new payRate: ";
			cin >> tmpDouble;
			emp[ind].setPayRate(tmpDouble);
		}
		else if(input == 3)
		{
			int ind;
			cout << "------------------\n";
			cout << "Input the storing index of the employee: ";
			cin >> ind;
		
			emp.erase(emp.begin() + ind);
			emp.resize(--empLength);
		}
		else break;
	
		system("CLS");
	}
	return 0;
} 
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

调参侠鱼尾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值