C++/열혈 C++ 프로그래밍(윤성우 저)

열혈 C++ 프로그래밍 8-1 문제

꾸준함. 2017. 6. 6. 12:06

/*

예제 EmployeeManager4.cpp 확장하여 다음 특성에 해당하는 ForeignSalesWorker 클래스를 추가로 정의해보자

"영업직 직원 일부는 오지산간으로 시장개척을 진행하고 있다. 일부는 아마존에서, 일부는 테러의 위험이 있는

지역에서 영업활동을 진행 중에 있다. 따라서 이러한 직원들을 대상으로 별도의 위험수당을 지급하고자 한다."

 

위험수당의 지급방식은 위험의 노출도에 따라서 다음과 같이 나뉘며, 한번 결정된 직원의 '위험 노출도' 변경되지 않는다고 가정한다(const)

1.리스크 A:영업직의 기본급여와 인센티브 합계 총액의 30% 추가로 지급한다

2.리스크 B:영업직의 기본급여와 인센티브 합계 총액의 20% 추가로 지급한다

3.리스크 C:영업직의 기본급여와 인센티브 합계 총액의 10% 추가로 지급한다

 

다음 main 함수와 함께 동작하도록 ForeignSlaesWorker 클래스를 정의하기 바라며, Employee 클래스는 객체 생성이 불가능한 추상 클래스로 정의하여라

int main(void)

{

        //직원과리를 목적으로 설계된 컨트롤 클래스의 객체 생성

        EmployeeHandler handler;

 

        //해외영업직 등록

        ForeignSalesWorker *fseller1 = new ForeignSalesWorker("Hong", 1000, 0.1, RISK_LEVEL::RISK_A);

        fseller1->AddSalesResult(7000); //영업실적 7000

        handler.AddEmployee(fseller1);

 

        ForeignSalesWorker *fseller2 = new ForeignSalesWorker("Yoon", 1000, 0.1, RISK_LEVEL::RISK_B);

        fseller2->AddSalesResult(7000); //영업실적 7000

        handler.AddEmployee(fseller2);

 

        ForeignSalesWorker *fseller3 = new ForeignSalesWorker("Lee", 1000, 0.1, RISK_LEVEL::RISK_C);;

        fseller3->AddSalesResult(7000); //영업실적 7000

        handler.AddEmployee(fseller3);

 

        //이번달에 지불해야 급여의 정보

        handler.ShowAllSalaryInfo();

        return 0;

}

*/

 

//EmployeeManager4.cpp

/*

#include <iostream>

#include <cstring>

using namespace std;

 

class Employee

{

private:

char name[100];

public:

Employee(char *name)

{

strcpy(this->name, name);

}

void ShowYourName() const

{

cout << "name: " << name << endl;

}

virtual int GetPay() const //바뀌어진 함수

{

return 0;

}

virtual void ShowSalaryInfo() const //바뀌어진 함수

{

}

};

 

class PermanentWorker :public Employee

{

private:

int salary; // 급여

public:

PermanentWorker(char *name, int money) :Employee(name), salary(money)

{

}

int GetPay() const

{

return salary;

}

void ShowSalaryInfo() const

{

ShowYourName();

cout << "salary: " << GetPay() << endl << endl;

}

};

 

class TemporaryWorker :public Employee

{

private:

int workTime; // 달에 일한 시간의 합계

int payPerHour; //시간당 급여

public:

TemporaryWorker(char *name, int pay) :Employee(name), workTime(0), payPerHour(pay)

{

}

void AddWorkTime(int time) //일한 시간의 추가

{

workTime += time;

}

int GetPay() const // 달의 급여

{

return workTime*payPerHour;

}

void ShowSalaryInfo() const

{

ShowYourName();

cout << "salary: " << GetPay() << endl << endl;

}

};

 

class SalesWorker :public PermanentWorker

{

private:

int salesResult; // 판매 실적

double bonusRatio; //상여금 비율

public:

SalesWorker(char *name, int money, double ratio) :PermanentWorker(name, money), salesResult(0), bonusRatio(ratio)

{

}

void AddSalesResult(int value)

{

salesResult += value;

}

int GetPay() const

{

return PermanentWorker::GetPay() + (int)(salesResult*bonusRatio);

}

void ShowSalaryInfo() const

{

ShowYourName();

cout << "salary: " << GetPay() << endl << endl; //SalesWorker GetPay 함수가 호출

}

};

 

class EmployeeHandler

{

private:

Employee *empList[50];

int empNum;

public:

EmployeeHandler() :empNum(0)

{

}

void AddEmployee(Employee *emp)

{

empList[empNum++] = emp;

}

void ShowAllSalaryInfo() const

{

for (int i = 0; i < empNum; i++)

empList[i]->ShowSalaryInfo(); //가상함수이므로 가장 마지막에 오버라이딩을 진행한 함수가 호출

}

void ShowTotalSalary() const

{

int sum = 0;

for (int i = 0; i < empNum; i++)

sum += empList[i]->GetPay();//가상함수이므로 가장 마지막에 오버라이딩을 진행한 함수가 호출

 

cout << "salary sum: " << sum << endl;

}

~EmployeeHandler()

{

for (int i = 0; i < empNum; i++)

delete empList[i];

}

};

 

int main(void)

{

//직원관리를 목적으로 설계된 컨트롤 클래스의 객체 생성

EmployeeHandler handler;

 

//정규직 등록

handler.AddEmployee(new PermanentWorker("KIM", 1000));

handler.AddEmployee(new PermanentWorker("LEE", 1500));

 

//임시직 등록

TemporaryWorker *alba = new TemporaryWorker("Jung", 700);

alba->AddWorkTime(5); //5시간 일한 경과 등록

handler.AddEmployee(alba);

 

//영업직 등록

SalesWorker *seller = new SalesWorker("Hong", 1000, 0.1);

seller->AddSalesResult(7000); //영업 실적 7000

handler.AddEmployee(seller);

 

//이번 달에 지불해야 급여의 정보

handler.ShowAllSalaryInfo();

 

//이번 달에 지불해야 급여의 총합

handler.ShowTotalSalary();

return 0;

}

 

*/

