0

我正在尝试制作工资单系统层次结构,我的所有基类都是抽象类,继承的所有其他类都是具体类(可以实例化的类)..我的基类中有一个纯虚函数 Earning()抽象基类员工.. 派生类中提供了正确的收入功能,具体取决于正在计算其收入的员工类型。我在抽象基类中还有一个虚函数,它显示在所有派生类中都被覆盖的信息。类层次结构是这样的

  1. 班级雇员;
  2. 类 CommissionEmployee :公职人员;
  3. 类受薪雇员:公职人员;
  4. 类 basePlusCommissionEmployee :公共雇员

佣金员工覆盖的纯虚拟功能收入将员工一周内的总销售额与佣金率相加,应返回两倍。

受薪员工的覆盖收入函数仅返回员工赚取的工资,因为他/她的工资是唯一的收入。

basePlusCommissionEmployee 意味着一个员工在他/她的收入函数中添加了这两个东西,即该员工的薪水和该员工将从他/她所做的销售中获得的佣金.. ia 面临的问题是,当我使用基类指针指向派生类的对象。动态多态性确实得到了实现,但是在每个类中也被覆盖的 displayInformation 函数中,我没有得到全部信息。它不显示名称员工或他/她的收入。在此处输入图像描述 我还发布了一张图片,显示了受薪员工类的覆盖 displayInformation 函数的输出。除了姓名和社会保险号之外,受薪员工类要求的唯一额外输入是该员工的薪水。

#include <iostream>
#include <cstdlib>
using namespace std;
class employee
{
private:
    string name;
    string socialSecurityNumber;
public:
    void setName(string);
    string getName();
    void setSSN(string);
    string getSSN();
    virtual double earnings() = 0;
    virtual void displayInformation();
};
void employee::setName(string argName)
{
    name = argName;
}
string employee::getName()
{
    return name;
}
void employee::setSSN(string argSSN)
{
    socialSecurityNumber = argSSN;
}
string employee::getSSN()
{
    return socialSecurityNumber;
}
void employee::displayInformation()
{
    cout <<"name: " <<getName()
    <<"Social security number: " <<getSSN();
}

class salariedEmployee : public employee
{
private:
    double salary;
public:
    void setSalary(double);
    double getSalary();
    virtual double earnings();
    virtual void displayInformation();
};
void salariedEmployee::setSalary(double argSalary)
{
    salary = argSalary;
}
double salariedEmployee::getSalary()
{
    return salary;
}
double salariedEmployee::earnings()
{
    //we will only return salary because that is the only earning of a salaried employee
    return salary;
}
void salariedEmployee::displayInformation()
{
    employee::displayInformation();
    cout <<"weekly salary: " <<getSalary();
}

class commissionEmployee : public employee
{
private:
    double commissionRate;
    double grossSales;
public:
    void setCommission(double);
    double getCommission();
    void setGrossSales(double);
    double getGrossSales();
    virtual double earnings();
    virtual void displayInformation();
};
void commissionEmployee::setCommission(double argCommission)
{
    commissionRate = argCommission;
}
double commissionEmployee::getCommission()
{
    return commissionRate;
}
void commissionEmployee::setGrossSales(double argSales)
{
    grossSales = argSales;
}
double commissionEmployee::getGrossSales()
{
    return grossSales;
}
double commissionEmployee::earnings()
{
    return (commissionRate * grossSales);
}
void commissionEmployee::displayInformation()
{
    employee::displayInformation();
    cout <<"\ncommission rate: " << getCommission()
    <<"\nweekly sales: " <<getGrossSales();
}

class basePlusCommissionEmployee : public commissionEmployee
{
private:
    double salary;
public:

    void setSalary(double);
    double getSalary();
    virtual double earnings();
    virtual void displayInformation();
};
void basePlusCommissionEmployee::setSalary(double argSalary)  
{
    salary = argSalary;
}
double basePlusCommissionEmployee::getSalary()
{
   return salary;
}
double basePlusCommissionEmployee::earnings()
{
    return (getSalary() + commissionEmployee::earnings());
}
void basePlusCommissionEmployee::displayInformation()
{
    employee::displayInformation();
    commissionEmployee::displayInformation();
    cout <<"salay: " <<salary;
    cout <<endl; 
}

int main()   
{
    int choice;
    double salary;
    string name, SSN;
    cout <<"choose\n1)salaried employee\n2)commission employee\n3)base plus salaried 
    employee\nenter: ";
    cin >> choice;
    cout <<"enter name: ";
    cin >> name;
    cout <<"enter SSN: ";
    cin >>SSN;
    if(choice == 1)
    {
        employee *pointerObj = new salariedEmployee;
        salariedEmployee obj;
        obj.setName(name);
        obj.setSSN(SSN);
        cout <<"enter weekly salary: ";
        cin >> salary;
        obj.setSalary(salary);
        system("cls");
        cout <<"total earning of this employee: " <<pointerObj->earnings();
        cout <<endl;
        pointerObj->displayInformation();
    }
}
4

1 回答 1

1

您有两个截然不同且不同的对象...

首先,您有 指向的对象pointerObj,您对该对象所做的唯一事情就是调用它的displayInformation函数。

然后你有obj对象,这是你设置所有值的地方。

如果你想打印你设置的所有数据,那么obj你需要调用:displayInformationobj

obj.displayInformation();

否则,您应该设置指向对象的所有信息,pointerObj并且根本没有变量obj

或者可能的第三种解决方案:

employee *pointerObj = &obj;
于 2019-11-05T08:50:09.020 回答