输入信息C++员工类别

Employee class C++ inputting the information.

本文关键字:信息 C++ 输入      更新时间:2023-10-16
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class Employee {
private:
string employee_firstname;
string employee_lastname;
string idnumber;
string address;
string phonenumber;
string tenure;
public:
Employee() {
employee_firstname = "";
employee_lastname = "";
idnumber = "";
address = "";
phonenumber = "";
tenure = "0";
}
Employee(string fn, string ln, string id, string ad, string ph, string ten) {
employee_firstname = fn;
employee_lastname = ln;
idnumber = id;
address = ad;
phonenumber = ph;
tenure = ten;

}
string getFirstName();
string getLastName();
string getidnumber();
string getAddress();
string getPhone();
string getTenure();
void setFirstname(string fn) {
employee_firstname = fn;
}
void setLastname(string ln) {
employee_lastname = ln;
}
void setidnumber(string id) {
idnumber = id;
}
void setaddress(string ad) {
address = ad;
}
void setphonenumber(string ph) {
phonenumber = ph;
}
void settenure(string ten) {
tenure = ten;
}
};
string Employee::getFirstName() {
return employee_firstname;
}
string Employee::getLastName() {
return employee_lastname;
}
string Employee::getidnumber() {
return idnumber;
}
string Employee::getAddress() {
return address;
}
string Employee::getPhone()
{
return phonenumber;
}
string Employee::getTenure() {
return tenure;
}
const int employee_num = 3;
int main()
{
Employee num[employee_num] = {
("John", "Smith", 4752, "8971 herlo st", "916-628-8452", 8),
("Cathy", "Guringo", 5826, "538 reed ct", "310-852-6654", 5),
("Kyle", "Ford", 7856, "292 murrietta st", "323-547-7423", 3),
};
for (int i = 0; i < employee_num; i++)
{
cout << num[i].getFirstName() << " ";
cout << num[i].getLastName() << " ";
cout << num[i].getidnumber() << " ";
cout << num[i].getAddress() << " ";
cout << num[i].getPhone() << " ";
cout << num[i].getTenure() << " ";
}
return 0;
}

我会完全诚实。我不明白如何在此处检索和显示员工的信息。我问过教授,他解释的方式对我来说真的没有意义。他真的无法用不同的方式解释它。

我的教授的提示是这样的:

编写包含以下字段的 Employee 类:

  • 名字
  • 员工编号
  • 地址
  • 电话号码
  • 受雇年限

该类应有两个构造函数: - 默认构造函数,将字段设置为空字符串 (") 和 0 表示使用年份 - 接受三个字段作为参数并将它们分配给姓氏、名字和员工 ID 的构造函数。

编写适当的突变器方法来存储字段中的值,并编写访问器方法来返回字段中的值。

在 main 函数中,通过从键盘输入每个对象的字段来创建三个 Employee 对象。

您的初始化存在一些问题:

  1. 您正在将int分配给string
  2. 使用()而不是{}

像这样更改它:

Employee num[employee_num] = {
{"John", "Smith", "4752", "8971 herlo st", "916-628-8452", "8"},
{"Cathy", "Guringo", "5826", "538 reed ct", "310-852-6654", "5"},
{"Kyle", "Ford", "7856", "292 murrietta st", "323-547-7423", "3"}
};

如果你想从用户那里获取这些数据,你可以使用std::getline和你的 setter 函数将 give 字符串分配给你的类成员。

或者你可以重载operator >>以你想要的方式获取用户输入,如下所示:

friend istream& operator>>(istream& is, Employee& emp)
{
std::cout << "Enter employee first name :";
std::getline(is, emp.employee_firstname);
std::cout << "Enter employee last name :";
std::getline(is, emp.employee_lastname);
std::cout << "Enter employee id number :";
std::getline(is, emp.idnumber);
std::cout << "Enter employee address :";
std::getline(is, emp.address);
std::cout << "Enter employee phone number :";
std::getline(is, emp.phonenumber);
std::cout << "Enter employee tenure :";
std::getline(is, emp.tenure);
return is;
}

另一件事是,您可以重载operator <<,以便您的类按照您想要的方式进行打印,如下所示:

friend ostream& operator<<(ostream& os, Employee const & emp)
{
return os << emp.employee_firstname << " " << emp.employee_lastname << " " << emp.idnumber << " "
<< emp.address << " " << emp.phonenumber << " " << emp.tenure << " " << endl;
}

main函数中像这样使用它:

for(int i = 0; i < employee_num; i++)
{
cout << num[i];
}