首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

对自定义种的list实现多态

2013-07-16 
对自定义类的list实现多态#ifndef EMPLOYEE_H#define EMPLOYEE_H#include string#include iostreamusi

对自定义类的list实现多态


#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <string>
#include <iostream>
using namespace std;

const int AccumPayManager = 12000;
const int AccumPaySalesManager = 8000;
const double DeductRateSalesman = 0.05;
const double DeductRateSalesManager = 0.04;


class employee
{
public:
employee(): grade(4), individualEmpNo(++tempNumber), wagePromotion(0) {}
employee(string strName, int iGra, int Num = ++tempNumber):
name(strName), grade(iGra), individualEmpNo(Num), wagePromotion(0) {}

virtual double PAY() const{return 0;};

// To promote an employee
virtual void promote() {};
virtual string displayPosition() const {return "NONE";};
virtual void transData(ofstream &of) {}

friend ostream& operator<<(ostream &, const employee *);
friend ostream& operator<<(ostream &, const employee &);
friend void initData(list<employee> &em, int &, int &, int &, int&);
friend void outitData(list<employee> &em, int intMan, int intTec, int intSMan, int intSManger);
protected:
int grade, individualEmpNo; 
string name;
static int tempNumber;
// promotion of one's payment
double wagePromotion;
};

// grade == 1
class manager: virtual public employee
{
public:
manager(string strName = "Default Name", int payment = AccumPayManager):
employee(strName, 1), accumPay(payment) {}; 
virtual double PAY() const
{
return accumPay + wagePromotion;
}
virtual void promote()
{
wagePromotion += 0.05 * PAY();
}
virtual string displayPosition() const
{
return "Manager";
}
virtual void transData(ofstream &of)
{
of << name << ' ' <<  accumPay << ' ' << individualEmpNo << endl;
}
protected:
int accumPay;
};

// grade == 2
class technician: virtual public employee
{


public:
technician(string strName = "Default Name", double WorkHours = 0):
employee(strName, 2), workHours(WorkHours) {};
virtual double PAY() const
{
return workHours * wagePerHour + wagePromotion;
}
virtual void promote()
{
wagePromotion += workHours * 10;
}
virtual string displayPosition() const
{
return "Technician";
}
protected:
// work-hours of a technician
double workHours;
// wage-per-hour
static const int wagePerHour = 260;
};

// grade == 3
class salesman: virtual public employee
{
public:
salesman(string strName = "Default Name", double Sales = 0, double deduction = DeductRateSalesman):
employee(strName, 3), sales(Sales), deductRate(deduction) 
{ totalSales += sales; };
virtual double PAY() const
{
return sales * deductRate + wagePromotion;
}
virtual void promote()
{
wagePromotion += sales * 0.15;
}
virtual string displayPosition() const
{
return "Salesman";
}
protected:
// sales and deduction rate of salesman&sales manager
double sales;
double deductRate;

// 销售员的销售总额,用于计算销售经理的提成
static double totalSales;
};

// grade == 4
class salesmanager: public salesman, public manager
{
public:
salesmanager(string strName = "Default Name"):
employee(strName, 4), 
salesman(strName, 0, DeductRateSalesManager), 
manager(strName, AccumPaySalesManager) {};
virtual double PAY() const
{
return accumPay + totalSales * deductRate + wagePromotion;
}
virtual void promote()
{
wagePromotion += 0.01 * totalSales; 
}
virtual string displayPosition() const 
{
return "Sales Manager";
}
};
#endif


假如我这样定义以后,再定义

list<employee> em;
manager tempMan;
em.push_front(tempMan);
list<employee>::iterator it = em.begin();


那么这时为什么用it去调用虚函数就无法实现多态性?如果要实现多态性该怎么做?


[解决办法]
运行时多态需要用指针,不想自己管理内存的话,可以用智能指针。

,搞不清楚为什么。把前后代码放出来 

热点排行