类的继承和封装
代码
#include <string.h>
class CEmployee
{
private:
char m_name[30];
public:
CEmployee(){};//此处,必须实现,以前C++版本是可以的,但vs2010下不可以
CEmployee(const char *nm) {
strcpy_s(m_name, nm);
}
};
class CWage : public CEmployee //時薪職員
{
private:
float m_wage; //時薪
float m_hours; //時間
public:
CWage(const char *nm) : CEmployee(nm) {
//CEmployee(nm);//構造函數可以直接調用?前面的代碼==此行?
m_wage = 250.0;
m_hours = 40.0;
}
void setWage(float wage) {m_wage = wage;}
void setHours(float hours) {m_hours = hours;}
};
//銷售員:有時薪,另外還有銷售額
class CSales : public CWage //銷售員
{
private:
float m_commission; //?
float m_sale; //銷售數量
public:
CSales(const char *name) : CWage(name) {
m_commission = m_sale = 0.0;
}
void setCommission(float commission){m_commission = commission;}
void setSales(float sale) { m_sale = sale; }
float computePay();
};
class CManager : public CEmployee
{
private:
float m_salary;
public:
CManager(const char*name) { m_salary = 15000; }
void setSalary(float salary) { m_salary = salary; }
float computePay();
};
void main01()
{
CManager manager("Manager Chen");
CSales sales("Sales Hou");
CWage wage("Wage Lee");
}
class CEmployee
{
private:
char m_name[30];
public:
CEmployee(){};//此处,必须实现,以前C++版本是可以的,但vs2010下不可以
CEmployee(const char *nm) {
strcpy_s(m_name, nm);
}
};
class CWage : public CEmployee //時薪職員
{
private:
float m_wage; //時薪
float m_hours; //時間
public:
CWage(const char *nm) : CEmployee(nm) {
//CEmployee(nm);//構造函數可以直接調用?前面的代碼==此行?
m_wage = 250.0;
m_hours = 40.0;
}
void setWage(float wage) {m_wage = wage;}
void setHours(float hours) {m_hours = hours;}
};
//銷售員:有時薪,另外還有銷售額
class CSales : public CWage //銷售員
{
private:
float m_commission; //?
float m_sale; //銷售數量
public:
CSales(const char *name) : CWage(name) {
m_commission = m_sale = 0.0;
}
void setCommission(float commission){m_commission = commission;}
void setSales(float sale) { m_sale = sale; }
float computePay();
};
class CManager : public CEmployee
{
private:
float m_salary;
public:
CManager(const char*name) { m_salary = 15000; }
void setSalary(float salary) { m_salary = salary; }
float computePay();
};
void main01()
{
CManager manager("Manager Chen");
CSales sales("Sales Hou");
CWage wage("Wage Lee");
}