미누에요
[C++] 윤성우의 열혈 C++ 08-1 상속 관계의 확장과 추상 클래스 문제풀이 본문
728x90
반응형
SMALL
예제 Employeemanager4.cpp를 확장하여 다음 특성에 해당하는 ForeginSalesWorker 클래스를 추가로 정의해보자.
Employeemanager4.cpp
#include <iostream>
#include <cstring>
using namespace std;
class Employee
{
private:
char name[50];
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 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 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 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);
handler.AddEmployee(alba);
SalesWorker *seller = new SalesWorker("Hong", 1000, 0.1);
seller->AddSalesResult(7000);
handler.AddEmployee(seller);
handler.ShowAllSalaryInfo();
handler.ShowTotalSalary();
return 0;
}
위험수당의 지급 방식은 '위험의 노출도'에 따라서 다음과 같이 나뉘며, 한번 결정된 직원의 '위험 노출도'는 변경되지 않는다고 가정한다.(이는 const 멤버변수의 선언을 유도하는 것이다.)
- 리스크 A : 영업직의 기본급여와 인센티브 합계 총액의 30%를 추가로 지급한다.
- 리스크 B : 영업직의 기본급여와 인센티브 합계 총액의 20%를 추가로 지급한다.
- 리스크 C : 영업직의 기본급여와 인센티브 합계 총액의 10%를 추가로 지급한다.
다음 main 함수와 함께 동작하도록 ForeginSalesWorker 클래스를 정의하기 바라며, 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;
namespace RISK_LEVEL
{
enum
{
RISK_A = 30,
RISK_B = 20,
RISK_C = 10,
};
}; // namespace RISK_LEVEL
class Employee
{
private:
char name[50];
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 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:
int risk;
public:
ForeignSalesWorker(char *name, int money, double ratio, int risk_level) : SalesWorker(name, money, ratio), risk(risk_level) {}
int GetPay() const
{
return SalesWorker::GetPay() + SalesWorker::GetPay() * (risk / 100.0);
}
int GetRiskPay() const
{
return SalesWorker::GetPay() * (risk / 100.0);
}
void ShowSalaryInfo() const
{
ShowYourName();
cout << "salary : " << SalesWorker::GetPay() << endl;
cout << "risk pay : " << GetRiskPay() << endl;
cout << "sum : " << SalesWorker::GetPay() + GetRiskPay() << 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 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);
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
'C++' 카테고리의 다른 글
[C++] 윤성우의 열혈 C++ 10-2 단항 연산자 오버로딩 문제풀이 (0) | 2024.05.12 |
---|---|
[C++] 윤성우의 열혈 C++ 10-1 두 가지 방법의 연산자 오버로딩 문제풀이 (0) | 2024.05.11 |
[C++] 윤성우의 열혈 C++ 프로그래밍 ch 07-2 : IS-A 관계의 상속 (0) | 2024.04.14 |
[C++] 윤성우의 열혈 C++ 프로그래밍 ch 07-1 : 상속과 생성자의 호출 문제풀이 (0) | 2024.04.14 |
[C++] this 포인터 (0) | 2024.03.25 |