요즘 c++ 배우고 있는데 가상함수 복잡복잡 하네요
#include <iostream> #include <cstring> using namespace std; class Employee { private: char name[100]; public: Employee(char * name) { strcpy(this->name, name); } virtual int GetPay() const =0; virtual void ShowSalaryInfo() const =0; void ShowYourName() const { cout<<"Name: "<<name<<endl; } }; 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 EmployeeHandler { private: Employee * empList[50]; int empNum; public: EmployeeHandler() : empNum(0) { } void AddEmployee(Employee * emp) { empList[empNum++]=emp; } void ShowAllSalaryInfo() const { } void ShowTotalSalary() const { int sum=0; cout<<"salary sum: "<<sum<<endl; } ~EmployeeHandler() { for(int i=0; i<empNum; i++) delete empList[i]; } }; 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 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; } }; 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; } |
'Programming' 카테고리의 다른 글
[dovelet]-crypt (0) | 2014.01.20 |
---|---|
os 판별 코드 (0) | 2013.10.11 |
Binary Search (0) | 2013.07.18 |
좋은 사이트.. (0) | 2013.07.13 |
BlackJack in Python (0) | 2013.05.12 |