首页 > 代码库 > C++继承与多态编程题

C++继承与多态编程题

要求编写一个简单的人员信息管理程序,具体要求如下:

(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个),通过相应方法输出这些对象的姓名(姓名的初始化通过键盘录入),员工号,收入(多态的形式)。 

(5)整个程序分为3个文件,employee.h,employee. cpp,mainemployee.cpp,分别用来声明类,实现类,以及实现主函数。在VC++环境下以project的形式进行联编并输出结果。

// employee.h文件

#include<iostream>
#include<string>
using namespace std;
class employee
{
    private:
	string EMPNO;
	string name;
	double income;

    public: 
	void setName(string name);
	string getName();
    void setEMPNO(string EMPNO);
	string getEMPNO();
	 double getIncome();
};

class technician: public employee
{
private:
	int hour;
	double hourWage;
public: 
	void setHour(int hour);
	void setHourWage(double hourWage);
	double getIncome(int hour,double hourWage);
     
};

class salesman:public employee
{
private :
	double salesVolume;
	double proportion;
public: 
	void setSalesVolume(double salesVolume);
    void setProportion(double proportion);
    double getIncome(double salesVolume,double proportion);
     
};

class manager:public employee
{
private:
	double monthlySalary;
public: 
     void setMonthlySalary(double monthlySalary);
     double getIncome(double monthlySalary);
  
};

//多继承子类
class salsemanager:public salesman,public manager
{
public:
	double getIncome(double monthlySalary,double salesVolume,double proportion);
};



// employee.cpp文件
#include"employee.h"

void employee::setName(string name){
	    this->name=name;
}

void employee::setEMPNO(string EMPNO){
	    this->EMPNO=EMPNO;
}

string employee::getName(){
	    return name;
}

string employee::getEMPNO(){
	    return EMPNO;
}

//设置技术员时间
void technician::setHour(int hour){
	this->hour=hour;
}
//设置技术员时薪
void technician::setHourWage(double hourWage){
	this->hourWage=hourWage;
}
//设置销售员销量
void salesman::setSalesVolume(double salesVolume){
	this->salesVolume=salesVolume;
}
//设置销售员分红比例
void salesman::setProportion(double proportion){
	this->proportion=proportion;
}
//设置经理月薪
void manager::setMonthlySalary(double monthlySalary){
	this->monthlySalary=monthlySalary;
}

//实现technician收入计算方法
double technician::getIncome(int hour,double hourWage){
	    return hour*hourWage;
	}
//实现salesman收入计算方法
double salesman::getIncome(double salesVolume,double proportion){
	    return salesVolume*proportion;
	}
//实现manager收入计算方法
double manager::getIncome(double monthlySalary){
	    return monthlySalary;
}
//实现salsemanager收入计算方法
double salsemanager::getIncome(double monthlySalary,double salesVolume,double proportion){
	    return monthlySalary+salesVolume*proportion ;
	}


//mainemployee.cpp文件

#include"employee.h"
int main()
{
	  int hour=210;
	  double hourWage=13.0;
	  double salesVolume=15000.0;
	  double proportion=0.3;
      double monthlySalary=10000;

	  employee *p[8];
	  salesman *a;
	  manager *b;
	  technician t1,t2;
	  salesman s1,s2;
	  manager  m1,m2;
	  salsemanager sm1,sm2;

	  p[0]=&t1;
	  p[1]=&t2;
	  p[2]=&s1;
	  p[3]=&s2;
	  p[4]=&m1;
	  p[5]=&m2;
	  a=&sm1;
	  b=&sm2;
	  string str;
	  
	  cout<<"请输入技术员1的姓名:"<<endl;

	  cin>>str;
		  p[0]->setName(str);
		  p[0]->setEMPNO("1001");
	  cout<<"姓名: "<<p[0]->getName()<<endl;
	  cout<<"员工号: "<<p[0]->getEMPNO()<<endl;
	  cout<<"工资: "<<t1.getIncome(hour,hourWage)<<endl;
	  
	  cout<<"请输入技术员2的姓名:"<<endl;
      
	  cin>>str;
		  p[1]->setName(str);
		  p[1]->setEMPNO("1002");
	  cout<<"姓名: "<<p[1]->getName()<<endl;
	  cout<<"员工号: "<<p[1]->getEMPNO()<<endl;
	  cout<<"工资: "<<t2.getIncome(hour,hourWage)<<endl;
	 
	  

	  cout<<"请输入销售员1的姓名:"<<endl;
	  cin>>str;
		  p[2]->setName(str);
		  p[2]->setEMPNO("2001");
	  cout<<"姓名"<<p[2]->getName()<<endl;
	  cout<<"员工号"<<p[2]->getEMPNO()<<endl;
	  cout<<"工资: "<<s1.getIncome(salesVolume,proportion)<<endl;
	  
	  cout<<"请输入销售员2的姓名:"<<endl;
	  cin>>str;
		  p[3]->setName(str);
		  p[3]->setEMPNO("2002");
	  cout<<"姓名"<<p[3]->getName()<<endl;
	  cout<<"员工号"<<p[3]->getEMPNO()<<endl;
	  cout<<"工资: "<<s2.getIncome(salesVolume,proportion)<<endl;
	  


	  cout<<"请输入经理1的姓名:"<<endl;
	  cin>>str;
		  p[4]->setName(str);
		  p[4]->setEMPNO("3001");
	  cout<<"姓名"<<p[4]->getName()<<endl;
	  cout<<"员工号"<<p[4]->getEMPNO()<<endl;
	  cout<<"工资: "<<m1.getIncome(monthlySalary)<<endl;
	  
	  cout<<"请输入经理2的姓名:"<<endl;
	  cin>>str;
		  p[5]->setName(str);
		  p[5]->setEMPNO("3002");
	  cout<<"姓名"<<p[5]->getName()<<endl;
	  cout<<"员工号"<<p[5]->getEMPNO()<<endl;
	  cout<<"工资: "<<m2.getIncome(monthlySalary)<<endl;



	  cout<<"请输入销售经理1的姓名:"<<endl;
	   cin>>str;
		  a->setName(str);
		  a->setEMPNO("4001");
	  cout<<"姓名"<<a->getName()<<endl;
	  cout<<"员工号"<<a->getEMPNO()<<endl;
	  cout<<"工资: "<<sm1.getIncome(monthlySalary,salesVolume,proportion)<<endl;
	  
	  cout<<"请输入销售经理2的姓名:"<<endl;
	   cin>>str;
		  b->setName(str);
		  b->setEMPNO("4002");
	  cout<<"姓名"<<b->getName()<<endl;
	  cout<<"员工号"<<b->getEMPNO()<<endl;
	  cout<<"工资: "<<sm2.getIncome(monthlySalary,salesVolume,proportion)<<endl;
	  
  return 0;
}