29. [C++] 가상 함수(Virtual Function)

2024. 4. 16. 17:43[C++]/C++ 언어 기초

 


실습 문제

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

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

 

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

  • 리스크 A: 영업직의 기본급여와 인센티브 합계 총액의 30%를 추가로 지급한다
  • 리스크 B: 영업직의 기본급여와 인센티브 합계 총액의 20%를 추가로 지급한다
  • 리스크 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;
}

 

내가 푼 코드

#include "stdafx.h"
#include <iostream>
#include <cstring>

using namespace std;


namespace RISK_LEVEL{
	enum{RISK_A = 30, RISK_B = 20, RISK_C = 10};
	//* 참고 상수는 정수형만 올 수 있음.
}

//부모 클래스
class Employee
{
private:
	char name[100];
public:
	Employee(const 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(const 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(const 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(const 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 ShowSalrayInfo() const
	{
		ShowYourName();
		cout << "salary: " << GetPay() << endl << endl;
	}
};


//ForeignSalesWorker 코드 추가
class ForeignSalesWorker : public SalesWorker
{
private:
	const int risk; //risk에 따른 인센티브 지급
public:
	//생성자
	ForeignSalesWorker(const char* name, int money, double ratio, int risks) : SalesWorker(name, money, ratio), risk(risks){}

	int GetRiskPay() const{
		return (int)(SalesWorker::GetPay() * (risk/100.0)); //인센티브 = (급여 + (급여*위험도%))
	}
    
	int GetPay() const{
		return SalesWorker::GetPay() + GetRiskPay();  //ForeignSalesWorker의 페이는 SalesWorker의 Pay에서 RiskPay를 더한 값
	}

	void ShowSalaryInfo() const{
		ShowYourName();
		cout << "salary: " << SalesWorker::GetPay() << endl;
		cout << "risk pay: " << GetRiskPay() << endl; //추가
		cout << "sum: " << SalesWorker::GetPay() + GetRiskPay() << endl<<endl; //추가 - SalesWorker의 페이와 리스크 페이를 더한 값
	}

};

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;
}
반응형