4. 다형성
by QerogramEmployee 하위 클래스로, PermanentWorker, TemporaryWorker, 등을 생성해
EmployeeHandler로 관리를 해봄.
- 열혈C++ 속의 문제라고 하는데, 구현을 해봄.
다형성, 상속, virtual function 모두 사용.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | #include <iostream> #include <cstring> using namespace std; class Employee { protected : char name[100]; public : Employee(char* name) { strcpy(this->name, name); } virtual void ShowYourName() const { } 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 ShowYourName() const { cout << name << "\t" << this->GetPay() << endl; } void ShowSalaryInfo() const { ShowYourName(); } }; 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(); } }; class SalesWorker : public PermanentWorker{ private : int salesResult; double bonusRatio; public : SalesWorker(char* name, int money, double ratio) : PermanentWorker(name, money), bonusRatio(ratio), salesResult(0) { } void ShowYourName() const { cout << name << "\t" <<this->PermanentWorker::GetPay() << "\t" << bonusRatio << endl; } int GetPay() const{ return PermanentWorker::GetPay() + (int)(salesResult*bonusRatio); } void ShowSalaryInfo() const { ShowYourName(); GetPay(); } void AddSalesResult(int value) { salesResult+=value; } }; class EmployeeHandler { private : Employee* empList[50]; int empNum; public : EmployeeHandler() : empNum(0) { } void AddEmployee(Employee* emp) { empList[empNum++] = emp; } void ShowSalaryInfo() { for(int i=0; i<empNum; ++i) empList[i]->ShowYourName(); } void ShowTotalSalary() const{ } }; int main(int argc, char* argv[]) { EmployeeHandler eh; eh.AddEmployee(new SalesWorker("qerogram", 1000, 0.5)); eh.AddEmployee(new PermanentWorker("qerogram2", 3000)); eh.ShowSalaryInfo(); return 0; } | cs |
* 결과
qerogram 1000 0.5
qerogram2 3000
'코딩 > C&C++' 카테고리의 다른 글
6. Quick Sort (0) | 2017.04.10 |
---|---|
5. void PTR (0) | 2017.04.09 |
3. 복사생성자를 통한 깊은 복사. (0) | 2017.04.08 |
2. 연산자 오버로딩(Overloading) (0) | 2017.04.08 |
1. Template (0) | 2017.04.07 |
블로그의 정보
Data+
Qerogram