#include <iostream>

#include <cstring>

using namespace std;

 

namespace RISK_LEVEL //RISK_LEVEL 네임스페이스 정의

{

        enum

        {

               RISK_A=30,

               RISK_B=20,

               RISK_C=10

        };

}

 

class Employee

{

private:

        char name[100];

public:

        Employee(char *name)

        {

               strcpy(this->name, name);

        }

        void ShowYourName() const

        {

               cout << "name: " << name << endl;

        }

        virtual int GetPay() const = 0; //몸통이 없는 함수

        virtual void ShowSalaryInfo() const = 0; //몸통이 없는 함수

};

 

class PermanentWorker :public Employee

{

private:

        int salary; // 급여

public:

        PermanentWorker(char *name, int money) :Employee(name), salary(money)

        {

        }

        int GetPay() const

        {

               return salary;

        }

        void ShowSalaryInfo() const

        {

               ShowYourName();

               cout << "salary: " << GetPay() << endl << endl;

        }

};

 

class TemporaryWorker :public Employee

{

private:

        int workTime; // 달에 일한 시간의 합계

        int payPerHour; //시간당 급여

public:

        TemporaryWorker(char *name, int pay) :Employee(name), workTime(0), payPerHour(pay)

        {

        }

        void AddWorkTime(int time) //일한 시간의 추가

        {

               workTime += time;

        }

        int GetPay() const // 달의 급여

        {

               return workTime*payPerHour;

        }

        void ShowSalaryInfo() const

        {

               ShowYourName();

               cout << "salary: " << GetPay() << endl << endl;

        }

};

 

class SalesWorker :public PermanentWorker

{

private:

        int salesResult; // 판매 실적

        double bonusRatio; //상여금 비율

public:

        SalesWorker(char *name, int money, double ratio) :PermanentWorker(name, money), salesResult(0), bonusRatio(ratio)

        {

        }

        void AddSalesResult(int value)

        {

               salesResult += value;

        }

        int GetPay() const

        {

               return PermanentWorker::GetPay() + (int)(salesResult*bonusRatio);

        }

        void ShowSalaryInfo() const

        {

               ShowYourName();

               cout << "salary: " << GetPay() << endl << endl; //SalesWorker GetPay 함수가 호출

        }

};

 

class ForeignSalesWorker :public SalesWorker

{

private:

        int riskLevel;

public:

        ForeignSalesWorker(char *name, int money, double ratio, int risk) :SalesWorker(name, money, ratio), riskLevel(risk)

        {

        }

        int GetRiskPay() const

        {

               return (int)(SalesWorker::GetPay())*(riskLevel / 100.0);

        }

        int GetPay() const

        {

               return SalesWorker::GetPay() + GetRiskPay();

        }

        void ShowSalaryInfo() const

        {

               ShowYourName();

               cout << "salary: " << SalesWorker::GetPay() << endl;

               cout << "risk pay: " << GetRiskPay() << endl;

               cout << "total: " << GetPay() << endl << endl;

        }

};

 

class EmployeeHandler

{

private:

        Employee *empList[50];

        int empNum;

public:

        EmployeeHandler() :empNum(0)

        {

        }

        void AddEmployee(Employee *emp)

        {

               empList[empNum++] = emp;

        }

        void ShowAllSalaryInfo() const

        {

               for (int i = 0; i < empNum; i++)

                       empList[i]->ShowSalaryInfo(); //가상함수이므로 가장 마지막에 오버라이딩을 진행한 함수가 호출

        }

        void ShowTotalSalary() const

        {

               int sum = 0;

               for (int i = 0; i < empNum; i++)

                       sum += empList[i]->GetPay();//가상함수이므로 가장 마지막에 오버라이딩을 진행한 함수가 호출

 

               cout << "salary sum: " << sum << endl;

        }

        ~EmployeeHandler()

        {

               for (int i = 0; i < empNum; i++)

                       delete empList[i];

        }

};

 

int main(void)

{

        //직원과리를 목적으로 설계된 컨트롤 클래스의 객체 생성

        EmployeeHandler handler;

 

        //해외영업직 등록

        ForeignSalesWorker *fseller1 = new ForeignSalesWorker("Hong", 1000, 0.1, RISK_LEVEL::RISK_A);

        fseller1->AddSalesResult(7000); //영업실적 7000

        handler.AddEmployee(fseller1);

 

        ForeignSalesWorker *fseller2 = new ForeignSalesWorker("Yoon", 1000, 0.1, RISK_LEVEL::RISK_B);

        fseller2->AddSalesResult(7000); //영업실적 7000

        handler.AddEmployee(fseller2);

 

        ForeignSalesWorker *fseller3 = new ForeignSalesWorker("Lee", 1000, 0.1, RISK_LEVEL::RISK_C);

        fseller3->AddSalesResult(7000); //영업실적 7000

        handler.AddEmployee(fseller3);

 

        //이번달에 지불해야 급여의 정보

        handler.ShowAllSalaryInfo();

        return 0;

}

개발 환경:Visual Studio 2017


지적, 조언, 질문 환영입니다! 댓글 남겨주세요~


[참고] 열혈 C++ 프로그래밍 윤성우 저

반응형