我正在尝试制作工资单系统层次结构,我的所有基类都是抽象类,继承的所有其他类都是具体类(可以实例化的类)..我的基类中有一个纯虚函数 Earning()抽象基类员工.. 派生类中提供了正确的收入功能,具体取决于正在计算其收入的员工类型。我在抽象基类中还有一个虚函数,它显示在所有派生类中都被覆盖的信息。类层次结构是这样的
- 班级雇员;
- 类 CommissionEmployee :公职人员;
- 类受薪雇员:公职人员;
- 类 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();
}
}