출처 : 윤성우의 열혈 c++ 프로그래밍
직원 정보 클래스
직원 핸들러 클래스
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];
}
}
};
-> 이렇게 기능의 처리를 실제로 담당하는 클래스를 가리켜 '컨트롤(control) 클래스 또는 '핸들러(handler)클래스' 라 한다.
Person 클래스
class Person
{
private:
int age;
char name[50];
public:
Person(char *myname, int myage) : age(myage)
{
strcpy(name, myname);
}
void WhatYourName() const
{
cout << "My name is " << name << endl;
}
void HowOldAreYou() const
{
cout << "My age is " << age << endl;
}
};
UnivStudent 클래스
class UnivStudent : public Person
{
private:
char major[50];
public:
UnivStudent(char *myname, int myage, char *mymajor) : Person(myage, myname);
{
strcpy(major, mymajor);
}
void WhoAreYou() const
{
WhatYourName();
HowOldAreYou();
cout << "My major is " << major << endl;
}
};
상속을 위한 기본 조건인 IS - A 관계의 성립
-> 무선 전화기 " is a " 전화기
-> 노트북 컴퓨터 "is a" 컴퓨터
#include <iostream>
#include <cstring>
using namespace std;
class Computer
{
private:
char owner[50];
public:
Computer(char *name)
{
strcpy(owner, name);
}
void Calculate()
{
cout << "요청 내용을 계산합니다." << endl;
}
};
class NotebookComp : public Computer
{
private:
int Battery;
public:
NotebookComp(char *name, int initChag) : Computer(name), Battery(initChag) {}
void Charging { Battery += 5; }
void UseBattery { Battery -= 1; }
void MovingCal()
{
if (GetBatteryInfo() < 1)
{
cout << "충전이 필요합니다" << endl;
return;
}
cout << "이동하면서";
Calculate();
UseBattery();
}
int GetBatteryInfo() { return Battery; }
};
class TabletNotebook : public NotebookComp
{
private:
char regstPenModel[50];
public:
TabletNotebook(char *name, int initChag, char *pen) : NotebookComp(name, initChag)
{
strcpy(regstPenModel, pen);
}
void Write(char *penInfo)
{
if (GetBatteryInfo() < 1)
{
cout << "충전이 필요합니다" << endl;
return;
}
if (strcmp(regstPenModel, penInfo) != 0)
{
cout << "등록된 펜이 아닙니다" < endl;
return;
}
cout << "필기 내용을 처리합니다." << endl;
UseBattery();
}
};
int main(void)
{
NotebookComp nc("정진철", 5);
TabletNotebook tn("정진철", 6, "ISE-231-223");
nc.MovingCal();
tn.Write("ISE-231-223");
return 0;
}
HAS - A 관계의 상속
#include <iostream>
#include <cstring>
using namespace std;
class Gun
{
private:
int bullet;
public:
Gun(int bnum) : bullet(bnum) { }
void Shot()
{
cout << "BBANG !!" << endl;
bullet--;
}
}
class Police
{
private:
int handcuffs;
Gun * pistol;
public:
Police(int bnum, int bcuff) : handcuffs(bcuff)
{
if(bnum > 0)
pistol = new Gun(bnum);
else
pistol = NULL;
}
void PutHandcuff()
{
cout <<"SNAP!" << endl;
handCuff --;
}
void Shot()
{
if(pistol == NULL)
cout << "Hut BBANG!! " << endl;
else
pistol->Shot();
}
~Poilce()
{
if(pistol != NULL)
delete pistol;
}
};
int main(void)
{
Police pman1(5,3);
pman1.Shot();
pman1.PutHandcuff();
Police pman2(0,3);
pman2.Shot();
pman2.PutHandcuff();
return 0;
}