如何为作为雇员类的指针传入的构造函数输入数据

How to input data for constructor that is passed in as a pointer for Employee Class?

本文关键字:输入 数据 指针 构造函数      更新时间:2023-10-16

我正在尝试传递一个字符串指针,该指针指向我的构造函数的名称以及其他两种数据类型。我不知道如何以不必重载流运算符的方式编写代码,以便可以读取 int main 中的数据。

我已经尝试取消引用,如您所见的 get 方法,但我迷路了。

#include<iostream>
#include<string>
#include<cmath>
using namespace std;
class Employee
{
private:
    string name;
    int emp_ID;
    int emp_GrossSal;
    double emp_Savings;
public:
    Employee()
    {
        string name = "";
    }
    Employee(string *n,int , int )
    {
        name = *n;
        emp_ID = NULL;
        emp_GrossSal = NULL;
        //emp_Savings = (.1*emp_GrossSal);
    }
    //get and set methods
    int getSalary();
    double getSavings();
    int getEmp_ID();
    string getName() const;
    void setEmp_ID(int);
    void setSalary(int);
    void setName(const string &);
    //functions
    void displayEmp();
    ~Employee();
    //overloaded operators
    void operator =(const Employee &right)
    {
            name = right.name;
            emp_GrossSal = right.emp_GrossSal;
            emp_ID = right.emp_ID;
            emp_Savings = right.emp_Savings;
    }
};
int Employee::getEmp_ID()
{
    return emp_ID;
}
int Employee::getSalary()
{
    return emp_GrossSal;
}
double Employee::getSavings()
{
    return (emp_GrossSal*(1/100));
}
string Employee::getName() const
{
    return name;
}
void Employee::setEmp_ID(int t)
{
    emp_ID = t;
}
void Employee::setSalary(int s)
{
    emp_GrossSal = s;
}
void Employee::setName( const string &n)
{
    name = n;
}
void Employee::displayEmp()
{
    cout << "Employee Name:" << name << endl;
    cout << "Employee ID  :" << emp_ID << endl;
    cout << "Employee Gross Salary: " << emp_GrossSal << endl;
    cout << "Employee Savings: " << emp_Savings << endl;
    cout << "Employee Monthy Salary: $ " << emp_GrossSal / 12 << endl;

}
Employee::~Employee()
{
}
int main()
{
    Employee One;
    string inputName;
    int ID, Gross_Sal, choice;
    cout << "Enter the number of Employees: " << endl;
    cin >> choice;

    cout << "ttPlease enter the Employee's Information Belownn" << endl;
    cout << "Enter the employees Name: " << endl;
    cin >> inputName;
    One.setName(inputName);
    cout << "Enter the employees ID:     " << endl;
    cin >> ID;
    One.setEmp_ID(ID);
    cout << "Employee Gross Salary:        " << endl;
    cin >> Gross_Sal;
    One.setSalary(Gross_Sal);

    One.displayEmp();
    system("Pause");
    return 0;
}   

我正在尝试输出员工信息,如姓名等。只是尝试先管理名称部分。

If you're trying to set the employee's name, set the name to n, not the other way around.
    Employee(string *n,int , int )
    {
        *n = name; (This should be name = *n;) I believe.
        emp_ID = NULL;
        emp_GrossSal = NULL;
        //emp_Savings = (.1*emp_GrossSal);
    }

至于设置员工姓名,我会做一个 setter 函数,但照原样,您也可以让 getter 返回一个引用,然后这应该可以工作:

 Employee One;
    char name;
    cout << "ttPlease enter the Employee's Information Belownn" << 
        endl;
    cout << "Enter the employees Name: ";
    cin >> One.getName();  (*(One.getName()) if using pointer)
    One.displayEmp();

编辑:

void Employee::setName(const string& name_)
{
    name = name_;
}

Employee One;
char name;
cout << "ttPlease enter the Employee's Information Belownn" << endl;
cout << "Enter the employees Name: ";
string inputName;
cin >> inputName;
One.setName(inputName);
One.displayEmp();

编辑#2:

//NOTE: Your issue here is the way your set salary and display functions work
    class Employee
    {
    private:
        string name;
        int emp_ID;
        int emp_GrossSal;
        //NOTE: This is never set
        double emp_Savings;
    public:
    double Employee::getSavings()
    {
        //NOTE: This is never called
        return (emp_GrossSal*(1/100));
    }
    void Employee::setSalary(int s)
    {
        //NOTE: Only the emp_GrossSal is set, emp_savings isn't
        emp_GrossSal = s;
    }
    void Employee::displayEmp()
    {
        cout << "Employee Gross Salary: " << emp_GrossSal << endl;
        //NOTE: When you print out savings, you use emp_Saving which was never set.
        cout << "Employee Savings: " << emp_Savings << endl;
    }

您有两种解决方案,要么需要设置节省,如下所示:

void Employee::setSalary(int s)
{
    emp_GrossSal = s;
    emp_Savings = (s * 0.1);
}

或者你可以完全摆脱emp_Savings变量,因为它没有在任何地方使用,只需调用你的get储蓄函数而不是使用成员变量,如下所示:

void Employee::displayEmp()
{
    cout << "Employee Gross Salary: " << emp_GrossSal << endl;
    //NOTE: This function will actually just get the salary divided by 10
    //NOTE: You want to keep this a function in case you change percentage in the future
    cout << "Employee Savings: " << getSavings() << endl;
}