본문 바로가기

Programming/C++ 3

[C++] 07-01. 상속에 대한 이해와 접근 [급여관리시스템 1단계]

반응형

상속에 대한 접근

1단계: 문제의 제시

2단계: 기본개념 소개

3단계: 문제의 해결

 

-> 상속은 기존에 정의해 놓은 클래스의 재활용을 목적으로 만들어진 문법적 요소

 


급여관리 시스템 - 1단계

: 직원마다 각자의 객체를 생성 

 

  • 연봉제 정규직 [데이터 중심 클래스]
  • 컨트롤 클래스 [컨트롤 클래스 객체를 통해서 프로그램이 동작]

//상속을 하는 시기, 상속하는 이유, 상속의 장점
#include <iostream>
#include <cstring>
using namespace std;

class PermanentWorker
{
private:
	char name[100];
	int salary;
public:
	PermanentWorker(const char* name, int money) //const char*로 선언
		: salary(money)
	{
		strcpy_s(this->name,strlen(name)+1, name); //strcpy_s(복사할 대상, 크기,복사될 대상)
	}
	int GetPay() const
	{
		return salary;
	}
	void ShowSalaryInfo() const
	{
		cout << "name: " << name << endl;
		cout << "salary: " << GetPay() << endl << endl;
	}
};

class EmployeeHandler //프로그램 전체의 기능을 처리하고, 흐름을 담당
{
private:
	PermanentWorker* empList[50];
	int empNum;
public:
	EmployeeHandler() : empNum(0)
	{

	}
	void AddEmployee(PermanentWorker* 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));
	handler.AddEmployee(new PermanentWorker("JUN", 2000));

	// 이번 달에 지불해야 할 급여의 정보
	handler.ShowAllSalaryInfo();

	// 이번 달에 지불해야 할 급여의 총합
	handler.ShowTotalSalary();
	return 0;
}

 

 

 

 


상속에 대한 접근

1단계: 문제의 제시

2단계: 기본개념 소개

3단계: 문제의 해결

 


급여관리 시스템

  • 연봉제 정규직
  • 인센티브 영업직
  • 시간제 임시직

-> 영업직과 임시직에 해당하는 클래스의 추가로 끝나는게 아닌, 컨트롤 클래스의 대대적인 변경이 필요함

 

요구사항의 변경 및 기능의 추가에 따른 변경을 최소화하기 위한 방법 -> 상속

 

반응형