编译环境:win10(64bit),VS2017
UML:
main.cpp
#include <iostream>
using namespace std;
#include "employee.h"
#include "salariedemployee.h"
#include "commissionemployee.h"
#include "basepluscommissionemployee.h"
#include "hourlyemployee.h"
int main()
{
SalariedEmployee xiaohong("123456", "xiaohong");
xiaohong.display();
CommissionEmployee xiaozhang("987654", "xiaozhang");
xiaozhang.display();
BasePlusCommissionEmployee xiaoming("112233", "xiaoming");
xiaoming.display();
HourlyEmployee xiaobai("445566", "xiaobai");
xiaobai.display();
BasePlusCommissionEmployee xiaohei("778855", "xiaohei");
xiaohei.display();
return 0;
}
mystring.h
#pragma once
#ifndef __MY_STRING_H
#define __MY_STRING_H
#include <iostream>
using namespace std;
class MyString
{
public:
MyString();
MyString(const char *str);
MyString(const MyString &str);
MyString &operator=(const MyString &str);
~MyString();
friend ostream &operator<<(ostream &os, const MyString &str);
friend bool operator==(const MyString &str1, const MyString &str2);
const char *getData(void);
void append(const char *str);
void disp(void) const;
private:
char *m_data;
unsigned int m_len;
};
#endif // __MY_STRING_H
mystring.cpp
#include "mystring.h"
#include <string.h>
MyString::MyString()
{
m_data = NULL;
m_len = 0;
}
MyString::MyString(const char *str)
{
m_len = strlen(str);
m_data = new char[m_len + 1];
strcpy(m_data, str);
}
MyString::MyString(const MyString &str)
{
m_len = strlen(str.m_data);
m_data = new char[m_len + 1];
strcpy(m_data, str.m_data);
}
MyString::~MyString()
{
if (m_data != NULL)
{
delete[]m_data;
}
}
const char *MyString::getData(void)
{
return m_data;
}
MyString &MyString::operator=(const MyString &str)
{
if (this == &str)
{
return *this;
}
if (m_data != NULL)
{
delete[]m_data;
}
m_len = strlen(str.m_data);
m_data = new char[m_len + 1];
strcpy(m_data, str.m_data);
return *this;
}
void MyString::append(const char *str)
{
m_len += strlen(str);
char *temp = new char[m_len + 1];
strcpy(temp, m_data);
strcat(temp, str);
delete m_data;
m_data = temp;
}
void MyString::disp(void) const
{
cout << m_data << endl;
}
bool operator==(const MyString &str1, const MyString &str2)
{
return strcmp(str1.m_data, str2.m_data);
}
ostream &operator<<(ostream &os, const MyString &str)
{
return os << str.m_data;
}
employee.h
#pragma once
#ifndef __EMPLOYEE_H
#define __EMPLOYEE_H
#include <iostream>
#include "mystring.h"
using namespace std;
class Employee
{
public:
enum WorkType
{
WorkType_NONE = 0,
WorkType_SalariedEmployee,
WorkType_CommissionEmployee,
WorkType_HourlyEmployee,
WorkType_BasePlusCommissionEmployee
};
Employee(const MyString &id = "123456",
WorkType workType = WorkType_NONE,
const MyString &name = "xiaoming",
const MyString &phone = "13356564545");
void setID(const MyString &id);
const MyString &getID() const;
void setWorkType(WorkType workType);
WorkType getWorkType() const;
void setName(const MyString &name);
const MyString &getName() const;
void setPhone(const MyString &phone);
const MyString &getphone() const;
double earnings() const ;
void display()const ;
private:
MyString m_id;
WorkType m_workType;
MyString m_name;
MyString m_phone;
};
#endif // __EMPLOYEE_H
employee.cpp
#include "employee.h"
Employee::Employee(const MyString &id ,
WorkType workType ,
const MyString &name ,
const MyString &phone )
{
m_id = id;
m_workType = workType;
m_name = name;
m_phone = phone;
}
void Employee::setID(const MyString &id)
{
m_id = id;
}
const MyString &Employee::getID() const
{
return m_id;
}
void Employee::setWorkType(WorkType workType)
{
m_workType = workType;
}
Employee::WorkType Employee::getWorkType() const
{
return m_workType;
}
void Employee::setName(const MyString &name)
{
m_name = name;
}
const MyString &Employee::getName() const
{
return m_name;
}
void Employee::setPhone(const MyString &phone)
{
m_phone = phone;
}
const MyString &Employee::getphone() const
{
return m_phone;
}
double Employee::earnings() const
{
return 0;
}
void Employee::display() const
{
cout << "Employee::ID: " << m_id << endl;
cout << "Employee::Type: " << m_workType << endl;
cout << "Employee::Name: " << m_name << endl;
cout << "Employee::Phone: " << m_phone << endl;
}
CommissionEmployee.h
#pragma once
#ifndef COMMISSION_EMPLOYEE_H
#define COMMISSION_EMPLOYEE_H
#include <iostream>
#include "employee.h"
class CommissionEmployee :virtual public Employee
{
public:
CommissionEmployee(const MyString &id = "123456",
const MyString &name = "xiaoming",
const MyString &phone = "13356564545",
double grossSales = 100,
double commissionRate = 0.35);
void setGrossSales(double grossSales);
double getGrossSales() const;
void setCommissionRate(double commissionRate);
double getCommissionRate() const;
double earnings() const;
void display() const;
private:
double m_grossSales;
double m_commissionRate;
};
#endif // !COMMISSION_EMPLOYEE_H
CommissionEmployee.cpp
#include "CommissionEmployee.h"
CommissionEmployee::CommissionEmployee(const MyString &id,
const MyString &name,
const MyString &phone,
double grossSales,
double commissionRate)
:Employee(id, Employee::WorkType_CommissionEmployee, name, phone)
{
m_grossSales = grossSales;
m_commissionRate = commissionRate;
}
void CommissionEmployee::setGrossSales(double grossSales)
{
m_grossSales = grossSales;
}
double CommissionEmployee::getGrossSales() const
{
return m_grossSales;
}
void CommissionEmployee::setCommissionRate(double commissionRate)
{
m_commissionRate = commissionRate;
}
double CommissionEmployee::getCommissionRate() const
{
return m_commissionRate;
}
double CommissionEmployee::earnings() const
{
return m_commissionRate + m_grossSales;
}
void CommissionEmployee::display() const
{
cout << "CommissionEmployee::ID: " << getID() << endl;
cout << "CommissionEmployee::Type: " << getWorkType() << endl;
cout << "CommissionEmployee::Name: " << getName() << endl;
cout << "CommissionEmployee::Phone: " << getphone() << endl;
cout << "CommissionEmployee::GrossSales: " << m_grossSales << endl;
cout << "CommissionEmployee::CommissionRate: " << m_commissionRate << endl;
}
HourlyEmployee.h
#pragma once
#ifndef __HOURLY_EMPLOYEE_H
#define __HOURLY_EMPLOYEE_H
#include <iostream>
#include "employee.h"
class HourlyEmployee:public Employee
{
public:
HourlyEmployee(const MyString &id = "123456",
const MyString &name = "xioaming",
const MyString &phone = "13356564545",
double wageForHour = 30,
double wageForOverTime = 35,
int hours = 48);
void setWageForHour(double wageForHour);
double getWageForHour() const;
void setWageForOverTime(double wageForOverTime);
double getWageForOverTime() const;
void setHours(int hours);
int getHours() const;
double earnings() const;
void display() const;
private:
double m_wageForHour;
double m_wageForOverTime;
int m_hours;
};
#endif // __HOURLY_EMPLOYEE_H
HourlyEmployee.cpp
#include "HourlyEmployee.h"
HourlyEmployee::HourlyEmployee(const MyString &id,
const MyString &name,
const MyString &phone,
double wageForHour,
double wageForOverTime,
int hours)
:Employee(id, Employee::WorkType_HourlyEmployee, name, phone)
{
m_wageForHour = wageForHour;
m_wageForOverTime = wageForOverTime;
m_hours = hours;
}
void HourlyEmployee::setWageForHour(double wageForHour)
{
m_wageForHour = wageForHour;
}
double HourlyEmployee::getWageForHour() const
{
return m_wageForHour;
}
void HourlyEmployee::setWageForOverTime(double wageForOverTime)
{
m_wageForOverTime = wageForOverTime;
}
double HourlyEmployee::getWageForOverTime() const
{
return m_wageForOverTime;
}
void HourlyEmployee::setHours(int hours)
{
m_hours = hours;
}
int HourlyEmployee::getHours() const
{
return m_hours;
}
double HourlyEmployee::earnings() const
{
double salary = 0;
if (m_hours > 40)
{
salary = 40 * m_wageForHour + (m_hours - 40) * m_wageForOverTime;
}
else
{
salary = m_hours * m_wageForHour;
}
return salary;
}
void HourlyEmployee::display() const
{
cout << "HourlyEmployee::ID: " << getID() << endl;
cout << "HourlyEmployee::Type: " << getWorkType() << endl;
cout << "HourlyEmployee::Name: " << getName() << endl;
cout << "HourlyEmployee::Phone: " << getphone() << endl;
cout << "HourlyEmployee::WageForHour: " << m_wageForHour << endl;
cout << "HourlyEmployee::WageForOverTime: " << m_wageForOverTime << endl;
cout << "HourlyEmployee::Hours: " << m_hours << endl;
}
SalariedEmployee.h
#pragma once
#ifndef __SALARIED_EMPLOYEE_H
#define __SALARIED_EMPLOYEE_H
#include <iostream>
#include "employee.h"
class SalariedEmployee:virtual public Employee
{
public:
SalariedEmployee(const MyString &id = "123456",
const MyString &name = "xiaoming",
const MyString &phone = "13356564545",
double weeklySalary = 1000);
void setWeeklySalary(double weeklySalary);
double getWeeklySalary() const;
double earnings() const;
void display() const;
private:
double m_weeklySalary;
};
#endif // __SALARIED_EMPLOYEE_H
SalariedEmployee.cpp
#include "SalariedEmployee.h"
SalariedEmployee::SalariedEmployee(const MyString &id, const MyString &name, const MyString &phone, double weeklySalary)
:Employee(id, Employee::WorkType_SalariedEmployee, name, phone)
{
m_weeklySalary = weeklySalary;
}
void SalariedEmployee::setWeeklySalary(double weeklySalary)
{
m_weeklySalary = weeklySalary;
}
double SalariedEmployee::getWeeklySalary() const
{
return m_weeklySalary;
}
double SalariedEmployee::earnings() const
{
return m_weeklySalary;
}
void SalariedEmployee::display() const
{
cout << "SalariedEmployee::ID: " << getID() << endl;
cout << "SalariedEmployee::Type: " << getWorkType() << endl;
cout << "SalariedEmployee::Name: " << getName() << endl;
cout << "SalariedEmployee::Phone: " << getphone() << endl;
cout << "SalariedEmployee::WeeklySalary: " << m_weeklySalary << endl;
}
BasePlusCommissionEmployee.h
#pragma once
#ifndef __BASEPLUSCOMMISSION_EMPLOYEE_H
#define __BASEPLUSCOMMISSION_EMPLOYEE_H
#include <iostream>
#include "CommissionEmployee.h"
#include "SalariedEmployee.h"
class BasePlusCommissionEmployee :public SalariedEmployee, public CommissionEmployee
{
public:
BasePlusCommissionEmployee(const MyString &id = "123456",
const MyString &name = "xiaoming",
const MyString &phone = "13356564545",
double weeklySalary = 1000,
double grossSales = 1000,
double commissionRate = 0.35);
double earnings() const;
void display() const;
};
#endif // __BASEPLUSCOMMISSION_EMPLOYEE_H
BasePlusCommissionEmployee.cpp
#include <iostream>
#include "BasePlusCommissionEmployee.h"
BasePlusCommissionEmployee::BasePlusCommissionEmployee(const MyString &id,
const MyString &name,
const MyString &phone,
double weeklySalary,
double grossSales,
double commissionRate) {};
double BasePlusCommissionEmployee::earnings() const
{
return getWeeklySalary() + getGrossSales() + getGrossSales() * getCommissionRate();
}
void BasePlusCommissionEmployee::display() const
{
cout << "BasePlusCommissionEmployee::ID: " << getID() << endl;
cout << "BasePlusCommissionEmployee::Type: " << getWorkType() << endl;
cout << "BasePlusCommissionEmployee::Name: " << getName() << endl;
cout << "BasePlusCommissionEmployee::Phone: " << getphone() << endl;
cout << "BasePlusCommissionEmployee::WeeklySalary: " << getWeeklySalary() << endl;
cout << "BasePlusCommissionEmployee::GrossSales: " << getGrossSales() << endl;
cout << "BasePlusCommissionEmployee::CommissionRate: " << getCommissionRate() << endl;
}