具体要求如下:
(1)程序涉及到五个类,分别是employee,technician,salesman,manager,salsemanager。这五个类的关系为:employee是顶层父类,technician,salesman,manager是employee的子类,salsemanager是salesman,manager的子类。
(2)employee具有员工号,姓名,收入等属性,有设置姓名,获取姓名,获取员工号,获取收入等函数。
(3)technician的收入计算方法为工作小时数*时薪,另外具有设置工作小时数,时薪的函数,具有工作小时数和时薪的属性。salesman的收入计算方法为销售额*提成比例,另外具有设置销售额,提成比例的函数,具有销售额和提成比例的属性。manager的收入计算方法为固定月薪,另外具有设置固定月薪的函数,具有固定月薪的属性。salsemanager收入计算方法为销售额*提成比例+固定月薪。
(4)要求利用employee类型的指针数组,指向8个子类对象(technician,salesman,manager,salsemanager每个类2个),通过相应方法输出这些对象的姓名(姓名的初始化通过键盘录入),员工号,收入(多态的形式)。
(1)程序涉及到五个类,分别是employee,technician,salesman,manager,salsemanager。这五个类的关系为:employee是顶层父类,technician,salesman,manager是employee的子类,salsemanager是salesman,manager的子类。
(2)employee具有员工号,姓名,收入等属性,有设置姓名,获取姓名,获取员工号,获取收入等函数。
(3)technician的收入计算方法为工作小时数*时薪,另外具有设置工作小时数,时薪的函数,具有工作小时数和时薪的属性。salesman的收入计算方法为销售额*提成比例,另外具有设置销售额,提成比例的函数,具有销售额和提成比例的属性。manager的收入计算方法为固定月薪,另外具有设置固定月薪的函数,具有固定月薪的属性。salsemanager收入计算方法为销售额*提成比例+固定月薪。
(4)要求利用employee类型的指针数组,指向8个子类对象(technician,salesman,manager,salsemanager每个类2个),通过相应方法输出这些对象的姓名(姓名的初始化通过键盘录入),员工号,收入(多态的形式)。
#include <iostream>
#include <string>
using namespace std;
class employee {
public:
employee(string name,int ID):m_Name(name),m_ID(ID){}
employee() {}
virtual ~employee() {}
public:
void setName(string name) { m_Name = name; }
virtual void setIncome(int x) {} //设置收入,重载3
virtual void setIncome(int x,float y) {}
virtual void setIncome(int x, float y,int z) {}
//void getName() { cout <<"name is" <<m_Name << endl; }
//void getIncome() { cout <<"income is "<< m_Income << endl; }
//void getID() { cout <<"ID is"<< m_ID << endl; }
virtual void display() {}
protected:
string m_Name;
int m_Income;
int m_ID;
};
class saleman:virtual public employee {
public:
saleman(string name, int ID) :employee(name,ID) {}
saleman() {}
public:
void setIncome( int m_Sales, float m_Scale) { m_Income = m_Sales * m_Scale; }
void display() { cout << m_Name << "is a saleman is " << m_Income << ", ID=" << m_ID << endl; }
protected:
int m_Sales;
float m_Scale;
};
class technician :public employee {
public:
technician(string name, int ID) :employee(name, ID) {}
technician() {}
public :
void setIncome(int m_Time, float m_Rate) { m_Income = m_Time * m_Rate; }
void display() { cout << m_Name << "is a technician,income is " << m_Income << ", ID=" << m_ID << endl; }
protected:
int m_Time;
int m_Rate;
};
class manager :virtual public employee {
public:
manager(string name, int ID) :employee(name, ID) {}
manager() {}
public:
void setIncome(int income) { m_FixedIncome = income,m_Income = m_FixedIncome; }
void display() { cout << m_Name << "is a manager,income is " << m_Income << ", ID=" << m_ID << endl; }
protected:
int m_FixedIncome;
};
class salemanager :public saleman ,public manager {
public:
salemanager(string name, int ID) :employee(name, ID) {}
salemanager() {}
public:
void setIncome(int sales, float scale, int fixedincome) { m_Sales = sales, m_Scale = scale, m_FixedIncome = fixedincome;m_Income = m_Sales * m_Scale + m_FixedIncome; }
void display() { cout << m_Name << "is a salemanager,income is " << m_Income << ", ID=" << m_ID << endl; }
};
int main()
{
employee *p[8] = {new employee() };
p[0] = new saleman("赵小", 2);
p[0]->setIncome(1400, 0.2);
p[1] = new salemanager("王大", 1);
p[1]->setIncome(1465,0.2,5000);
p[2] = new manager("张三", 3);
p[2]->setIncome(5000);
p[3] = new technician("李四", 4);
p[3]->setIncome(240,20.0);
for (int i = 0;i < 4;i++) {
p[i]->display();
delete p[i];
}
system("pause");
}