假设我们有下列需求
很明显,针对这个需求,我们需要设计四个不同的类,即经理、技术人员、推销员和销售经理四个不同的类,但是对于需要存储的东西来说,很多都是相同的,比如说姓名、编号、级别、薪水等,因此我们设计一个抽象基类
因此我们首先设计一个称为Employee的基类,图示如下
并且根据需求,每个类都有不同的晋升机制,每个类都有计算薪水的机制以及每个类都有展示出自己所有信息的方式,因此我们提取void promote(),void calcSalary(),void disInfo()三个函数作为接口来处理这个问题
首先点击添加一个类Employee,在.h文件中输入以下内容
1 #ifndef EMPLOYEE_H 2 #define EMPLOYEE_H 3 #include <iostream> 4 using namespace std; 5 6 class Employee 7 { 8 public: 9 Employee(); 10 protected: 11 protected: 12 string _name; 13 int _num; 14 int _level; 15 float _salary; 16 static int _startNumber; 17 };
在这里就是将经理、技术人员、推销员和销售经理四个不同的类中相同的部分抽象成一个基类
类Employee在.cpp中输入以下内容
1 #include "employee.h" 2 3 //静态的东西需要老早就初始化 4 int Employee::_startNumber=1000; 5 Employee::Employee() 6 { 7 8 }
抽象基类设计完成,我们设计Manager类
在Manager类的.h文件中输入以下内容
1 #ifndef MANAGER_H 2 #define MANAGER_H 3 #include "employee.h" 4 5 class Manager:virtual public Employee 6 { 7 public: 8 Manager(); 9 virtual void init(); 10 virtual void promote(); 11 virtual void calcSalary(); 12 virtual void disInfo(); 13 protected: 14 float _fixedSalary; 15 }; 16 17 #endif // MANAGER_H
在Manager类中的.cpp文件中输入以下内容
1 #include "manager.h" 2 3 Manager::Manager() 4 { 5 } 6 7 void Manager::init() 8 { 9 _fixedSalary=8000; 10 _num=_startNumber++; 11 _level=1; 12 cout<<"请输入经理的姓名:"<<endl; 13 cin>>_name; 14 } 15 void Manager::promote() 16 { 17 _level+=4; 18 } 19 void Manager::calcSalary() 20 { 21 _salary=_fixedSalary; 22 } 23 void Manager::disInfo() 24 { 25 cout<<"姓名 :"<<_name<<endl; 26 cout<<"工号 :"<<_num<<endl; 27 cout<<"级别 :"<<_level<<endl; 28 cout<<"本月的结算的薪水 :"<<_salary<<endl; 29 cout<<"+++++++++++++++++++++++"<<endl; 30 }
然后再生成一个SalesMan类
在SalesMan类的.h文件中输入以下内容
1 #ifndef SALESMAN_H 2 #define SALESMAN_H 3 #include "employee.h" 4 5 class SalesMan:virtual public Employee 6 { 7 public: 8 SalesMan(); 9 virtual void init(); 10 virtual void promote(); 11 virtual void calcSalary(); 12 virtual void disInfo(); 13 protected: 14 float _saleAmount; 15 float _rate; 16 }; 17 18 #endif // SALESMAN_H
然后在SalesMan中的.cpp文件中输入以下内容
1 #include "salesman.h" 2 3 SalesMan::SalesMan() 4 { 5 } 6 7 void SalesMan::init() 8 { 9 _rate=0.04; 10 _num=_startNumber++; 11 _level=1; 12 cout<<"请输入销售员的姓名:"<<endl; 13 cin>>_name; 14 cout<<"请输入本月销售额:"<<endl; 15 cin>>_saleAmount; 16 } 17 void SalesMan::promote() 18 { 19 _level +=1; 20 } 21 void SalesMan::calcSalary() 22 { 23 _salary=_saleAmount * _rate; 24 } 25 void SalesMan::disInfo() 26 { 27 cout<<"姓名 :"<<_name<<endl; 28 cout<<"工号 :"<<_num<<endl; 29 cout<<"级别 :"<<_level<<endl; 30 cout<<"本月的销售额度 :"<<_saleAmount<<endl; 31 cout<<"本月的提成比率 :"<<_rate<<endl; 32 cout<<"本月的结算的薪水 :"<<_salary<<endl; 33 cout<<"++++++++++++++++++++++"<<endl; 34 }
然后生成Technician类,并在其.h文件中输入以下内容
1 #ifndef TECHNICIAN_H 2 #define TECHNICIAN_H 3 4 #include "employee.h" 5 6 class Technician:public Employee 7 { 8 public: 9 Technician(); 10 virtual void init(); 11 virtual void promote(); 12 virtual void calcSalary(); 13 virtual void disInfo(); 14 protected: 15 int _amountHour; 16 int _moneyPerHour; 17 }; 18 19 #endif // TECHNICIAN_H
然后在其.cpp文件中输入以下内容
1 #include "technician.h" 2 3 Technician::Technician() 4 { 5 } 6 7 void Technician::init() 8 { 9 _num=_startNumber++; 10 _level=1; 11 _moneyPerHour=100; 12 cout<<"请输入技术人员的姓名:"<<endl; 13 cin>>_name; 14 cout<<"请输入本月工作的小时数:"<<endl; 15 cin>>_amountHour; 16 } 17 void Technician::promote() 18 { 19 _level+=3; 20 } 21 void Technician::calcSalary() 22 { 23 _salary=_amountHour * _moneyPerHour; 24 } 25 void Technician::disInfo() 26 { 27 cout<<"姓名 :"<<_name<<endl; 28 cout<<"工号 :"<<_num<<endl; 29 cout<<"级别 :"<<_level<<endl; 30 cout<<"本月工作的小时数 :"<<_amountHour<<endl; 31 cout<<"每个工作时的薪水 :"<<_moneyPerHour<<endl; 32 cout<<"本月的结算的薪水 :"<<_salary<<endl; 33 cout<<"++++++++++++++++++++++"<<endl; 34 }
最后要生成多继承的销售经理,其同时要继承销售类和经理类
在其.h文件中输入以下内容
1 #ifndef SALEMANAGER_H 2 #define SALEMANAGER_H 3 #include "salemanager.h" 4 #include "manager.h" 5 #include "salesman.h" 6 7 class SaleManager:public Manager,public SalesMan 8 { 9 public: 10 SaleManager(); 11 virtual void init(); 12 virtual void promote(); 13 virtual void calcSalary(); 14 virtual void disInfo(); 15 }; 16 17 #endif // SALEMANAGER_H
在其.cpp文件中输入以下内容
1 #include "salemanager.h" 2 3 SaleManager::SaleManager() 4 { 5 6 } 7 8 void SaleManager::init() 9 { 10 _fixedSalary=5000; 11 _num=_startNumber++; 12 _rate=0.05; 13 _level=1; 14 cout<<"请输入销售经理的姓名:"<<endl; 15 cin>>_name; 16 cout<<"请输入本月销售额:"<<endl; 17 cin>>_saleAmount; 18 } 19 void SaleManager::promote() 20 { 21 _level+=3; 22 } 23 void SaleManager::calcSalary() 24 { 25 _salary=_fixedSalary +_saleAmount * _rate; 26 } 27 28 void SaleManager::disInfo() 29 { 30 cout<<"姓名 :"<<_name<<endl; 31 cout<<"工号 :"<<_num<<endl; 32 cout<<"级别 :"<<_level<<endl; 33 cout<<"本月的固定的薪水 :"<<_fixedSalary<<endl; 34 cout<<"本月销售额度 :"<<_saleAmount<<endl; 35 cout<<"本月的提成比率 :"<<_rate<<endl; 36 cout<<"本月的结算的薪水 :"<<_salary<<endl; 37 cout<<"++++++++++++++++++++++"<<endl; 38 }
最后是其main函数
1 #include <iostream> 2 #include "technician.h" 3 #include "manager.h" 4 #include "salesman.h" 5 #include "salemanager.h" 6 using namespace std; 7 8 int main() 9 { 10 Technician tech; 11 tech.init(); 12 tech.promote(); 13 tech.calcSalary(); 14 tech.disInfo(); 15 16 Manager man; 17 man.init(); 18 man.promote(); 19 man.calcSalary(); 20 man.disInfo(); 21 22 SalesMan sman; 23 sman.init(); 24 sman.promote(); 25 sman.calcSalary(); 26 sman.disInfo(); 27 28 SaleManager sm; 29 sm.init(); 30 sm.promote(); 31 sm.calcSalary(); 32 sm.disInfo(); 33 return 0; 34 }
为了简便起见,还可以将其写成数组
1 #include <iostream> 2 #include "technician.h" 3 #include "manager.h" 4 #include "salesman.h" 5 #include "salemanager.h" 6 using namespace std; 7 8 int main() 9 { 10 Employee *empArr[]={new Manager,new Technician, 11 new SalesMan,new SaleManager}; 12 for(auto emp:empArr) 13 { 14 emp->init(); 15 emp->promote(); 16 emp->calcSalary(); 17 emp->disInfo(); 18 } 19 return 0; 20 }