Minwoo Dev.

[C++] 윤성우의 열혈 C++ 08-1 상속 관계의 확장과 추상 클래스 문제풀이 본문

C++

[C++] 윤성우의 열혈 C++ 08-1 상속 관계의 확장과 추상 클래스 문제풀이

itisminu 2024. 7. 17. 22:21
728x90
반응형
SMALL

문제

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

"영업직  직원 중 일부는 오지산간으로 시장개척을 진행하고 있다. 일부는 아마존에서, 또 일부는 테러의 위험이 있는 지역에서 영업활동을 진행 중에 있다. 따라서 이러한 직원들을 대상으로 별도의 위험수당을 지급하고자 한다."

 

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

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

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

 

 

[main함수]

int main(void){
    EmployeeHandler handler;

    ForeignSalesWorker *fseller1 = new ForeignSalesWorker("Hong", 1000, 0.1, RISK_LEVEL::RISK_A);
    fseller1->AddSalesResult(7000);
    handler.AddEmployee(fseller1);

    ForeignSalesWorker *fseller2 = new ForeignSalesWorker("Yoon", 1000, 0.1, RISK_LEVEL::RISK_B);
    fseller2->AddSalesResult(7000);
    handler.AddEmployee(fseller2);

    ForeignSalesWorker *fseller3 = new ForeignSalesWorker("Lee", 1000, 0.1, RISK_LEVEL::RISK_C);
    fseller3->AddSalesResult(7000);
    handler.AddEmployee(fseller3);

    handler.ShowAllSalaryInfo();
    return 0;
}

 

 

 

 

 

 

 

 

 

 

정답

#include<iostream>
#include<cstring>
using namespace std;

enum RISK_LEVEL
{
    RISK_A = 3,
    RISK_B = 2,
    RISK_C = 1
};

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;
    }
};

class ForeignSalesWorker : public SalesWorker{
private:
    double riskRatio;
public:
    ForeignSalesWorker(char* name, int money, double ratio, int risk):SalesWorker(name,money,ratio),riskRatio(risk/10.0){}
    int GetPay() const{
        return SalesWorker::GetPay() * (1 + riskRatio);
    }
    void ShowSalaryInfo() const{
        ShowYourName();
        cout << "salary : " << SalesWorker::GetPay() << endl;
        cout << "risk pay : " << riskRatio * SalesWorker::GetPay() << endl;
        cout << "sum : " << 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 num : " << 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);
    handler.AddEmployee(fseller1);

    ForeignSalesWorker *fseller2 = new ForeignSalesWorker("Yoon", 1000, 0.1, RISK_LEVEL::RISK_B);
    fseller2->AddSalesResult(7000);
    handler.AddEmployee(fseller2);

    ForeignSalesWorker *fseller3 = new ForeignSalesWorker("Lee", 1000, 0.1, RISK_LEVEL::RISK_C);
    fseller3->AddSalesResult(7000);
    handler.AddEmployee(fseller3);

    handler.ShowAllSalaryInfo();
    return 0;
}

 

 

실행 결과

728x90
반응형
LIST