需求:1.一家公司内,有四种职位,经理(Manager),技术人员(Technician,销售(Salesman),销售经理(SalesManager)。
2.每个员工都有自己的工号,工号从1000开始,每多一个人加1。
3.公司:经理:8000,技术人员:100/h,销售:4%*月销售额,销售经理:2%*月总销售额。
1.首先创建.h文件 Staff.h,五种职位,创建五个类。
#ifndef _STAFF_H_ #define _STAFF_H_ #include <iostream> using namespace std; class Staff { protected: string m_name; static int m_num; string m_position; double m_money; int m_id; static double m_allmoney; public: Staff(string name,string position); virtual void print() = 0; }; class Technician:public Staff { int m_time; public: Technician(string name,int time); void print(); }; class Manager : public Staff { public: Manager(string name); void print(); }; class Salesman : public Staff { public: Salesman(string name); Salesman(string name,int money); void print(); }; class SalesManager : public Salesman { public: SalesManager(string name); void print(); }; #endif
2.然后床垫.cpp文件,staff.cpp
#include "Staff.h" #include <iostream> using namespace std; int Staff::m_num = 1000; double Staff::m_allmoney = 0; Staff::Staff(string name,string position) { m_name = name; m_position = position; m_num ++; m_id = m_num; } Technician::Technician(string name,int time) :Staff(name,"Technician") { m_time = time; m_money = (100 * m_time); } void Technician::print() { cout << "The name is : " << m_name << endl; cout << "The jobnumber is : " << m_num << endl; cout << "The position is : " << m_position << endl; cout << "The worktime is : " << m_time << endl; cout << "The monthmoney is: " << m_money << endl; cout << "*********************************" << endl; } Manager::Manager(string name) : Staff(name,"Manager") { m_money = 8000; } void Manager::print() { cout << "The name is : " << m_name << endl; cout << "The jobid is : " << m_id << endl; cout << "The position is : " << m_position << endl; cout << "The monthmoney is: " << m_money << endl; cout << "***************************" << endl; } Salesman::Salesman(string name):Staff(name,"SalesManager") { } Salesman::Salesman(string name,int money) : Staff(name,"Salesman") { m_money = 0.04 * money; m_allmoney += money; } void Salesman::print() { cout << "The name is : " << m_name << endl; cout << "The jobid is : " << m_id << endl; cout << "The position is : " << m_position << endl; cout << "The monthmoney is: " << m_money << endl; cout << "***************************" << endl; } SalesManager::SalesManager(string name) : Salesman(name) { m_money = 4000 + 0.02 * m_allmoney; } void SalesManager::print() { cout << "The name is : " << m_name << endl; cout << "The jobid is : " << m_id << endl; cout << "The position is : " << m_position << endl; cout << "The monthmoney is: " << m_money << endl; cout << "******************************" << endl; }
3.创建执行文件main.cpp
#include <iostream> #include "Staff.h" using namespace std; int main(int argc, char **argv) { Manager a("Laowangba"); Technician b("Zhanganliu",rand() % 300); Salesman c("Xiaojibatao",24561); Salesman d("Da jibatao",36541); Salesman f("Old jibatao",54134); SalesManager e("Niu jia"); a.print(); b.print(); c.print(); d.print(); e.print(); f.print(); return 0; }