Employee类C ++输入信息。

时间:2018-04-16 04:33:08

标签: c++ class object

#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方法来存储字段和访问器方法中的值,以返回字段中的值。

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

1 个答案:

答案 0 :(得分:3)

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

  1. 您要将int分配给string
  2. 使用()代替{}
  3. 像这样改变:

    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;
    }
    

    另一件事是,您可以为您的班级重载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];
    }
    
相关问题