열혈 c++ 프로그래밍을 보고 요약정리합니다.
class Employee
{
private:
char name[100];
public:
Employee(char * name)
{
strcpy(this->name, name);
}
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
{
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() {
EmployeeHandler handler;
handler.AddEmployee(new PermanentWorker("KIM", 1000));
handler.AddEmployee(new PermanentWorker("LEE", 1500));
handler.AddEmployee(new PermanentWorker("JUN", 2000));
handler.ShowAllSalaryInfo();
handler.ShowAllSalaryInfo();
return 0;
}
영업직 급여는 “기본급여(월 기본급여) + 인센티브” 형태
임시직 급여는 “시간당 급여 * 일한 시간” 형태
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;
}
};
class First
{
public:
void MyFunc() const
{
cout << "FirstFunc" << endl;
}
};
class Second : public First
{
public:
void MyFunc() const
{
cout << "SecondFunc" << endl;
}
};
class Third : public Second
{
public:
void MyFunc() const
{
cout << "ThirdFunc" << endl;
}
};
int main() {
Third * tptr = new Third();
Second * sptr = tptr;
First * fptr = sptr;
fptr->MyFunc(); //FirstFunc
sptr->MyFunc(); //SecondFunc
tptr->MyFunc(); //ThirdFunc
delete tptr;
return 0;
}
class First
{
public:
virtual void MyFunc() const
{
cout << "FirstFunc" << endl;
}
};
class Second : public First
{
public:
virtual void MyFunc() const
{
cout << "SecondFunc" << endl;
}
};
class Third : public Second
{
public:
virtual void MyFunc() const
{
cout << "ThirdFunc" << endl;
}
};
int main() {
Third * tptr = new Third();
Second * sptr = tptr;
First * fptr = sptr;
fptr->MyFunc(); //ThirdFunc
sptr->MyFunc(); //ThirdFunc
tptr->MyFunc(); //ThirdFunc
delete tptr;
return 0;
}
Employee * employee = new Employee();
class Employee
{
private:
char name[100];
public:
Employee(char * name)
{
strcpy(this->name, name);
}
void ShowYourName() const
{
cout << "name: " << name << endl;
}
virtual int Getpay() const = 0; // 순수 가상함수
virtual int ShowSalaryInfo() const = 0; //순수 가상함수
};
A * a = new B();
delete a; //A클래스의 소멸자만 실행됨.
class A
{
public:
virtual ~A()
{
....
}
}