공부 중 메모/수업 예제 (KGCA)

Cpp 다형성 예제(2) (직원 급여 관리 예제)

라이피 (Lypi) 2018. 7. 15. 21:48
반응형

Employee.h

 #pragma once

#include <iostream>
#include <string>
#include <math.h>

using std::cout;
using std::cin;
using std::endl;
using std::string;


class Employee  //기본 추상 클래스
{

protected:
	string	m_sName;
	int		m_iPositionLevel;

public:

	string getName() const { return m_sName; }
	virtual int getPay() = 0;	//순수가상함수 //급여를 계산하는 함수

	void PrintData();


	Employee(string name, int PL);
};

Employee.cpp

 #include "Employee.h"

void Employee::PrintData()
{
	cout << "이름 : " << m_sName << "직급 : " << m_iPositionLevel << endl;
}

Employee::Employee(string name, int PL)
{
	m_sName = name;
	m_iPositionLevel = PL;
}

Temporary.h

 #pragma once

#include "Employee.h"

class Temporary : public Employee //시급제
{
	int m_iTime;

public:
	int getPay() override;

	Temporary(string name, int PL, int Time);
};

Temporary.cpp

 #include "Temporary.h"

int Temporary::getPay()
{
	return m_iTime * (int)pow(2, m_iPositionLevel-1);
}

Temporary::Temporary(string Name, int PL, int Time) : Employee(Name, PL)
{
	m_iTime = Time;
}

Salaryman.h

 #pragma once

#include "Employee.h"

class Salaryman : public Employee //월급제
{
	int m_iSalary;
	int m_iLevel;

public:
	int getPay() override;

	Salaryman(string name, int PL, int Level);
};

Salaryman.cpp

 #include "Salaryman.h"

int Salaryman::getPay()
{
	return (m_iSalary + ((m_iPositionLevel-1) * 20) + (m_iLevel * 5));
}

Salaryman::Salaryman(string Name, int PL, int Level) : Employee(Name, PL)
{
	m_iSalary = 200;
	m_iLevel = Level;
}

Permanent.h

 #pragma once

#include "Employee.h"

class Permanent : public Employee //연봉제
{
	int m_iSalary;
	int m_iLevel;

public:
	//추상클래스를 상속한 클래스는 부모의 순수가상함수를 꼭 구현해야한다. 
	int getPay() override;	//재정의

	Permanent(string name, int PL, int Level);
};

Permanent.cpp

 #include "Permanent.h"

int Permanent::getPay()
{
	return (m_iSalary + ((m_iPositionLevel-1) * 1000) + (m_iLevel * 500)) / 12;
}

Permanent::Permanent(string Name, int PL, int Level) : Employee(Name, PL)
{
	m_iSalary = 2000;
	m_iLevel = Level;
}

main.cpp

 #include "SalaryCalculator.h"

int main()
{
	//순수 가상 함수가 하나라도 있는 클래스를 추상 클래스라고 하며, 추상 클래스는 인스턴스화를 할 수 없다. (=객체를 만들 수 없다.)
	//Employee fvfuse;

	SalaryCalculator department;

	//직원 등록.
	department.AddEmployee(new Temporary("권상우", 1, 200));
	department.AddEmployee(new Temporary("조인성", 1, 220));
	department.AddEmployee(new Salaryman("감우성", 2, 3));
	department.AddEmployee(new Salaryman("강동원", 2, 5));
	department.AddEmployee(new Permanent("황정민", 3, 3));
	department.AddEmployee(new Salaryman("박중훈", 3, 15));
	department.AddEmployee(new Salaryman("최민식", 4, 20));
	department.AddEmployee(new Permanent("정진영", 4, 3));
	department.AddEmployee(new Temporary("안성기", 5, 50));
	department.AddEmployee(new Permanent("송강호", 5, 4));
	

	//최종적으로 이번달에 지불해야할 급여는?
	department.ShowList();
}


반응형