【发布时间】:2013-09-27 16:55:38
【问题描述】:
我正在尝试让我的班级执行以下操作...
EmployeeHandler:将 m_employeeCount 初始化为零。AddEmployee:由菜单选项 1 调用。显示“新员工”。提示用户 员工的名字、姓氏和工资率,一次一个。使用 Employee.Setup 添加 m_lstEmployee 的雇员。显示“已添加员工 m_employeeCount”。增量 m_employeeCount。EmployeeSelection:按索引显示员工列表;提示用户输入员工 索引并返回索引。EditEmployee:由菜单选项 2 调用。使用 EmployeeSelection 获取 员工进行编辑。验证索引是否有效,如果无效则显示错误消息。用途 Employee.Output 显示所选员工的当前信息。提示用户 员工的新名字、姓氏和工资率,一次一个。使用 Employee.Setup 来 更改 m_lstEmployee 中的员工信息。显示“** Employee index updated”, 其中 index 是用户选择的索引。LayoffEmployee:由菜单选项 3 调用。使用 EmployeeSelection 获取 员工下岗。使用 Employee.Output 显示所选员工的名字,姓氏 姓名和工资率。使用 Employee.LayOff 解雇员工。显示“员工 index 下岗”,其中 index 为下岗员工的索引。DisplayEmployeeList:由菜单选项 4 调用。显示“EMPLOYEES”。然后使用 Employee.Output 显示每个员工的记录,像这样,“[1] David Johnson, 工资:5.00 美元(现任员工)”和一位前员工的记录是这样的,“[2] David Johnson, PAY: $5.00 (Former EMPLOYEE)”,括号中的数字是 m_lstEmployee 中的员工索引。GetEmployee:返回m_lstEmployee中所选员工记录的地址。GetEmployeeCount:返回 m_employeeCount 中的员工人数。
到目前为止我...
#ifndef _EMPLOYEEHANDLER
#define _EMPLOYEEHANDLER
#include "Employee.h"
class EmployeeHandler
{
public:
EmployeeHandler()
{
m_employeeCount = 0; //undefined?
};
void AddEmployee()
{
string firstName;
string lastName;
float payRate;
cout<<"NEW EMPLOYEE"<<endl;
cout<<"First Name:"<<endl;
cin>>firstName;
cout<<"Last Name:"<<endl;
cin>>lastName;
cout<<"Pay Rate:"<<endl;
cin>>payRate;
Employee.Setup(firstName,lastName,payRate); //Problem here
cout<<"**Employee m_employeeCount added"<<endl;
m_employeeCount+=1; //m_employeeCount undefined?
}
void EditEmployee()
{
int indexEdit;
string newFirst;
string newLast;
float newPay;
cout<<"Which employee would you like to edit"<<endl;
cin>>indexEdit;
EmployeeSelection(indexEdit); //undefined?
Employee.Output(); //
cout<<"Employee new first name:"<<endl;
cin>>newFirst;
cout<<"Employee new last name:"<<endl;
cin>>newLast;
cout<<"Employee new pay rate:"<<endl;
cin>>newPay;
Employee.Setup(newFirst,newLast,newPay); ///
cout<<"** Employee index updated"<<endl;
}
void LayoffEmployee()
{
EmployeeSelection();
Employee.Output(EmployeeSelection); //Problems here
Employee.LayOff(EmployeeSelection);
cout<<"Employee laid off"<<endl;
}
void DisplayEmployeeList()
{
cout<<"EMPLOYEES"<<endl;
for (int i=0; i<50; i++)
cout<<[i]<<Employee.Output(m_1stEmployee)<<endl; //
}
int EmployeeSelection()
{
int indexNumber;
for (int i= 0; i <50; i++)
cout<<[i]m_1stEmployee<<endl; //
cout<<"Which Employee Index would you like to select?"<<endl;
cin>>indexNumber;
for (int i = 0; i <50; i++)
if ([i]=indexNumber) //
return [i]
}
Employee& GetEmployee( int index )
{if (index=; // completely confused here
}
int GetEmployeeCount()
{
return m_employeeCount;
};
private:
Employee m_lstEmployee[50];
int m_employeeCount;
};
#endif
employee.h文件如下...
#ifndef _EMPLOYEE
#define _EMPLOYEE
#include<iostream>
#include<iomanip>
#include <string>
using namespace std;
class Employee
{
public:
void Setup( const string& first, const string& last, float pay );
{
m_firstName = first;
m_lastName = last;
m_payPerHour = pay;
m_activeEmployee = true;
}
string GetName()
{
return m_firstName+""+m_lastName
};
bool GetIsActive()
{
return m_activeEmployee;
};
void LayOff()
{
m_activeEmployee= false;
};
void Output()
cout<<GetName()<<",PAY:$"<<fixed<<setprecision(2)<<m_payPerHour<<endl;
private:
string m_firstName;
string m_lastName;
float m_payPerHour;
bool m_activeEmployee;
};
#endif
过去两天我一直在写这门课,试图找出我做错了什么。这是我第一次尝试用 C++ 编写类。任何和所有的帮助都非常非常感谢。我已经标记了// 有问题的地方。
【问题讨论】:
-
在 LayoffEmployee() 中,您需要一个 Employee 对象来使用它的非静态成员函数。提示使用 EmployeeSelection() 的返回值而不是丢弃它。
-
是
Employee.h交给你的吗?其中有几个错误。如果不先修复这些错误,就很难完善EmployeeHandler.h。 -
不要将分号放在函数的末尾。 (在最后的
}之后)
标签: c++ function class pointers void