动态多态性未在c ++中正确实现

问题描述 投票:0回答:1

我正在尝试建立薪资系统层次结构,而我的所有基础类都是抽象类,而所有继承的其他类都是具体类(可以实例化的类。。)基本类抽象基本类的雇员..根据要计算其收入的雇员的类型,在派生类中提供适当的收入功能。我在抽象基类中还有一个虚函数,它是显示信息,在所有派生类中都将其覆盖。类层次结构是这样的

  1. 班级雇员;
  2. 类别佣金雇员:公共雇员;
  3. class salariedEmployee:公共雇员;
  4. class basePlusCommissionEmployee:公共雇员

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

受薪雇员的超支收入功能仅返回该雇员的工资,因为他/她的工资是唯一的收入。

basePlusCommissionEmployee表示一个雇员,他/她的收入功能中既增加了这两项,又是该雇员的薪水和该雇员从其进行的销售中所赚取的佣金。我面临的问题是,当我使用基类指针指向派生类的对象时。实现了动态多态,但是在每个类中都覆盖了的displayInformation函数中,我没有得到全部信息。它不显示雇员的姓名或他/她的收入。enter image description here我还发布了一张图像,该图像显示了受薪雇员类别的覆盖的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();
    }
}
c++ polymorphism
1个回答
1
投票

您有两个不同的对象...

首先,您有一个由pointerObj指向的对象,并且对该对象唯一要做的就是调用其displayInformation函数。

然后您有了obj对象,可以在其中设置所有值。

如果要打印为obj设置的所有数据,则需要在displayInformation上调用obj

obj.displayInformation();

否则,应将指向该对象的所有信息设置为pointerObj,并且完全没有变量obj

或可能的第三个解决方案:

employee *pointerObj = &obj;
© www.soinside.com 2019 - 2024. All rights reserved.