- 简单介绍
- 代码
- educationh
- educationcpp
- maincpp
简单介绍
- 在员工(Staff)类中有一个属性是教育背景,它又包含了毕业院校和员工学历两个属性,因此把教育背景设计成一个CEducation类,而由他创建的对象作为一个子对象组合出Staff类。
- 从企业员工中划分出来的销售人员、行政人员和经理都具有企业员工的一般特征,因此这三个类可以继承Staff类,而销售经理同时具有销售人员和经理这两个类的共同特征,因此该类可以由这两个类共同派生出来。由此产生一个局部的多继承关系。在这个局部多继承关系中,Staff类是公共基类,销售经理类就是公共派生类。为了避免二义性的发生,可以把Staff类声明为虚基类。为了避免繁琐的初始化,所有的虚基类的构造函数都不带参数,他们的数据成员都通过键盘录入的方式进行有效的赋值。
代码
education.h
#ifndef __EDUCATION__H__
#define __EDUCATION__H__
#include
#include
using namespace std;
const int LEN = 50;
class CEducation
{
public:
CEducation();
CEducation(char cSchool[], char cDegree);
~CEducation();
void GetEdu(void);
void PutEdu(void) const;
private:
char m_cSchool[LEN];
char m_cDegree;
};
class Staff
{
public:
Staff();
~Staff();
void CalculateSal();
void OutPut();
void InPut();
protected:
CEducation Edu;
int m_iStaffNum;
char m_cName;
float m_fRateOfAttend;
float m_fBasicSal;
float m_fPrize;
static int s_iCount;
};
class CAdminStaff: public Staff
{
public:
CAdminStaff();
~CAdminStaff();
void CalculateSal();
};
class CManager: virtual public Staff
{
public:
CManager();
~CManager();
void SetMData();
void CalculateSal();
protected:
float m_fDeductTRate;
float m_fTAmount;
};
class CSaleMan: virtual public Staff
{
public:
CSaleMan();
~CSaleMan();
void SetSData();
void CalculateSal();
protected:
float m_fDeductRate;
float m_fPersonAmount;
};
class CSaleManager: public CSaleMan, public CManager
{
public:
CSaleManager();
~CSaleManager();
void CalculateSal();
};
#endif
education.cpp
#include "education.h"
int Staff::s_iCount = 1000;
CEducation:: CEducation(){};
CEducation:: CEducation(char cSchool[], char cDegree)
{
strcpy(m_cSchool, cSchool);
m_cDegree = cDegree;
}
CEducation:: ~CEducation(){};
void CEducation:: GetEdu(void)
{
cout << "School:"<< endl;
cin >> m_cSchool;
cout << "Degree:"<< endl;
cin >> m_cDegree;
m_cDegree = toupper(m_cDegree);
}
void CEducation:: PutEdu(void) const
{
cout << "School: " << m_cSchool << endl;
cout << "Degree: ";
switch (m_cDegree)
{
case 'c':
cout << "college" << endl;
break;
case 'u':
cout << "undergraduate college" << endl;
break;
case 'M':
cout << "master" << endl;
break;
case 'D':
cout << "doctor" << endl;
break;
default:
cout << "void" << endl;
break;
}
}
Staff:: Staff(){};
Staff:: ~Staff(){};
void Staff:: CalculateSal(){};
void Staff:: InPut()
{
m_iStaffNum = ++s_iCount;
cout << "No: "<< m_iStaffNum << endl;
cout << "name :";
cin >> m_cName;
Edu.GetEdu();
cout << " Basic salary:";
cin >> m_fBasicSal;
cout << " Prize: ";
cin >> m_fPrize;
cout << "Rate Of Attend: ";
cin >> m_fRateOfAttend;
if (m_fRateOfAttend > 1.0)
{
m_fRateOfAttend = 1.0;
}
else if (m_fRateOfAttend < 0)
{
m_fRateOfAttend = 0;
}
}
void Staff:: OutPut()
{
cout << "No: " << m_iStaffNum << endl;
cout << "name: " << m_cName << endl;
Edu.PutEdu();
cout << "basic saraly: " << m_fBasicSal << endl;
cout << "Prize: " << m_fPrize << endl;
cout << "Rate of Attend" << m_fRateOfAttend * 100 << "%" << endl;
}
CAdminStaff:: CAdminStaff(){};
CAdminStaff:: ~CAdminStaff(){};
void CAdminStaff::CalculateSal()
{
cout << "the fact salary:";
cout << m_fBasicSal + m_fRateOfAttend * m_fPrize << endl;
}
CManager:: CManager(){};
CManager:: ~CManager(){};
void CManager:: SetMData()
{
cout << "the company sales";
cin >> m_fTAmount;
cout << "the rates of person getted";
cin >> m_fDeductTRate;
}
void CManager:: CalculateSal()
{
cout << "the fact salary:";
cout << m_fBasicSal + m_fDeductTRate * m_fTAmount << endl;
}
CSaleMan :: CSaleMan(){};
CSaleMan :: ~CSaleMan(){};
void CSaleMan:: SetSData()
{
cout << "the person sales";
cin >> m_fPersonAmount;
cout << "the rates of person getted";
cin >> m_fDeductRate;
}
void CSaleMan:: CalculateSal()
{
cout << "the fact salary:";
cout << m_fBasicSal + m_fPersonAmount * m_fDeductRate << endl;
}
CSaleManager:: CSaleManager(){};
CSaleManager:: ~CSaleManager(){};
void CSaleManager:: CalculateSal()
{
cout << "the fact salary:";
cout << m_fBasicSal + m_fPersonAmount * m_fDeductRate
+ m_fTAmount * m_fDeductTRate << endl;
}
main.cpp
#include "education.h"
int main(int argc, char const *argv[])
{
char flag = 'Y';
while (toupper (flag) == 'Y')
{
cout << "1.maneger 2.saleman 3.saleanager 4.administor" << endl;
int n;
cin >> n;
switch(n)
{
case 1:
{
CManager staff;
staff.InPut();
staff.SetMData();
staff.OutPut();
staff.CalculateSal();
break;
}
case 2:
{
CSaleMan staff;
staff.InPut();
staff.SetSData();
staff.OutPut();
staff.CalculateSal();
break;
}
case 3:
{
CSaleManager staff;
staff.InPut();
staff.SetMData();
staff.SetSData();
staff.OutPut();
staff.CalculateSal();
break;
}
case 4:
{
CAdminStaff staff;
staff.InPut();
staff.OutPut();
staff.CalculateSal();
break;
}
default:
{
cout << "error" << endl;
break;
}
}
cout << "continue?(y/n)" << endl;
cin >> flag;
}
return 0;
}