#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 类:
  • 姓氏
  • 名字
  • 员工 ID
  • 地址
  • 电话号码
  • 工作年限

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

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

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

    最佳答案

    您的初始化有一些问题:

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

  • 像这样改变它:
    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 函数将字符串分配给您的类成员。

    或者您可以重载 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;
    }
    

    另一件事是您可以为您的类(class)重载 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];
    }
    

    关于c++ - 员工类 C++ 输入信息。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49849703/

    10-14 10:42