error C2039: 'computePay' : is not a member of 'CEmployee'
#include <string>
#include <iostream>
using namespace std;
class CEmployee //职员
{
private:
char m_name[30];
public:
CEmployee();
CEmployee(const char* nm) { strcpy(m_name, nm); }
};
//----------------------------------// 时薪职员是一种职员
class CWage : public CEmployee
{
private :
float m_wage;//钟点费
float m_hours;//每周工时
public :
CWage(const char* nm) : CEmployee(nm) { m_wage = 250.0; m_hours = 40.0; }
void setWage(float wg) { m_wage = wg; }
void setHours(float hrs) { m_hours = hrs; }
virtual void computePay(){ cout<<"output from the class CWage"<<endl; }
};
//----------------------// 销售员是一种时薪职员
class CSales : public CWage
{
private :
float m_comm;//佣金
float m_sale;//销售额
public :
CSales(const char* nm) : CWage(nm) { m_comm = m_sale = 0.0; }
void setCommission(float comm) { m_comm = comm; }
void setSales(float sale) { m_sale = sale; }
void computePay(){ cout<<"output from the class CSales"<<endl; }
};
//------------------------// 经理也是一种职员
class CManager : public CEmployee
{
private :
float m_salary;//薪水
public :
CManager(const char* nm) : CEmployee(nm) { m_salary = 15000.0; }
void setSalary(float salary) { m_salary = salary; }
void computePay(){ cout<<"output from the class CManager"<<endl; }
};
//---------------------------
void main()
{
CEmployee* pEmp;
CWage aWager("曾铭源");
CSales aSales("侯俊杰");
CManager aManager("陈美静");
pEmp = &aWager;
cout << pEmp->computePay(); // 调用的是 CWage::computePay
pEmp = &aSales;
cout << pEmp->computePay(); // 调用的是 CSales::computePay
pEmp = &aManager;
cout << pEmp->computePay(); // 调用的是 CManager::computePay
}
//求指点,我这个程序就是想实现虚函数的多态性,用基类的指针调用派生类的虚函数。可是它总是提示我有错误(如题)
[解决办法]
class CEmployee //职员 { private: char m_name[30]; public: CEmployee(); CEmployee(const char* nm) { strcpy(m_name, nm); } virtual void computePay(){ cout<<"output from the class CEmployee"<<endl; } }; //----------------------------------// 时薪职员是一种职员 class CWage : public CEmployee { private : float m_wage;//钟点费 float m_hours;//每周工时 public : CWage(const char* nm) : CEmployee(nm) { m_wage = 250.0; m_hours = 40.0; } void setWage(float wg) { m_wage = wg; } void setHours(float hrs) { m_hours = hrs; } void computePay(){ cout<<"output from the class CWage"<<endl; } //virtual可以省略}; //----------------------// 销售员是一种时薪职员 class CSales : public CWage { private : float m_comm;//佣金 float m_sale;//销售额 public : CSales(const char* nm) : CWage(nm) { m_comm = m_sale = 0.0; } void setCommission(float comm) { m_comm = comm; } void setSales(float sale) { m_sale = sale; } void computePay(){ cout<<"output from the class CSales"<<endl; } }; //------------------------// 经理也是一种职员 class CManager : public CEmployee { private : float m_salary;//薪水 public : CManager(const char* nm) : CEmployee(nm) { m_salary = 15000.0; } void setSalary(float salary) { m_salary = salary; } void computePay(){ cout<<"output from the class CManager"<<endl; } }; //--------------------------- void main() { CEmployee* pEmp; CWage aWager("曾铭源"); CSales aSales("侯俊杰"); CManager aManager("陈美静"); pEmp = &aWager; pEmp->computePay(); // 调用的是 CWage::computePay pEmp = &aSales; pEmp->computePay(); // 调用的是 CSales::computePay pEmp = &aManager; pEmp->computePay(); // 调用的是 CManager::computePay return;}
[解决办法]