C++ 动态多态性在c+中没有正确实现+;
我试图建立一个工资系统层次结构,我的所有基类都是一个抽象类,所有继承的其他类都是具体类(可以实例化的类)。。我的基类抽象基类employee中有一个纯虚函数earning()。。根据正在计算收入的员工类型,派生类中提供了适当的收入功能。我在抽象基类中还有一个虚拟函数,它显示所有派生类中重写的信息。 类层次结构如下所示C++ 动态多态性在c+中没有正确实现+;,c++,polymorphism,C++,Polymorphism,我试图建立一个工资系统层次结构,我的所有基类都是一个抽象类,所有继承的其他类都是具体类(可以实例化的类)。。我的基类抽象基类employee中有一个纯虚函数earning()。。根据正在计算收入的员工类型,派生类中提供了适当的收入功能。我在抽象基类中还有一个虚拟函数,它显示所有派生类中重写的信息。 类层次结构如下所示 班级员工 类别专员员工:公职人员 受薪员工类别:公职人员 basePlusCommissionEmployee类:公共雇员 佣金员工的替代纯虚拟函数收入将员工一周的总销售额与佣金率
#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();
}
}
#包括
#包括
使用名称空间std;
班级员工
{
私人:
字符串名;
字符串socialSecurityNumber;
公众:
void setName(字符串);
字符串getName();
无效设置SN(字符串);
字符串getSSN();
虚拟双收益()=0;
虚拟空显示信息();
};
void employee::setName(字符串argName)
{
name=argName;
}
字符串employee::getName()
{
返回名称;
}
void employee::setSSN(字符串argSSN)
{
社会安全编号=argSSN;
}
字符串employee::getSSN()
{
返回社会安全号码;
}
void employee::displayInformation()
{
你可以有两个不同的物体吗
首先,您有一个由pointerObj
指向的对象,您对该对象所做的唯一事情就是调用它的displayInformation
函数
然后是obj
对象,您可以在其中设置所有值
如果要打印为obj
设置的所有数据,则需要在obj
上调用displayInformation
:
obj.displayInformation();
否则,您应该将指向的对象上的所有信息设置为pointerObj
,并且根本没有变量obj
或者可能的第三种解决方案:
employee *pointerObj = &obj;
请不要发布错误消息的图像,只需将它们复制/粘贴为文本。没有错误。它们是一些逻辑错误。我发布了图像只是为了以防万一,如果我不清楚如何解释问题。有人会看到输出并知道输出结果。你似乎期望指向obj
。你是c融合两个不同的变量pointerObj
和obj
。除了混淆两个不同的对象外,您还通过不再删除pointerObj
产生了内存泄漏(您可以通过使用智能指针来避免这种必要性,例如std::unique\u ptr
)…我建议您尝试对抽象类使用组合
而不是使用继承