类的简单运用

//要求:公司有四个职务:经理:基本工资(8000) 技工:时薪100  销售:销售额的百分之四 销售经理:基本工资(4000)以及销售额的百分之二
//用到:类,多继承,虚继承,序函数&抽象类,随机变量

#include <iostream>
using namespace std;
#include <cstring>
#include <time.h>


class employee          //员工类
{
protected:
    string m_name;      //姓名
    static int m_num;   //员工编号,由一千开始增加
    int m_id;           //员工个人编号
    double m_salary;     //   薪水
public:
    employee(string name);
    virtual double Getsalary() = 0; // 虚函数 ,薪水
    virtual void show() = 0;        //虚函数 ,显示员工信息


};
int employee::m_num = 1000;         //员工编号赋初值由一千开始
employee::employee(string name)     //构造函数赋值,员工姓名,员工ID
{
    m_name = name;
    m_num ++;
    m_id = m_num;
}
class Manager:virtual public employee       //经理虚继承员工类,避免下面销售经理在继承经理和销售的类时,出现二义性
{
protected:
    int m_BS;                               //基本工资 
    int m_ES;                               //额外工资
public:
    Manager(string name,int bs,int es = 0);     //构造函数赋值,姓名,基本工资,额外工资
    double Getsalary();
    void show();
};
Manager::Manager(string name,int bs,int es):employee(name)
{
    m_BS = bs;
    m_ES = es;
}
void Manager::show()
{
    cout <<"姓名: "<< m_name << "  工号: " << m_id << "  职位: 经理" <<"  薪水:"<< m_salary<< endl; 
}
double Manager::Getsalary()
{
    m_salary =  m_BS + m_ES;
    return m_salary;
}
class Sale:virtual public employee              //销售类,虚继承员工类
{
protected:
    int m_SS;                                   //销售额
    static double m_exo;                        //销售经理提成额度累积量
public:
    Sale(string name,int ss = 0);               //销售构造函数,姓名,销售额
    double Getsalary();
    void show();
};
double Sale::m_exo = 0;                     //静态变量赋初值
void Sale::show()
{
    cout << "姓名: " << m_name << "  工号: " << m_id << "  职位: 销售" << "  薪水: " << m_salary << endl;
}
Sale::Sale(string name,int ss ):employee(name)  
{
    m_SS = ss;
}
double Sale::Getsalary()
{
    m_exo += 0.02*m_SS;
    m_salary = 0.04*m_SS;
    return m_salary;
}
class SaleManager:public Manager,public Sale            //销售经理,继承销售和经理类
{
public:
    SaleManager(string name,int bs,int ss = 0,int es = 0); //构造函数赋值,姓名,基本工资,销售额,额外工资(销售的提成)
    double Getsalary();    
    void show();
};
SaleManager::SaleManager(string name,int bs,int ss ,int es ):Sale(name,ss),Manager(name,bs,es),employee(name)
{
    
}
void SaleManager::show()
{
    cout << "姓名: " << m_name << "  工号: "<< m_id << "  职位:  销售经理" << "  薪水: " << m_salary << endl;
}
double SaleManager::Getsalary()
{
    m_salary =  m_BS + m_ES + m_exo;
    return m_salary;
}


class Technician:public employee                //技工,继承员工类
{
protected:
   // int m_HS;
    int m_H;                                    //定义变量工时,
public:
    Technician(string name,int hs);             //构造函数,姓名,工时
    double Getsalary();
    void show();
};
Technician::Technician(string name,int h):employee(name)
{
    //m_HS = hs;
    m_H = h;
}
double Technician::Getsalary()
{
    m_salary = m_H *100;
    return m_salary;
}
void Technician::show()
{


    cout << "姓名: " << m_name << "  工号: " << m_id <<"  职位: 技工" << "  薪水: " << m_salary << endl;


}
    int main(int argc, char **argv)
{
    //srand((unsigned int ) time(NULL));   //设置随机函数种子     
    //int i = rand() % 200000;              //可以设置随机数来方便手动输入
    //这里简单一点手动赋值
    Manager s1("陶吉吉",8000);
    Technician s2("章吉吉",240);
    Sale s3("小胖纸" , 150000);
    Sale s4("帅哥卫",200000);
    SaleManager s5("小陶",4000);
    s1.Getsalary();
    s2.Getsalary();
    s3.Getsalary();
    s4.Getsalary();
    s5.Getsalary();
    s1.show();
    s2.show();
    s3.show();
    s4.show();
    s5.show();
    return 0;
}
阅读更多

没有更多推荐了,返回首页